summaryrefslogtreecommitdiffstats
path: root/lib/book.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-24 18:54:20 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-24 18:54:20 +0100
commit1583ede3d7ae005f7bd399ab587ca9bddfe845ca (patch)
tree51620a7fa837c1160ef3c81d29b16211c8bbe6b5 /lib/book.js
parent572994a17e24b51624437e5b0f5da44a6156ffd7 (diff)
downloadgitbook-1583ede3d7ae005f7bd399ab587ca9bddfe845ca.zip
gitbook-1583ede3d7ae005f7bd399ab587ca9bddfe845ca.tar.gz
gitbook-1583ede3d7ae005f7bd399ab587ca9bddfe845ca.tar.bz2
Add Book.generateFile to output a pdf
Diffstat (limited to 'lib/book.js')
-rw-r--r--lib/book.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/book.js b/lib/book.js
index bde30da..a2bd40b 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -237,6 +237,59 @@ Book.prototype.generateMultiLingual = function(generator) {
});
};
+// Extract files from ebook generated
+Book.prototype.generateFile = function(output, options) {
+ var book = this;
+
+ options = _.defaults(options || {}, {
+ ebookFormat: path.extname(output).slice(1)
+ });
+ output = output || path.resolve(book.root, "book."+options.ebookFormat);
+
+ return fs.tmp.dir()
+ .then(function(tmpDir) {
+ book.config.extend({
+ output: tmpDir
+ });
+
+ return book.generate(options.ebookFormat)
+ .then(function(_options) {
+ var copyFile = function(lang) {
+ var _outputFile = output;
+ var _tmpDir = tmpDir;
+
+ if (lang) {
+ _outputFile = _outputFile.slice(0, -path.extname(_outputFile).length)+"_"+lang+path.extname(_outputFile);
+ _tmpDir = path.join(_tmpDir, lang);
+ }
+
+ book.logInfo("copy ebook to", output);
+ return fs.copy(
+ path.join(_tmpDir, "index."+options.ebookFormat),
+ _outputFile
+ );
+ };
+
+ // Multi-langs book
+ return Q()
+ .then(function() {
+ if (book.isMultilingual()) {
+ return Q.all(
+ _.map(book.langs, function(lang) {
+ return copyFile(lang.lang);
+ })
+ );
+ } else {
+ return copyFile();
+ }
+ })
+ .then(function() {
+ return fs.remove(tmpDir);
+ });
+ });
+ });
+};
+
// Parse list of plugins
Book.prototype.parsePlugins = function() {
var that = this;