summaryrefslogtreecommitdiffstats
path: root/lib/generators/json.js
blob: a1202ad231741fa2a22f5d96315d48f428a683ab (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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) { };
Generator.prototype.finish = function() { };

// Convert an input file
Generator.prototype.writeParsedFile = function(page) {
    var that = this;
    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)
    );
};

// Generate languages index
// Contains the first languages readme and langs infos
Generator.prototype.langsIndex = function(langs) {
    var that = this;

    if (langs.length == 0) return Q.reject("Need at least one language");

    var mainLang = _.first(langs).lang;
    var readme = links.changeExtension(that.book.readmeFile, ".json");

    return Q()
    .then(function() {
        // Read readme from main language
        return fs.readFile(
            path.join(that.options.output, mainLang, 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;