summaryrefslogtreecommitdiffstats
path: root/lib/generators/ebook.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-23 00:16:46 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-23 00:16:46 +0100
commit19f1d31758727d09a173037583d8ed765d539a33 (patch)
tree2e47ef2542e5ff0f42e15a034cb0d35af7a83331 /lib/generators/ebook.js
parentee2420cbcca41f3c33a243084b11fd9946b87ed1 (diff)
downloadgitbook-19f1d31758727d09a173037583d8ed765d539a33.zip
gitbook-19f1d31758727d09a173037583d8ed765d539a33.tar.gz
gitbook-19f1d31758727d09a173037583d8ed765d539a33.tar.bz2
Add base for ebook generator
Diffstat (limited to 'lib/generators/ebook.js')
-rw-r--r--lib/generators/ebook.js114
1 files changed, 114 insertions, 0 deletions
diff --git a/lib/generators/ebook.js b/lib/generators/ebook.js
new file mode 100644
index 0000000..fa5dcbd
--- /dev/null
+++ b/lib/generators/ebook.js
@@ -0,0 +1,114 @@
+var util = require("util");
+var path = require("path");
+var Q = require("q");
+var _ = require("lodash");
+var exec = require('child_process').exec;
+
+var fs = require("../utils/fs");
+var stringUtils = require("../utils/string");
+var BaseGenerator = require("./website");
+
+var Generator = function(book, format) {
+ BaseGenerator.apply(this, arguments);
+
+ // eBook format
+ this.ebookFormat = format || "pdf";
+
+ // Styles to use
+ this.styles = ["ebook", this.ebookFormat];
+};
+util.inherits(Generator, BaseGenerator);
+
+Generator.prototype.prepareTemplates = function() {
+ this.templates["page"] = this.plugins.template("ebook:page") || path.resolve(this.options.theme, 'templates/ebook/page.html');
+ this.templates["summary"] = this.plugins.template("ebook:summary") || path.resolve(this.options.theme, 'templates/ebook/summary.html');
+ this.templates["glossary"] = this.plugins.template("ebook:glossary") || path.resolve(this.options.theme, 'templates/ebook/glossary.html');
+
+ return Q();
+};
+
+// Generate table of contents
+Generator.prototype.writeSummary = function() {
+ var that = this;
+
+ return this._writeTemplate(this.templates["summary"], {}, path.join(this.options.output, "SUMMARY.html"));
+};
+
+
+Generator.prototype.langsIndex = function(langs) {
+ return Q();
+};
+
+Generator.prototype.finish = function() {
+ var that = this;
+
+ return BaseGenerator.prototype.finish.apply(this)
+ .then(function() {
+ return that.writeSummary();
+ })
+ .then(function() {
+ var d = Q.defer();
+
+ if (!that.options.cover && fs.existsSync(path.join(that.options.output, "cover.jpg"))) {
+ that.options.cover = path.join(that.options.output, "cover.jpg");
+ }
+
+ var _options = {
+ "--cover": that.options.cover,
+ "--title": that.options.title,
+ "--comments": that.options.description,
+ "--isbn": that.options.isbn,
+ "--authors": that.options.author,
+ "--publisher": "GitBook",
+ "--chapter": "descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter ')]",
+ "--chapter-mark": "pagebreak",
+ "--page-breaks-before": "/",
+ "--level1-toc": "descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-1 ')]",
+ "--level2-toc": "descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-2 ')]",
+ "--level3-toc": "descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-3 ')]",
+ "--no-chapters-in-toc": true,
+ "--max-levels": "1",
+ "--breadth-first": true
+ };
+
+ if (that.ebookFormat == "pdf") {
+ var pdfOptions = that.options.pdf;
+
+ _.extend(_options, {
+ "--margin-left": String(pdfOptions.margin.left),
+ "--margin-right": String(pdfOptions.margin.right),
+ "--margin-top": String(pdfOptions.margin.top),
+ "--margin-bottom": String(pdfOptions.margin.bottom),
+ "--pdf-default-font-size": String(pdfOptions.fontSize),
+ "--pdf-mono-font-size": String(pdfOptions.fontSize),
+ "--paper-size": String(pdfOptions.paperSize),
+ "--pdf-page-numbers": Boolean(pdfOptions.pageNumbers),
+ "--pdf-header-template": String(pdfOptions.headerTemplate),
+ "--pdf-footer-template": String(pdfOptions.footerTemplate)
+ });
+ }
+
+ var command = [
+ "ebook-convert",
+ path.join(that.options.output, "SUMMARY.html"),
+ path.join(that.options.output, "index."+that.ebookFormat),
+ stringUtils.optionsToShellArgs(_options)
+ ].join(" ");
+
+ exec(command, function (error, stdout, stderr) {
+ if (error) {
+ if (error.code == 127) {
+ error.message = "Need to install ebook-convert from Calibre";
+ } else {
+ error.message = error.message + " "+stdout;
+ }
+ return d.reject(error);
+ }
+ d.resolve();
+ });
+
+ return d.promise;
+ });
+};
+
+module.exports = Generator;