-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
52 lines (43 loc) · 1.3 KB
/
parser.js
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
/*
Turns mustache.js template files into web pages using provided data.
*/
var fs = require('fs');
var mustache = require("mustache");
var builtins={
"head" : "components/head.html",
"header" : "components/header.html",
"footer" : "components/footer.html"
};
var readTemplateFile=(filename, callback)=>{
fs.readFile("templates/"+filename, {encoding: 'utf-8'}, function (err,data) {
if (err) {
console.log(err);
callback(null);
}
callback(data);
});
};
var parseFile=(filename, view, callback)=>{
fs.readFile("templates/"+filename, {encoding: 'utf-8'}, function (err,data) {
if (err) {
console.log(err);
callback(null);
}
callback(mustache.render(data, view));
});
};
var parseBuiltins=(view)=>{
var parsedBuiltins=new Map();
var result={};
Object.keys(builtins).forEach((key)=>result[key]=mustache.render(builtins[key], view));
return result;
}
exports.run=(filename, view, callback)=>{
parseFile(filename, Object.assign(parseBuiltins(view), view), callback);
};
// get builtins from files
Object.keys(builtins).forEach((key)=>{
readTemplateFile(builtins[key], (result)=>{
builtins[key]=result;
});
});