summaryrefslogtreecommitdiffstats
path: root/lib/generators/site.js
blob: c8b9e91b47421846d6c7155b6fdde729f134597f (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var util = require("util");
var path = require("path");
var Q = require("q");
var _ = require("lodash");
var nunjucks = require("nunjucks");

var fs = require("../utils/fs");
var BaseGenerator = require("../generator");
var links = require("../utils/links");
var pageUtil = require("../utils/page");

var Generator = function() {
    BaseGenerator.apply(this, arguments);

    // revision
    this.revision = new Date();

    // Style to integrates i nthe output
    this.styles = ["website"];

    // base folder for templates
    this.templatesRoot = path.resolve(__dirname, "../../theme/templates/website")
};
util.inherits(Generator, BaseGenerator);

// Prepare the genertor
Generator.prototype.prepare = function() {
    var that = this;

    return BaseGenerator.prototype.prepare.apply(this)
    .then(function() {
        return that.prepareStyles();
    })
    .then(function() {
        return that.prepareTemplates();
    });
};

// Prepare all styles
Generator.prototype.prepareStyles = function() {
    var that = this;
    this.styles = _.chain(this.styles)
        .map(function(style) {
            var stylePath = that.options.styles[style];
            if (fs.existsSync(path.resolve(that.book.root, stylePath))) {
                return stylePath;
            }
            return null;
        })
        .compact()
        .value();
};

// Prepare template engine
Generator.prototype.prepareTemplates = function() {
	this.pageTemplate = this.plugins.template("site:page") || path.resolve(this.templatesRoot, 'page.html');
    this.langsTemplate = this.plugins.template("site:langs") || path.resolve(this.templatesRoot, 'langs.html');
    this.glossaryTemplate = this.plugins.template("site:glossary") || path.resolve(this.templatesRoot, 'templates/website/glossary.html');

    var folders = _.chain(
    	[
    		this.pageTemplate, this.langsTemplate, this.glossaryTemplate
	    ])
    	.map(path.dirname)
    	.uniq()
    	.value();

    console.log("templates folders", folders)

	this.env = new nunjucks.Environment(
		new nunjucks.FileSystemLoader(folders),
		{
			autoescape: true
		}
	);

	return Q();
};

// 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 output = links.changeExtension(page.path, ".html");
    output = path.join(that.options.output, output);

    return that.normalizePage(page)
    .then(function() {
        return that._writeTemplate(that.pageTemplate, {

        }, output);
    });
};

// Write the index for langs
Generator.prototype.langsIndex = function(langs) {

};


// Convert a page into a normalized data set
Generator.prototype.normalizePage  = function(page) {
    var that = this;

    var _callHook = function(name) {
        return that.callHook(name, page)
        .then(function(_page) {
            page = _page;
            return page;
        });
    };

    return Q()
    .then(function() {
        return _callHook("page");
    })
    .then(function() {
        return page;
    });
};

// Generate a template
Generator.prototype._writeTemplate = function(tpl, options, output, interpolate) {
    var that = this;

    interpolate = interpolate || _.identity;
    return Q()
    .then(function(sections) {
    	return that.env.render(
    		tpl,
    		_.extend({
	            styles: that.styles,

	            revision: that.revision,

	            title: that.options.title,
	            description: that.options.description,

	            glossary: that.options.glossary,

	            summary: that.options.summary,
	            allNavigation: that.options.navigation,

	            plugins: that.plugins,
	            pluginsConfig: JSON.stringify(that.options.pluginsConfig),
	            htmlSnippet: _.partialRight(that.plugins.html, that, options),

	            options: that.options
	        }, options)
	    );
    })
    .then(interpolate)
    .then(function(html) {
        return fs.writeFile(
            output,
            html
        );
    });
};

module.exports = Generator;