diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-22 11:00:21 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-22 11:00:21 +0200 |
commit | 4336fdb2414d460ffee68a0cc87c0cb0c85cf56e (patch) | |
tree | 279f711ab98666c892c19a7b9e4073a094f03f98 /lib/output2/json.js | |
parent | 87db7cf1d412fa6fbd18e9a7e4f4755f2c0c5547 (diff) | |
download | gitbook-4336fdb2414d460ffee68a0cc87c0cb0c85cf56e.zip gitbook-4336fdb2414d460ffee68a0cc87c0cb0c85cf56e.tar.gz gitbook-4336fdb2414d460ffee68a0cc87c0cb0c85cf56e.tar.bz2 |
Base
Diffstat (limited to 'lib/output2/json.js')
-rw-r--r-- | lib/output2/json.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/output2/json.js b/lib/output2/json.js new file mode 100644 index 0000000..7061141 --- /dev/null +++ b/lib/output2/json.js @@ -0,0 +1,47 @@ +var conrefsLoader = require('./conrefs'); + +var JSONOutput = conrefsLoader(); + +JSONOutput.prototype.name = 'json'; + +// Don't copy asset on JSON output +JSONOutput.prototype.onAsset = function(filename) {}; + +// Write a page (parsable file) +JSONOutput.prototype.onPage = function(page) { + var that = this; + + // Parse the page + return page.toHTML(this) + + // Write as json + .then(function() { + var json = page.getOutputContext(that); + + // Delete some private properties + delete json.config; + + // Specify JSON output version + json.version = '3'; + + return that.writeFile( + page.withExtension('.json'), + JSON.stringify(json, null, 4) + ); + }); +}; + +// At the end of generation, generate README.json for multilingual books +JSONOutput.prototype.finish = function() { + if (!this.book.isMultilingual()) return; + + // Copy README.json from main book + var mainLanguage = this.book.langs.getDefault().id; + return this.copyFile( + this.resolve(mainLanguage, 'README.json'), + 'README.json' + ); +}; + + +module.exports = JSONOutput; |