diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-13 23:53:34 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-13 23:53:34 +0200 |
commit | 3c7cae6262fdbc2dd3c9944661097d7cee60d9ce (patch) | |
tree | 07feb14f5f2d2ff90805a1cb929173c6c81fb5ff /lib/generate/index.js | |
parent | 555c0d63ee905f9a258cccf164b58b1e1e1bd950 (diff) | |
download | gitbook-3c7cae6262fdbc2dd3c9944661097d7cee60d9ce.zip gitbook-3c7cae6262fdbc2dd3c9944661097d7cee60d9ce.tar.gz gitbook-3c7cae6262fdbc2dd3c9944661097d7cee60d9ce.tar.bz2 |
Move more build logic to the core generation module
Diffstat (limited to 'lib/generate/index.js')
-rw-r--r-- | lib/generate/index.js | 75 |
1 files changed, 71 insertions, 4 deletions
diff --git a/lib/generate/index.js b/lib/generate/index.js index 4804548..ba0e340 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -1,8 +1,8 @@ var Q = require("q"); var _ = require("lodash"); - var path = require("path"); var swig = require('swig'); +var tmp = require('tmp'); var fs = require("./fs"); var parse = require("../parse"); @@ -15,6 +15,10 @@ var generators = { "json": require("./json") }; +/* + * Use a specific generator to convert a gitbook to a site/pdf/ebook/ + * output is always a folder + */ var generate = function(options) { var generator = null; var files; @@ -39,14 +43,16 @@ var generate = function(options) { theme: path.resolve(__dirname, '../../theme') }); - if (!options.title || !options.input || !options.output) { - return Q.reject(new Error("Need options: title, input, output")); + if (!options.title || !options.input) { + return Q.reject(new Error("Need options: title, input")); } if (!generators[options.generator]) { return Q.reject(new Error("Invalid generator (availables are: "+_.keys(generators).join(", "))); } + options.output = options.output || path.join(options.input, "_book"); + // Clean output folder return fs.remove(options.output) @@ -142,7 +148,68 @@ var generate = function(options) { }); }; +/* + * Extract files from generate output in a temporary folder + */ +var generateFile = function(options) { + options = _.defaults(options || {}, { + input: null, + output: null, + extension: null + }); + console.log(options); + + Q.nfcall(tmp.dir) + .then(function(tmpDir) { + return generate( + _.extend({}, + options, + { + output: tmpDir + }) + ) + .then(function(_options) { + var ext = options.extension; + var outputFile = options.output || path.resolve(options.input, "book."+ext); + + var copyFile = function(lang) { + var _outputFile = outputFile; + var _tmpDir = tmpDir; + + if (lang) { + _outputFile = _outputFile.slice(0, -path.extname(_outputFile).length)+"_"+lang+path.extname(_outputFile); + _tmpDir = path.join(_tmpDir, lang); + } + + console.log("generating", _outputFile); + return fs.copy( + path.join(_tmpDir, "index."+ext), + _outputFile + ); + }; + + // Multi-langs book + return Q() + .then(function() { + if (_options.langsSummary) { + return Q.all( + _.map(_options.langsSummary.list, function(lang) { + return copyFile(lang.lang); + }) + ); + } else { + return copyFile(); + } + }) + .then(function() { + return fs.remove(tmpDir); + }); + }); + }) +}; + module.exports = { generators: generators, - folder: generate + folder: generate, + file: generateFile }; |