/
jst.js
311 lines (269 loc) · 9.63 KB
/
jst.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
/*
Copyright 2008, mark turansky (www.markturansky.com)
Copyright 2010, Andrew Kelley (superjoesoftware.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
(This is the license from www.json.org and I think it's awesome)
*/
var Jst = (function () {
// private variables:
// all write and writeln functions eval'd by Jst
// concatenate to this internal variable
var html = "";
// private functions
function CharacterStack(str) {
var i;
this.characters = [];
this.peek = function peek() {
return this.characters[this.characters.length - 1];
};
this.pop = function pop() {
return this.characters.pop();
};
this.push = function push(c) {
this.characters.push(c);
};
this.hasMore = function hasMore() {
return this.characters.length > 0;
};
for (i = str.length; i >= 0; i -= 1) {
this.characters.push(str.charAt(i));
}
}
function StringWriter() {
this.str = "";
this.write = function write(s) {
this.str += s;
};
this.toString = function toString() {
return this.str;
};
}
function parseScriptlet(stack) {
var fragment = new StringWriter();
var c; // character
while (stack.hasMore()) {
if (stack.peek() === _public.delimiter) { //possible end delimiter
c = stack.pop();
if (stack.peek() === '>') { //end delimiter
// pop > so that it is not available to main parse loop
stack.pop();
if (stack.peek() === '\n') {
fragment.write(stack.pop());
}
break;
} else {
fragment.write(c);
}
} else {
fragment.write(stack.pop());
}
}
return fragment.toString();
}
function startsWith(str, c) {
return str.charAt(0) === c;
}
function endsWith(str, c) {
return str.charAt(str.length - 1) === c;
}
function quoteRegExp(str) {
return str.replace(/([.?*+\^$\[\]\\(){}\-])/g, "\\$1");
}
function replaceAll(str, a, b) {
return str.replace(new RegExp(quoteRegExp(a), 'g'), b);
}
function appendExpressionFragment(writer, fragment, jstWriter) {
var i,j;
var c;
// check to be sure quotes are on both ends of a string literal
if (startsWith(fragment, "\"") && ! endsWith(fragment, "\"")) {
//some scriptlets end with \n, especially if the script ends the file
if (endsWith(fragment, "\n") && fragment.charAt(fragment.length - 2) === '"') {
//we're ok...
} else {
throw { "message":"'" + fragment + "' is not properly quoted"};
}
}
if (!startsWith(fragment, "\"") && endsWith(fragment, "\"")) {
throw { "message":"'" + fragment + "' is not properly quoted"};
}
// print or println?
if (endsWith(fragment, "\n")) {
writer.write(jstWriter + "ln(");
//strip the newline
fragment = fragment.substring(0, fragment.length - 1);
} else {
writer.write(jstWriter + "(");
}
if (startsWith(fragment, "\"") && endsWith(fragment, "\"")) {
//strip the quotes
fragment = fragment.substring(1, fragment.length - 1);
writer.write("\"");
for (i = 0; i < fragment.length; i += 1) {
c = fragment.charAt(i);
if (c === '"') {
writer.write("\\");
writer.write(c);
}
}
writer.write("\"");
} else {
for (j = 0; j < fragment.length; j += 1) {
writer.write(fragment.charAt(j));
}
}
writer.write(");");
}
function appendTextFragment(writer, fragment) {
var i;
var c;
if (endsWith(fragment, "\n")) {
writer.write("writeln(\"");
} else {
writer.write("write(\"");
}
for (i = 0; i < fragment.length; i += 1) {
c = fragment.charAt(i);
if (c === '"') {
writer.write("\\");
}
// we took care of the line break with print vs. println
if (c != '\n' && c != '\r') {
writer.write(c);
}
}
writer.write("\");");
}
function parseExpression(stack) {
var fragment = new StringWriter();
var c;
while (stack.hasMore()) {
if (stack.peek() === _public.delimiter) { //possible end delimiter
c = stack.pop();
if (stack.peek() === '>') { //end delimiter
//pop > so that it is not available to main parse loop
stack.pop();
if (stack.peek() === '\n') {
fragment.write(stack.pop());
}
break;
} else {
fragment.write(_public.delimiter);
}
} else {
fragment.write(stack.pop());
}
}
return fragment.toString();
}
function parseText(stack) {
var fragment = new StringWriter();
var c,d;
while (stack.hasMore()) {
if (stack.peek() === '<') { //possible delimiter
c = stack.pop();
if (stack.peek() === _public.delimiter) { // delimiter!
// push c onto the stack to be used in main parse loop
stack.push(c);
break;
} else {
fragment.write(c);
}
} else {
d = stack.pop();
fragment.write(d);
if (d === '\n') { //done with this fragment. println it.
break;
}
}
}
return fragment.toString();
}
function safeWrite(s) {
s = s.toString();
s = replaceAll(s, '&', '&');
s = replaceAll(s, '"', '"');
s = replaceAll(s, '<', '<');
s = replaceAll(s, '>', '>');
html += s;
}
function safeWriteln(s) {
safeWrite(s + "\n");
}
function write(s) {
html += s;
}
function writeln(s) {
write(s + "\n");
}
// public methods:
var _public = {};
// change this if you want to use something other than percent sign
_public.delimiter = '%';
// pre-compile a template for quicker rendering. save the return value and
// pass it to evaluate.
_public.compile = function (src) {
var stack = new CharacterStack(src);
var writer = new StringWriter();
var c;
var fragment;
while (stack.hasMore()) {
if (stack.peek() === '<') { //possible delimiter
c = stack.pop();
if (stack.peek() === _public.delimiter) {
c = stack.pop();
if (stack.peek() === "=") {
// expression, escape all html
stack.pop();
fragment = parseExpression(stack);
appendExpressionFragment(writer, fragment, "safeWrite");
} else if (stack.peek() === "+") {
// expression, don't escape html
stack.pop();
fragment = parseExpression(stack);
appendExpressionFragment(writer, fragment, "write");
} else {
fragment = parseScriptlet(stack);
writer.write(fragment);
}
} else { //not a delimiter
stack.push(c);
fragment = parseText(stack);
appendTextFragment(writer, fragment);
}
} else {
fragment = parseText(stack);
appendTextFragment(writer, fragment);
}
}
return writer.toString();
};
// evaluate a pre-compiled script. recommended approach
_public.evaluate = function (script, args) {
with(args) {
html = "";
eval(script);
return html;
}
};
// if you're lazy, you can use this
_public.evaluateSingleShot = function (src, args) {
return _public.evaluate(_public.compile(src), args);
};
return _public;
})();