diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-03-09 10:43:12 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-03-09 10:43:12 +0100 |
commit | 34fc2831e0cf0fed01c71cec28d93472d87f455b (patch) | |
tree | a803cc907c20491ba02863b5d3dd5aedf6bfed10 /lib/generators/json.js | |
parent | e1594cde2c32e4ff48f6c4eff3d3d461743d74e1 (diff) | |
parent | 1bf68a5aa0703b5a1815cfe4ebb731b5fb6ed9d2 (diff) | |
download | gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.zip gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.tar.gz gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.tar.bz2 |
Merge branch 'version/2.0'
Diffstat (limited to 'lib/generators/json.js')
-rw-r--r-- | lib/generators/json.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/generators/json.js b/lib/generators/json.js new file mode 100644 index 0000000..6c9439d --- /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 links = require("../utils/links"); + +var Generator = function() { + BaseGenerator.apply(this, arguments); +}; +util.inherits(Generator, BaseGenerator); + +// Ignore some methods +Generator.prototype.transferFile = function(input) { }; + +// Convert an input file +Generator.prototype.convertFile = function(input) { + var that = this; + + return that.book.parsePage(input) + .then(function(page) { + var json = { + progress: page.progress, + sections: page.sections + }; + + var output = links.changeExtension(page.path, ".json"); + output = path.join(that.options.output, output); + + return fs.writeFile( + output, + JSON.stringify(json, null, 4) + ); + }); +}; + +// Finish generation +Generator.prototype.finish = function() { + return this.writeReadme(); +}; + +// Write README.json +Generator.prototype.writeReadme = function() { + var that = this; + var mainlang, langs; + + return Q() + .then(function() { + langs = that.book.langs; + mainLang = langs.length > 0? _.first(langs).lang : null; + + readme = links.changeExtension(that.book.readmeFile, ".json"); + + // Read readme from main language + return fs.readFile( + mainLang? path.join(that.options.output, mainLang, readme) : path.join(that.options.output, readme) + ); + }) + .then(function(content) { + // Extend it with infos about the languages + var json = JSON.parse(content); + _.extend(json, { + langs: langs + }); + + // Write it as README.json + return fs.writeFile( + path.join(that.options.output, "README.json"), + JSON.stringify(json, null, 4) + ); + }); +}; + +module.exports = Generator; |