diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 12:11:43 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 12:11:43 -0700 |
commit | d19906d5b2638cd55c1b65eeab3327d7533ebca1 (patch) | |
tree | 9ccc41b8c2c74d96de3c8b60a498160fba29e63f /lib/generate/index.js | |
parent | fb26b634ce2f332bcc3a8764009bdc65a2245f35 (diff) | |
download | gitbook-d19906d5b2638cd55c1b65eeab3327d7533ebca1.zip gitbook-d19906d5b2638cd55c1b65eeab3327d7533ebca1.tar.gz gitbook-d19906d5b2638cd55c1b65eeab3327d7533ebca1.tar.bz2 |
Add base for multi-languages book
Diffstat (limited to 'lib/generate/index.js')
-rw-r--r-- | lib/generate/index.js | 116 |
1 files changed, 71 insertions, 45 deletions
diff --git a/lib/generate/index.js b/lib/generate/index.js index 38e7503..b46c96b 100644 --- a/lib/generate/index.js +++ b/lib/generate/index.js @@ -2,6 +2,7 @@ var Q = require("q"); var _ = require("lodash"); var path = require("path"); +var swig = require('swig'); var fs = require("./fs"); var parse = require("../parse"); @@ -54,60 +55,85 @@ var generate = function(options) { // List all files in the repository .then(function() { - return fs.list(options.input); + return fs.list(options.input) + .then(function(_files) { + files = _files; + }) }) - // Check repository is valid - .then(function(_files) { - files = _files; - - if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) { + // Detect multi-languages book + .then(function() { + if (_.contains(files, "README.md") && _.contains(files, "LANGS.md")) { + // Multi-languages book + return fs.readFile(path.join(options.input, "LANGS.md"), "utf-8") + + // Generate sub-books + .then(function(_langsSummary) { + options.langsSummary = parse.summary(_langsSummary); + + // Generated a book for each valid entry + return Q.all( + _.chain(langsSummary.chapters) + .filter(function(entry) { + return entry.path != null; + }) + .map(function(entry) { + return generate(_.extend({}, options, { + input: path.join(options.input, entry.path), + output: path.join(options.output, entry.path) + })); + }) + .value() + ); + }); + } else if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) { + // Invalid book return Q.reject(new Error("Invalid gitbook repository, need SUMMARY.md and README.md")); - } - }) + } else { + // Generate the book + return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8") - // Get summary - .then(function() { - return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8") - .then(function(_summary) { - options.summary = parse.summary(_summary); + // Get summary + .then(function(_summary) { + options.summary = parse.summary(_summary); - // Parse navigation - options.navigation = parse.navigation(options.summary); - }); - }) + // Parse navigation + options.navigation = parse.navigation(options.summary); + }) - // Create the generator - .then(function() { - generator = new generators[options.generator](options); - }) + // Create the generator + .then(function() { + generator = new generators[options.generator](options); + }) - // Copy file and replace markdown file - .then(function() { - return Q.all( - _.chain(files) - .map(function(file) { - if (!file) return; - - if (file[file.length -1] == "/") { - return Q(generator.transferFolder(file)); - } else if (path.extname(file) == ".md" && options.navigation[file] != null) { - return fs.readFile(path.join(options.input, file), "utf-8") - .then(function(content) { - return Q(generator.convertFile(content, file)); - }); - } else { - return Q(generator.transferFile(file)); - } + // Copy file and replace markdown file + .then(function() { + return Q.all( + _.chain(files) + .map(function(file) { + if (!file) return; + + if (file[file.length -1] == "/") { + return Q(generator.transferFolder(file)); + } else if (path.extname(file) == ".md" && options.navigation[file] != null) { + return fs.readFile(path.join(options.input, file), "utf-8") + .then(function(content) { + return Q(generator.convertFile(content, file)); + }); + } else { + return Q(generator.transferFile(file)); + } + }) + .value() + ); }) - .value() - ); - }) - // Finish gneration - .then(function() { - return generator.finish(); - }); + // Finish gneration + .then(function() { + return generator.finish(); + }); + } + }) }; module.exports = { |