-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejs.js
More file actions
executable file
·132 lines (124 loc) · 2.68 KB
/
ejs.js
File metadata and controls
executable file
·132 lines (124 loc) · 2.68 KB
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
#!rhino
EJS = function (template, opts) { return (this instanceof EJS) ? this.initialize(template, opts) : new EJS(template, opts) };
EJS.prototype = {
initialize : function (template, opts) {
this.template = template;
this.generator = this.compile(template, opts || {});
this.processor = this.generator();
// print(this.processor);
},
run : function (stash) {
return this.processor(stash);
},
compile : function (s, opts) {
s = String(s);
ret = [
'var ret = [];',
'ret.push(""'
];
var m, c, flag;
while (m = s.match(/<%(=*)/)) {
flag = m[1];
ret.push(',', uneval(s.slice(0, m.index)));
s = s.slice(m.index + m[0].length);
m = s.match(/(.*?)%>/);
s = s.slice(m.index + m[0].length);
c = m[1];
switch (flag) {
case "=":
case "==":
ret.push(', (', c,').replace(/[&<>]/g, f)');
break;
case "===":
ret.push(',', c);
break;
default:
ret.push(");", c, "\nret.push(''");
break;
}
}
ret.push(
',', uneval(s), ');',
'return ret.join("");'
);
if (opts.useWith) {
ret.unshift("with (s) {");
ret.push("}");
}
ret.unshift(
'var map = { "&" : "&", "<" : "<" , ">" : ">"}, f = function (m) {return map[m]};',
"return function (s) {"
);
ret.push("}");
return new Function(ret.join(''));
}
};
//---
//var tester = [
// "aaaa<%=foo%>bbbbb<%=bar%>ccc",
// "aaaa<% if (foo) {%>bbbb<%=bar%><%}%>ccc",
//];
//
//for (var i = 0; i < tester.length; i++) {
// var t = EJS(tester[i], {useWith: true});
// print(t.generator);
// print(t.run({foo:"test", bar:"foobar"}));
//}
//
//COUNT = 500;
//
//var t = "aaaa<%=s.foo%>bbbbb<%=s.bar%>ccc";
//var e = EJS(t);
//var f = EJS(t, {useWith:true});
//var m = {foo:"test", bar:"foobar"};
//var b = [
// function compile () {
// EJS(t);
// },
// function processing () {
// e.run(m);
// },
// function processing_with_with () {
// f.run(m);
// },
// function replace () {
// t.replace(/<%=s\.(\w+)%>/g, function (_,a) {
// return m[a];
// });
// }
//];
//
//
//for (var i = 0; i < b.length; i++) {
// var fun = b[i];
// print(fun.name);
// var res = 0;
// for (var j = 0; j < COUNT; j++) {
// var now = (new Date).getTime();
// fun();
// res += (new Date).getTime() - now;
// }
// print(res + "ms / " + (res/COUNT) + "ms");
//};
//importPackage(java.io);
//function _readLines(file) {
// var br = new BufferedReader(
// new InputStreamReader(
// new FileInputStream(file),
// "UTF-8"
// )
// );
// var ret = [];
// while (br.ready()) ret.push(br.readLine());
// return ret;
//}
//var t = new EJS(_readLines(File("template.html")).join("\n"));
//var r = t.run({
// title : "test",
// home : "/",
// entries : [
// ],
//});
//
//print(r);
//print(t.processor);