diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 13:44:14 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 13:44:14 -0700 |
commit | 014b2f9c6902235216fee390078ece0049344c0b (patch) | |
tree | 4b7a40abe74cce7b961f8a3bc63da9d4f044b315 /lib/generate/index.js | |
parent | fb26b634ce2f332bcc3a8764009bdc65a2245f35 (diff) | |
parent | 65cb23feabb7ae311f18c5f50978686d202dafec (diff) | |
download | gitbook-014b2f9c6902235216fee390078ece0049344c0b.zip gitbook-014b2f9c6902235216fee390078ece0049344c0b.tar.gz gitbook-014b2f9c6902235216fee390078ece0049344c0b.tar.bz2 |
Merge pull request #35 from GitbookIO/feature/multilangs
Fix #34: Feature/multilangs
Diffstat (limited to 'lib/generate/index.js')
-rw-r--r-- | lib/generate/index.js | 114 |
1 files changed, 70 insertions, 44 deletions
diff --git a/lib/generate/index.js b/lib/generate/index.js index 38e7503..5f44633 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,27 +55,10 @@ var generate = function(options) { // List all files in the repository .then(function() { - return fs.list(options.input); - }) - - // Check repository is valid - .then(function(_files) { - files = _files; - - if (!_.contains(files, "SUMMARY.md") || !_.contains(files, "README.md")) { - return Q.reject(new Error("Invalid gitbook repository, need SUMMARY.md and README.md")); - } - }) - - // Get summary - .then(function() { - return fs.readFile(path.join(options.input, "SUMMARY.md"), "utf-8") - .then(function(_summary) { - options.summary = parse.summary(_summary); - - // Parse navigation - options.navigation = parse.navigation(options.summary); - }); + return fs.list(options.input) + .then(function(_files) { + files = _files; + }) }) // Create the generator @@ -82,32 +66,74 @@ var generate = function(options) { generator = new generators[options.generator](options); }) - // Copy file and replace markdown file + // Detect multi-languages book .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)); - } + 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.langs(_langsSummary); + + // Generated a book for each valid entry + return Q.all( + _.map(options.langsSummary.list, function(entry) { + return generate(_.extend({}, options, { + input: path.join(options.input, entry.path), + output: path.join(options.output, entry.path) + })); + }) + ); }) - .value() - ); - }) - // Finish gneration - .then(function() { - return generator.finish(); - }); + // Generate languages index + .then(function() { + return generator.langsIndex(options.langsSummary); + }); + } 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(_summary) { + options.summary = parse.summary(_summary); + + // Parse navigation + options.navigation = parse.navigation(options.summary); + }) + + // 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() + ); + }) + + // Finish gneration + .then(function() { + return generator.finish(); + }); + } + }) }; module.exports = { |