summaryrefslogtreecommitdiffstats
path: root/lib/generators/ebook.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2015-10-03 20:24:56 +0200
committerSamy Pessé <samypesse@gmail.com>2015-10-06 09:47:04 +0200
commit33593c13ae4e7b4db62a56d76ffee48a2e5f5d5e (patch)
tree30d6a0feaac4cd93751c86f6b9fb87bb305b1d83 /lib/generators/ebook.js
parenteef82c18c2959a09ea65a5440e007a82d1414ee3 (diff)
downloadgitbook-33593c13ae4e7b4db62a56d76ffee48a2e5f5d5e.zip
gitbook-33593c13ae4e7b4db62a56d76ffee48a2e5f5d5e.tar.gz
gitbook-33593c13ae4e7b4db62a56d76ffee48a2e5f5d5e.tar.bz2
Inline css for footer/header templates
Diffstat (limited to 'lib/generators/ebook.js')
-rw-r--r--lib/generators/ebook.js36
1 files changed, 34 insertions, 2 deletions
diff --git a/lib/generators/ebook.js b/lib/generators/ebook.js
index e6a33d3..075fca5 100644
--- a/lib/generators/ebook.js
+++ b/lib/generators/ebook.js
@@ -2,6 +2,7 @@ var util = require("util");
var path = require("path");
var Q = require("q");
var _ = require("lodash");
+var juice = require("juice");
var exec = require("child_process").exec;
var fs = require("../utils/fs");
@@ -41,6 +42,37 @@ Generator.prototype.writeSummary = function() {
return this._writeTemplate(this.templates.summary, {}, path.join(this.options.output, "SUMMARY.html"));
};
+// Return template for footer/header with inlined css
+Generator.prototype.getPDFTemplate = function(id) {
+ var tpl = this.options.pdf[id+'Template'];
+ var defaultTpl = path.resolve(this.options.theme, 'templates/ebook/'+id+'.html');
+ var defaultCSS = path.resolve(this.options.theme, 'assets/print.css');
+
+ // Default template from theme
+ if (!tpl && fs.existsSync(defaultTpl)) {
+ tpl = fs.readFileSync(defaultTpl, { encoding: 'utf-8' });
+ }
+
+ // Inline CSS using juice
+ var stylesheets = [];
+
+ // From theme
+ if (fs.existsSync(defaultCSS)) {
+ stylesheets.push(fs.readFileSync(defaultCSS, { encoding: 'utf-8' }));
+ }
+
+ // Custom PDF style
+ if (this.styles.pdf) {
+ stylesheets.push(fs.readFileSync(this.book.resolve(this.styles.pdf), { encoding: 'utf-8' }));
+ }
+
+ tpl = juice(tpl, {
+ extraCss: stylesheets.concat('/n')
+ });
+
+ return tpl;
+};
+
Generator.prototype.finish = function() {
var that = this;
@@ -90,8 +122,8 @@ Generator.prototype.finish = function() {
"--pdf-mono-font-size": String(pdfOptions.fontSize),
"--paper-size": String(pdfOptions.paperSize),
"--pdf-page-numbers": Boolean(pdfOptions.pageNumbers),
- "--pdf-header-template": String(pdfOptions.headerTemplate) || "<p class='header'><span>"+that.options.title+"</span></p>",
- "--pdf-footer-template": String(pdfOptions.footerTemplate) || "<p class='footer'><span>_SECTION_</span> <span style='float:right;'>_PAGENUM_</span></p>"
+ "--pdf-header-template": that.getPDFTemplate('header'),
+ "--pdf-footer-template": that.getPDFTemplate('footer')
});
} else if (that.ebookFormat == "epub") {
_.extend(_options, {