diff options
-rw-r--r-- | lib/book.js | 53 | ||||
-rw-r--r-- | lib/configuration.js | 5 | ||||
-rw-r--r-- | lib/index.js | 24 | ||||
-rw-r--r-- | lib/utils/fs.js | 8 | ||||
-rw-r--r-- | lib/utils/git.js | 2 |
5 files changed, 89 insertions, 3 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; diff --git a/lib/configuration.js b/lib/configuration.js index d5406c5..375833b 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -101,6 +101,11 @@ Configuration.prototype.load = function() { }); }; +// Extend the configuration +Configuration.prototype.extend = function(options) { + _.extend(this.options, options); +}; + // Get structure file Configuration.prototype.getStructure = function(name) { return this.options.structure[name].split(".").slice(0, -1).join("."); diff --git a/lib/index.js b/lib/index.js index 44574fd..7d5fb25 100644 --- a/lib/index.js +++ b/lib/index.js @@ -53,6 +53,30 @@ module.exports = { } }, + // Build an ebook and output it + { + name: "pdf [book] [output]", + description: "build a book to pdf", + options: [ + LOG_OPTION + ], + exec: function(args, kwargs) { + var input = args[0] || process.cwd(); + var output = args[1]; + + var book = new Book(input, _.extend({}, { + 'logLevel': Book.LOG_LEVELS[(kwargs.log).toUpperCase()] + })); + + return book.parse() + .then(function() { + return book.generateFile(output, { + ebookFormat: "pdf" + }); + }); + } + }, + // Build and serve a book { name: "serve [book]", diff --git a/lib/utils/fs.js b/lib/utils/fs.js index 51577c6..d81f3d0 100644 --- a/lib/utils/fs.js +++ b/lib/utils/fs.js @@ -64,8 +64,12 @@ var getFiles = function(path) { module.exports = { tmp: { - file: Q.denodeify(tmp.file.bind(tmp)), - dir: Q.denodeify(tmp.dir.bind(tmp)) + file: function() { + return Q.nfcall(tmp.file.bind(tmp)).get(0) + }, + dir: function() { + return Q.nfcall(tmp.dir.bind(tmp)).get(0) + } }, list: getFiles, stat: Q.denodeify(fs.stat), diff --git a/lib/utils/git.js b/lib/utils/git.js index 8b515ec..f6d33bb 100644 --- a/lib/utils/git.js +++ b/lib/utils/git.js @@ -59,7 +59,7 @@ function cloneGitRepo(host, ref) { // Create temporary folder to store git repos .then(function() { if (GIT_TMP) return; - return fs.tmp.dir().get(0) + return fs.tmp.dir() .then(function(_tmp) { GIT_TMP = _tmp; }); |