diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-19 17:25:16 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-19 17:25:16 +0100 |
commit | be4d2679a4daf885a470b5f89e8e6488b0c7a820 (patch) | |
tree | 4fb8929a39254a275ed75d7bc04786fa7338cc12 /lib | |
parent | 70189a5ee2da4ae2237178e6e6630b3355857f56 (diff) | |
download | gitbook-be4d2679a4daf885a470b5f89e8e6488b0c7a820.zip gitbook-be4d2679a4daf885a470b5f89e8e6488b0c7a820.tar.gz gitbook-be4d2679a4daf885a470b5f89e8e6488b0c7a820.tar.bz2 |
Add base test for generation
Diffstat (limited to 'lib')
-rw-r--r-- | lib/book.js | 13 | ||||
-rw-r--r-- | lib/generator.js | 11 | ||||
-rw-r--r-- | lib/generators/index.js | 4 | ||||
-rw-r--r-- | lib/generators/json.js | 76 |
4 files changed, 93 insertions, 11 deletions
diff --git a/lib/book.js b/lib/book.js index 062e02c..73f3847 100644 --- a/lib/book.js +++ b/lib/book.js @@ -6,7 +6,9 @@ var fs = require("./utils/fs"); var Configuration = require("./configuration"); var TemplateEngine = require("./template"); var Plugin = require("./plugin"); + var parsers = require("./parsers"); +var generators = require("./generators"); var Book = function(root, options, parent) { // Root folder of the book @@ -106,14 +108,21 @@ Book.prototype.parse = function() { // Generate the output Book.prototype.generate = function() { - var that = this; + var that = this, generator; + if (that.isMultilingual()) return that.generateMultiLingual(); return Q() // Clean output folder .then(function() { - return fs.clean(that.options.output); + return fs.remove(that.options.output); + }) + + // Create generator + .then(function() { + Generator = generators[that.options.generator]; + if (!Generator) throw "Generator '"+that.options.generator+"' doesn't exist"; }); }; diff --git a/lib/generator.js b/lib/generator.js index ef64018..d0db3b6 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -1,7 +1,7 @@ var _ = require("lodash"); var path = require("path"); var Q = require("q"); -var fs = require("./fs"); +var fs = require("./utils/fs"); var BaseGenerator = function(book, options) { this.options = options; @@ -19,17 +19,10 @@ BaseGenerator.prototype.load = function() { return this.loadPlugins(); }; -BaseGenerator.prototype.loadPlugins = function() { +BaseGenerator.prototype.preparePlugins = function() { var that = this; - return Plugin.fromList(this.options.plugins, this.options.input, this, { - assetsBase: this.pluginAssetsBase - }) - .then(function(_plugins) { - that.plugins = _plugins; - return that.callHook("init"); - }); }; BaseGenerator.prototype.convertFile = function(content, input) { diff --git a/lib/generators/index.js b/lib/generators/index.js new file mode 100644 index 0000000..74efb60 --- /dev/null +++ b/lib/generators/index.js @@ -0,0 +1,4 @@ + +module.exports = { + json: require("./json") +}; diff --git a/lib/generators/json.js b/lib/generators/json.js new file mode 100644 index 0000000..4632a17 --- /dev/null +++ b/lib/generators/json.js @@ -0,0 +1,76 @@ +var util = require("util"); +var path = require("path"); +var Q = require("q"); +var _ = require("lodash"); + +var fs = require("../utils/fs"); +var BaseGenerator = require("../generator"); + + +var Generator = function() { + BaseGenerator.apply(this, arguments); +}; +util.inherits(Generator, BaseGenerator); + +Generator.prototype.transferFile = function(input) { + // ignore +}; + +Generator.prototype.convertFile = function(content, input) { + var that = this; + var json = { + progress: parse.progress(this.options.navigation, input) + }; + + return Q() + .then(function() { + return parse.page(content, { + dir: path.dirname(input) || '/' + }); + }) + .then(function(parsed) { + json.lexed = parsed.lexed; + json.sections = parsed.sections; + }) + .then(function() { + return fs.writeFile( + path.join(that.options.output, input.replace(".md", ".json")), + JSON.stringify(json, null, 4) + ); + }); +}; + +// Generate languages index +// Contains the first languages readme and langs infos +Generator.prototype.langsIndex = function(langs) { + var that = this; + + if (langs.list.length == 0) return Q.reject("Need at least one language"); + + var mainLang = _.first(langs.list).lang; + console.log("Main language is", mainLang); + + return Q() + .then(function() { + return fs.readFile( + path.join(that.options.output, mainLang, "README.json") + ); + }) + .then(function(content) { + var json = JSON.parse(content); + _.extend(json, { + langs: langs.list + }); + + return fs.writeFile( + path.join(that.options.output, "README.json"), + JSON.stringify(json, null, 4) + ); + }); +}; + +Generator.prototype.finish = function() { + // ignore +}; + +module.exports = Generator; |