summaryrefslogtreecommitdiffstats
path: root/lib/generate/template.js
blob: 8f631907ccd480e6acf8128dbe35986bcf45c059 (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
var swig = require('swig');
var path = require('path');
var _ = require("lodash");

var parse = require("../parse");

var fs = require('./fs');

// return a templeter for page
var initTemplate = function(options) {
    var tpl = swig.compileFile(path.resolve(__dirname, '../../templates/page.html'));

    options = _.defaults(options || {}, {
        // Base folder for input
        root: "./",

        // Base folder for output
        output: "./",

        // Locals for templates
        locals: {}
    });

    return function(input, output, local) {
        input = path.join(options.root, input);
        output = path.join(options.output, output);

        // Read markdown file
        return fs.readFile(input, "utf-8")

        // Parse sections
        .then(function(markdown) {
            return parse.page(markdown);
        })

        //Calcul template
        .then(function(sections) {
            return tpl(
                _.extend(local || {}, options.locals, {
                    _input: input,
                    content: sections
                })
            );
        })

        // Write html
        .then(function(html) {
            return fs.writeFile(output, html);
        })
    }
};

module.exports = initTemplate;