summaryrefslogtreecommitdiffstats
path: root/lib/output/json.js
blob: a77b45a46068fecc5308bad4003bbd35dda49c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var util = require('util');
var ConrefsLoader = require('./conrefs');
var gitbook = require('../gitbook');

function JSONOutput() {
    ConrefsLoader.apply(this, arguments);
}
util.inherits(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 = {
            gitbook: {
                version: gitbook.version
            },
            path: page.path,
            sections: page.content
        };

        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;