-
Notifications
You must be signed in to change notification settings - Fork 1
/
tpl.js
269 lines (255 loc) · 6.75 KB
/
tpl.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var fs = require('fs');
var items = {};
var literal = {};
var cwd = __dirname;
/**
* Assigns a value to a key that will be replaced in the template
* @param {string} key
* @param {object} value
* @returns {bool}
*/
exports.assign = function(key, value){
value = typeof value !== "undefined" ? value : "";
items[key] = value;
return true;
};
/**
* Sets the current working directory
* @param {string} cd
* @returns {undefined}
*/
exports.setcwd = function(cd){
cwd = cd;
};
/**
* Gets a template and outputs the result
* @param {string} filename
* @returns {string}
*/
exports.display = function(filename){
process.stdout.write(this.fetch(filename));
};
/**
* Gets and returns the template.
* @param {type} filename
* @returns {string}
*/
exports.fetch = function(filename){
var tpl = fs.readFileSync(getPath(filename), {encoding: 'utf8'});
tpl = getIncludes(tpl);
tpl = removeLiterals(tpl);
tpl = replaceForeach(tpl);
tpl = replaceIf(tpl);
tpl = replaceVars(tpl);
tpl = addLiterals(tpl);
return tpl;
};
/**
* Remove the litterals from the template and save them to add back later
* @param {string} tpl
* @returns {string}
*/
removeLiterals = function(tpl){
var reg = /\{literal\}([\s\S]+?)\{\/literal\}/ig;
var matches = tpl.match(reg);
var n = 0;
tpl = tpl.replace(reg, function(){
return "__literal_" + (n++) + "__";
});
n = 0;
for(var i in matches){
literal[n] = matches[i].replace(/\{\/?literal\}/ig, "");
}
return tpl;
};
/**
* Add the literals back into the template
* @param {string} tpl
* @returns {string}
*/
addLiterals = function(tpl){
for(var i in literal){
tpl = tpl.replace("__literal_" + i + "__", literal[i]);
}
return tpl;
};
getIncludes = function(tpl){
var reg = /\{include\s+?['"](.+?)['"]\}/ig;
var matches = tpl.match(reg);
var n = 0;
var includes = 0;
tpl = tpl.replace(reg, function(){
includes++;
return "__include_" + (n++) + "__";
});
n = 0;
for(var i in matches){
var filename = /['"](.+?)['"]/ig.exec(matches[i]);
if(filename[1]){
var fname = replaceVars(filename[1], false);
var file = fs.readFileSync(getPath(fname), {encoding: 'utf8'});
tpl = tpl.replace("__include_" + n + "__", file);
}
n++;
}
if(includes > 0){
tpl = getIncludes(tpl);
}
return tpl;
};
/**
* Replaces placeholders.
* @param {type} tpl
* @param {boolean} hasBraces
* @returns {string}
*/
replaceVars = function(tpl, hasBraces){
hasBraces = typeof hasBraces === "undefined" ? true : hasBraces;
var reg = hasBraces ? /\{\$(.+?)\}/ig : /\$(\S+)?/ig;
var matches = tpl.match(reg);
var n = 0;
tpl = tpl.replace(reg, function(){
return "__var_" + (n++) + "__";
});
n = 0;
for(var i in matches){
var clean = matches[i].replace(/\{|\}|\$/g, "");
var key = clean.split(".")[0];
var value = "";
if(items[key]){
value = getValue(items[key], clean, items[key]);
}
tpl = tpl.replace("__var_" + n + "__", value);
n++;
}
return tpl;
};
/**
* Replaces if statements
* @param {string} tpl
* @returns {string}
*/
replaceIf = function(tpl){
var reg = /\{if(.+?)\}([\s\S]+?)\{\/if\}/ig;
var matches = tpl.match(reg);
var n = 0;
tpl = tpl.replace(reg, function(){
return "__if_block_" + (n++) + "__";
});
n = 0;
// Loop through each block
for(var i in matches){
var str = matches[i];
var results = [];
str.match(/\{[^{}]+/g).forEach(function(a, b){
results[b] = [a.replace(/(?:if|else|elseif|\/if|\{)\s?/ig, '')];
results[b][0] === '' && results[b].splice(0, 1);
});
str.match(/[^{}]+\{/g).forEach(function(a, b){
results[b].push(a.substring(0, a.length - 1).trim());
});
results.pop();
var value = "";
for(var j in results){
var item = results[j];
if(item.length > 1){
var test = replaceVars(item[0], false);
if(eval(test)){
value = item[1];
break;
}
}
if(item.length === 1){
value = item[0];
break;
}
}
tpl = tpl.replace("__if_block_" + n + "__", value);
n++;
}
return tpl;
};
/**
* Replaces loops
* @param {string} tpl
* @returns {string}
*/
replaceForeach = function(tpl){
var reg = /\{for\s+\$(\w+?)\s+in\s+\$(\w+?)\}([\s\S]+?)\{\/for\}/ig;
var matches = tpl.match(reg);
var n = 0;
tpl = tpl.replace(reg, function(){
return "__for_block_" + (n++) + "__";
});
n = 0;
// Loop through each block
for(var i in matches){
var block = matches[i];
var blocks = /\{for\s+\$(\w+?)\s+in\s+\$(\w+?)\}([\s\S]+?)\{\/for\}/ig;
var data = blocks.exec(block);
var blockkey = data[1];
var blockobj = data[2];
var blocktpl = data[3];
var newblock = "";
// Replace each object in the block
for(var j in items[blockobj]){
var tmpblocktpl = blocktpl;
var arritems = tmpblocktpl.match(/\{\$(.+?)\}/g);
var itemsblock = tmpblocktpl;
for(var k in arritems){
var clean = arritems[k].replace("{$", "").replace("}", "");
var value = getValue(items[blockobj][j], clean, j);
itemsblock = itemsblock.replace(arritems[k], value);
}
newblock += itemsblock;
}
tpl = tpl.replace("__for_block_" + n + "__", newblock);
n++;
}
return tpl;
};
/**
*
* @param {object} o
* @param {string} s
* @param {string} d
* @returns {obejct}
*/
getValue = function(o, s, d){
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
var a = s.split('.');
a.shift();
if(a.length === 0){
return d;
}
for(var i = 0, n = a.length; i < n; ++i){
var k = a[i];
if(k in o){
o = o[k];
}else{
return;
}
}
return o;
};
/**
* Resolves a path to full path based on the cwd.
* Examples:
* ./mytpl.tpl = /cwd/mytpl.tpl
* /mytpl.tpl = /mytpl.tpl
* @param {type} file
* @todo Handle paths that contain "../"
* @returns {String}
*/
getPath = function(file){
var firstChar = file.charAt(0);
var secondChar = file.charAt(1);
if(firstChar !== "/" && firstChar !== "."){
return cwd + "/" + file;
}else if(firstChar === "." && secondChar === "/"){
return cwd + "/" + file.substr(2);
}else{
return file;
}
};