diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-19 12:06:04 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-19 12:06:04 +0100 |
commit | 60993d3123ef81f72e028dd496f26d3bbba0eec0 (patch) | |
tree | 0282d581d0469eb7e0dde645ff08985dc0736968 /lib | |
parent | 39b2aaf4898921fc845ffa165d038fa58404548d (diff) | |
download | gitbook-60993d3123ef81f72e028dd496f26d3bbba0eec0.zip gitbook-60993d3123ef81f72e028dd496f26d3bbba0eec0.tar.gz gitbook-60993d3123ef81f72e028dd496f26d3bbba0eec0.tar.bz2 |
Parse langs and glossary
Diffstat (limited to 'lib')
-rw-r--r-- | lib/book.js | 72 | ||||
-rw-r--r-- | lib/configuration.js | 16 |
2 files changed, 77 insertions, 11 deletions
diff --git a/lib/book.js b/lib/book.js index 9c489f1..50ef252 100644 --- a/lib/book.js +++ b/lib/book.js @@ -6,12 +6,12 @@ var fs = require("./utils/fs"); var Configuration = require("./configuration"); var parser = require("./parser"); -var Book = function(root) { +var Book = function(root, options) { // Root folder of the book this.root = root; // Configuration - this.config = new Configuration(this); + this.config = new Configuration(this, options); Object.defineProperty(this, "options", { get: function () { return this.config.options; @@ -19,20 +19,68 @@ var Book = function(root) { }); // Summary - this.summary = []; + this.summary = {}; + + // Glossary + this.glossary = {}; + + // Langs + this.langs = {}; }; // Initialize and parse the book: config, summary, glossary Book.prototype.init = function() { var that = this; + var multilingal = false; return this.config.load() + .then(function() { + return that.parseLangs() + .then(function() { + multilingal = that.langs.list.length > 0; + + // Sub-books that inherit from the current book configuration + that.books = _.map(that.langs.list, function(lang) { + return new Book( + path.join(that.root, lang.path), + _.extend({}, that.options, { + 'lang': lang.lang + }) + ) + }); + }); + }) + .then(function() { + if (multilingal) return; + return that.parseSummary(); + }) + .then(function() { + if (multilingal) return; + return that.parseGlossary(); }) .thenResolve(this); }; +// Parse langs +Book.prototype.parseLangs = function() { + var that = this; + + return that.findFile("LANGS") + .then(function(langs) { + if (!langs) return {}; + + return that.readFile(langs.path) + .then(function(content) { + return langs.parser.langs(content); + }); + }) + .then(function(langs) { + that.langs = langs; + }); +}; + // Parse summary Book.prototype.parseSummary = function() { var that = this; @@ -51,6 +99,24 @@ Book.prototype.parseSummary = function() { }); }; +// Parse glossary +Book.prototype.parseGlossary = function() { + var that = this; + + return that.findFile("GLOSSARY") + .then(function(glossary) { + if (!glossary) return {}; + + return that.readFile(glossary.path) + .then(function(content) { + return glossary.parser.glossary(content); + }); + }) + .then(function(glossary) { + that.glossary = glossaryy; + }); +}; + // Find file that can be parsed with a specific filename Book.prototype.findFile = function(filename) { var that = this; diff --git a/lib/configuration.js b/lib/configuration.js index c5c1ba2..82bf010 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -6,7 +6,7 @@ var fs = require("./utils/fs"); var Configuration = function(book, options) { this.book = book; - this.options = _.defaults(options || {}, Configuration.DEFAULT); + this.options = _.extend({}, Configuration.DEFAULT, options || {}); }; // Read and parse the configuration @@ -16,7 +16,7 @@ Configuration.prototype.load = function() { return Q() .then(function() { try { - var _config = require(path.resolve(that.book.root, "book")); + var _config = require(path.resolve(that.book.root, that.options.)); that.options = _.merge( that.options, _.omit(_config, 'input', 'configFile', 'defaultsPlugins', 'generator') @@ -30,16 +30,16 @@ Configuration.prototype.load = function() { // Default configuration Configuration.DEFAULT = { + // Options that can't be extend + "configFile": "book", + "generator": "site", + "extension": null, + // Book metadats (somes are extracted from the README by default) "title": null, "description": null, "isbn": null, - - // For ebook format, the extension to use for generation (default is detected from output extension) - // "epub", "pdf", "mobi" - // Caution: it overrides the value from the command line - // It's not advised this option in the book.json - "extension": null, + "lang": "en", // Plugins list, can contain "-name" for removing default plugins "plugins": [], |