summaryrefslogtreecommitdiffstats
path: root/lib/cli
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-29 15:27:53 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-29 15:27:53 +0200
commit730298110cea46e0c861e65d0c2c3fd82c0edd18 (patch)
tree60c7992a9c631ad5e713cb2cbea376aceea23375 /lib/cli
parent07a5daefb025868ecf41054713a18d50e01511c0 (diff)
downloadgitbook-730298110cea46e0c861e65d0c2c3fd82c0edd18.zip
gitbook-730298110cea46e0c861e65d0c2c3fd82c0edd18.tar.gz
gitbook-730298110cea46e0c861e65d0c2c3fd82c0edd18.tar.bz2
Add ebook commands pdf/mobi/epub
Diffstat (limited to 'lib/cli')
-rw-r--r--lib/cli/buildEbook.js76
-rw-r--r--lib/cli/index.js6
2 files changed, 81 insertions, 1 deletions
diff --git a/lib/cli/buildEbook.js b/lib/cli/buildEbook.js
new file mode 100644
index 0000000..ce1d836
--- /dev/null
+++ b/lib/cli/buildEbook.js
@@ -0,0 +1,76 @@
+var path = require('path');
+var tmp = require('tmp');
+
+var Promise = require('../utils/promise');
+var fs = require('../utils/fs');
+var Parse = require('../parse');
+var Output = require('../output');
+
+var options = require('./options');
+var getBook = require('./getBook');
+
+
+module.exports = function(format) {
+ return {
+ name: (format + ' [book] [output]'),
+ description: 'build a book into an ebook file',
+ options: [
+ options.log
+ ],
+ exec: function(args, kwargs) {
+ // Output file will be stored in
+ var outputFile = args[1] || ('book.' + format);
+
+ // Create temporary directory
+ var outputFolder = tmp.dirSync().name;
+
+ var book = getBook(args, kwargs);
+ var logger = book.getLogger();
+ var Generator = Output.getGenerator('ebook');
+
+ return Parse.parseBook(book)
+ .then(function(resultBook) {
+ return Output.generate(Generator, resultBook, {
+ root: outputFolder,
+ format: format
+ });
+ })
+
+ // Extract ebook file
+ .then(function(output) {
+ var book = output.getBook();
+ var languages = book.getLanguages();
+
+ if (book.isMultilingual()) {
+ return Promise.ForEach(languages, function(lang) {
+ var langID = lang.getID();
+
+ var langOutputFile = path.join(
+ path.dirname(outputFile),
+ path.basename(outputFile, format) + '_' + langID + format
+ );
+
+ return fs.copy(
+ path.resolve(outputFolder, langID, 'index' + format),
+ langOutputFile
+ );
+ })
+ .thenResolve(languages.getCount());
+ } else {
+ return fs.copy(
+ path.resolve(outputFolder, 'index' + format),
+ outputFile
+ ).thenResolve(1);
+ }
+ })
+
+ // Log end
+ .then(function(count) {
+ logger.info.ok(count + ' file(s) generated');
+
+ logger.debug('cleaning up... ');
+ return logger.debug.promise(fs.rmDir(outputFolder));
+ });
+ }
+ };
+};
diff --git a/lib/cli/index.js b/lib/cli/index.js
index bb65578..f1fca1d 100644
--- a/lib/cli/index.js
+++ b/lib/cli/index.js
@@ -1,8 +1,12 @@
+var buildEbook = require('./buildEbook');
module.exports = [
require('./build'),
require('./serve'),
require('./install'),
require('./parse'),
- require('./init')
+ require('./init'),
+ buildEbook('pdf'),
+ buildEbook('epub'),
+ buildEbook('mobi')
];