-
Notifications
You must be signed in to change notification settings - Fork 10
/
priest.js
executable file
·65 lines (60 loc) · 2.06 KB
/
priest.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
53
54
55
56
57
58
59
60
61
62
63
64
65
(function(module){
var DOMParser = require('xmldom').DOMParser;
var fs = require('fs');
var node = require('./xml2instructions');
var builder = require('./code_generator');
module.exports = function(templates_files, as_module, drop_spaces, classes_namespace, no_modify_regex){
if(!(templates_files instanceof Array)){
templates_files = [templates_files];
}
if (typeof classes_namespace !== 'string'){
classes_namespace = '';
}
if(typeof no_modify_regex === 'undefined'){
no_modify_regex = /js/;
}
var parser = new DOMParser();
var templates = {};
function collector(ins, source, arg1, arg2, arg3) {
if(! templates.hasOwnProperty(source)) {
templates[source] = [];
}
if(ins === 'node') { //Nodes go up
templates[source].unshift([ins, arg1, arg2]);
} else { // Other down
templates[source].push([ins, arg1, arg2, arg3]);
}
}
templates_files.forEach(function(val, index, array) {
var _ = val.split('.');
_.pop();
_ = _.join('.');
if (_.indexOf('/') >= 0) {
_ = _.split('/');
} else {
_ = _.split('\\');
}
var name = _.pop();
var template_string = fs.readFileSync(val, {encoding: 'utf8'});
if(drop_spaces) {
template_string = template_string.replace(/>\s+</g, '><');
}
node(name, 'root', parser.parseFromString(template_string), collector);
});
var templates_code = [];
for(var k in templates) {
if(templates[k].length > 1) //Ignore stop instruction
templates_code.push('"' + k +'"' + ': function(pool){' + builder(templates[k], classes_namespace, no_modify_regex) + '}');
}
if(as_module){
return ['var temple_utils = require("temple-wat");',
'module.exports = {' + templates_code.join(',') + '};'
].join("\n");
} else {
return '(function(window){' +
'var templates_list = {' + templates_code.join(',') + '};' +
'window.templates = temple_utils.pool(templates_list);' +
'})(window);';
}
};
})(module);