-
Notifications
You must be signed in to change notification settings - Fork 0
/
piecewise.js
311 lines (246 loc) · 7.28 KB
/
piecewise.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"use strict";
var fs = require("fs");
var path = require("path");
var ESCAPE = /^\s*\{\{\s*$/;
var INCLUDE = /^\s*([\w-]+)\s*$/;
var VARIABLE = /^\s*@([\w-]+(?:\.[\w-]+)*)\s*((?:\|\s*[\w-]+\s*)*|\|\s*)$/;
var OPERATOR = /^\s*([\w-]+)\s*([<!]?)\s*@([\w-]+(?:\.[\w-]+)*)\s*$/;
var operators = {
"": "if",
"!": "ifnot",
"<": "repeat"
};
var reservedWords = [
"break", "case", "catch", "continue", "debugger", "default", "delete",
"do", "else", "finally", "for", "function", "if", "in", "instanceof",
"new", "return", "switch", "this", "throw", "try", "typeof", "var",
"void", "while", "with",
"class", "const", "enum", "export", "extends", "import", "super",
"implements", "interface", "let", "package", "private", "protected",
"public", "static", "yield"
];
var filters = {
html: function(text) {
return String(text).replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
},
text: function(text) {
return String(text).replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
},
attr: function(text) {
return String(text).replace(/&/g, "&")
.replace(/"/g, """);
},
url: encodeURIComponent
};
function singleQuotedEscape(string) {
return string.replace(/\\/g, "\\\\")
.replace(/'/g, "\\'")
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
}
function escapePath(root, path) {
return root + path.split(".").map(function(part) {
if (part.indexOf("-") === -1)
return "." + part;
return "['" + part + "']";
}).join("");
}
function filter(expression, filters) {
var code = "";
for (var i = filters.length; i--;)
code += filters[i] + "(";
return code + expression + new Array(filters.length + 1).join(")");
}
var LETTER_I = 105;
var LETTER_Z = 122;
function VariableManager() {
this.used = {
filters: true,
data: true
};
}
VariableManager.prototype.getName = function(name) {
name = name
.replace(/[.-]+$/, "")
.replace(/[.-]+(.)/g, function(_, c) {
return c.toUpperCase();
});
if (/^\d/.test(name) || reservedWords.indexOf(name) !== -1)
name = "_" + name;
while (this.used.hasOwnProperty(name))
name += "_";
this.used[name] = true;
return name;
};
VariableManager.prototype.getIndexName = function() {
var i = LETTER_I;
var name;
while (i <= LETTER_Z && this.used.hasOwnProperty(name = String.fromCharCode(i)))
i++;
if (i > LETTER_Z)
return this.getName("i_");
this.used[name] = true;
return name;
};
function parseFilters(filterList) {
filterList = filterList.trim();
if (!filterList)
return ["filters.html"];
if (filterList === "|")
return [];
return filterList.match(/[\w-]+/g).map(function(f) {
return escapePath("filters", f);
});
}
function lex(template) {
var i;
var last = 0;
var tokens = [];
var errors = [];
while ((i = template.indexOf("{{", last)) !== -1) {
if (i > last)
tokens.push({
type: "text",
value: template.substring(last, i)
});
var start = i + 2;
var end = template.indexOf("}}", start);
if (end === -1) {
errors.push({
index: i,
message: "Unclosed opening braces"
});
break;
}
var expression = template.substring(start, end);
var m;
if (ESCAPE.test(expression))
tokens.push({ type: "escape", value: "{{" });
else if (m = INCLUDE.exec(expression))
tokens.push({ type: "include", name: m[1] });
else if (m = VARIABLE.exec(expression))
tokens.push({
type: "variable",
name: m[1],
filters: parseFilters(m[2])
});
else if (m = OPERATOR.exec(expression))
tokens.push({
type: operators[m[2]],
template: m[1],
variable: m[3]
});
else
errors.push({
index: start,
message: "Unrecognized expression"
});
last = end + 2;
}
if (last < template.length)
tokens.push({
type: "text",
value: template.substring(last)
});
if (errors.length)
throw errors;
return tokens;
}
function compile(load, dataVariable, name, variables, chain, functions) {
var compile_ = function(dataVariable, name) {
if (functions.hasOwnProperty(name))
return "' + " + functions[name] + "(" + dataVariable + ") + '";
return compile(load, dataVariable, name, variables, chain, functions);
};
var template = load(name);
var tokens = lex(template);
var code = "";
if (!functions.hasOwnProperty(name) && chain.indexOf(name) !== -1) {
var functionName = variables.getName(name);
functions[name] = functionName;
var funcVariables = new VariableManager();
funcVariables.used[functionName] = true;
code = "' + (function " + functionName + "(data) { var output = '" + compile(load, "data", name, funcVariables, chain, functions) + "'; return output; })(" + dataVariable + ") + '";
delete functions[name];
return code;
}
chain.push(name);
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
switch (token.type) {
case "text":
case "escape":
code += singleQuotedEscape(token.value);
break;
case "variable":
var filtered = filter(escapePath(dataVariable, token.name), token.filters);
code +=
"' + " + filtered + " + '";
break;
case "include":
code += compile_(dataVariable, token.name);
break;
case "if":
var conditionPath = escapePath(dataVariable, token.variable);
code +=
"';\nif (" + conditionPath + ") {\n" +
"output += '" + compile_(dataVariable, token.template) +
"';\n}\noutput += '";
break;
case "ifnot":
code +=
"';\nif (!" + escapePath(dataVariable, token.variable) + ") {\n" +
"output += '" + compile_(dataVariable, token.template) +
"';\n}\noutput += '";
break;
case "repeat":
var indexName = variables.getIndexName();
var collectionPath = escapePath(dataVariable, token.variable);
var variableName = variables.getName(token.template);
code +=
"';\nfor (var " + indexName + " = 0; " + indexName + " < " + collectionPath + ".length; " + indexName + "++) {\n" +
"var " + variableName + " = " + collectionPath + "[" + indexName + "];\n" +
"output += '" + compile_(variableName, token.template) +
"';\n}\noutput += '";
break;
default:
throw new Error("Unrecognized token type " + token.type);
}
}
return code;
}
function Template(code) {
this.func = new Function("filters, data", "var output = '" + code + "';\nreturn output;");
}
Template.prototype.render = function(data) {
return this.func(filters, data);
};
function DirectoryLoader(root, options) {
this.root = root;
this.ext = (options && options.extension) || ".pwp";
this.cache = {};
}
DirectoryLoader.prototype.read = function(name) {
if (this.cache.hasOwnProperty(name))
return this.cache[name];
return this.cache[name] = fs.readFileSync(path.join(this.root, name + this.ext), "utf8");
};
DirectoryLoader.prototype.load = function(name, override) {
var loader = this;
return new Template(compile(function(name) {
if (override.hasOwnProperty(name))
return override[name];
return loader.read(name);
}, "data", name, new VariableManager(), [], {}));
};
module.exports.filters = filters;
module.exports.compile = compile;
module.exports.Template = Template;
module.exports.DirectoryLoader = DirectoryLoader;