-
Notifications
You must be signed in to change notification settings - Fork 7
/
nTenjin.js
159 lines (133 loc) · 3.96 KB
/
nTenjin.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
/*
* $Rev: 39 $
* $Release: 0.0.0 $
* $Copyright$
* License: MIT License
*/
/**
* namespace
*/
var nTenjin = {
version: '0.1.1',
_escape_table: { '&': '&', '<': '<', '>': '>', '"': '"' },
_escape_func: function(m) { return nTenjin._escape_table[m]; },
escapeXml: function(s) {
//if (s == null) return '';
return typeof(s) != 'string' ? s : s.replace(/[&<>"]/g, nTenjin._escape_func); //"
//return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); //"
},
escapeXml2: function(s) {
if (s == null) return '';
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); //"
},
strip: function(s) {
if (! s) return s;
//return s.replace(/^\s+|\s+$/g, '');
return s.replace(/^\s+/, '').replace(/\s+$/, '');
},
_end: undefined // dummy property to escape strict warning (not legal in ECMA-262)
};
delete(nTenjin._end);
// 因为用 new Function 创建的函数,其[[scope]]的作用域链只包含全局对象,所以这里定义为全局变量
_nTenjinEscapeXml = nTenjin.escapeXml;
/**
* Template class
*/
nTenjin.Template = function(properties) {
if (properties) {
var p = properties;
if (p['escaefunc']) this.escapefunc = p['escapefunc'];
}
};
nTenjin.Template.prototype = {
escapefunc: '_nTenjinEscapeXml',
convert: function(input) {
var buf = [];
buf.push("var _buf='';");
this.parseStatements(buf, input);
buf.push("return _buf;");
buf = buf.join('').split("_buf+='';").join('')
.split("var _buf='';_buf+=").join('var _buf=');
try {
return this.render = new Function('it', buf);
//eval('this.render = function(it){' + buf + '};');
//return this.render;
} catch (e) {
if (typeof console !== 'undefined') console.log("Could not create a template function: " + buf);
throw e;
}
},
parseStatements: function(buf, input) {
var regexp = /<\?js(\s(.|\n)*?) ?\?>/mg;
var pos = 0;
var m;
while ((m = regexp.exec(input)) != null) {
var stmt = m[1];
var text = input.substring(pos, m.index);
pos = m.index + m[0].length;
//
if (text) this.parseExpressions(buf, text);
if (stmt) buf.push(stmt);
}
var rest = pos == 0 ? input : input.substring(pos);
this.parseExpressions(buf, rest);
},
parseExpressions: function(buf, input) {
if (! input) return;
//buf.push(" _buf+=");
var regexp = /([$#])\{(.*?)\}/g;
var pos = 0;
var m;
while ((m = regexp.exec(input)) != null) {
var text = input.substring(pos, m.index);
var s = m[0];
pos = m.index + s.length;
this.addText(buf, text);
buf.push(";_buf+=");
var indicator = m[1];
var expr = m[2];
if (indicator == "$")
buf.push(this.escapefunc, "(", expr, ");");
else
buf.push(expr, ";");
}
var rest = pos == 0 ? input : input.substring(pos);
rest ? this.addText(buf, rest, true) : buf.push('""');
buf.push(";");
if (input.charAt(input.length-1) == "\n")
buf.push("\n");
},
addText: function(buf, text, encode_newline) {
if (! text) return;
var s = text.replace(/[\'\\]/g, '\\$&').replace(/\n/g, '\\n\\\n');
buf.push("_buf+='", s, "'");
},
_end: undefined // dummy property to escape strict warning (not legal in ECMA-262)
};
delete(nTenjin.Template.prototype._end);
/*
* convenient function
*/
nTenjin.render = function(template_str, context) {
var template = new nTenjin.Template();
template.convert(template_str);
var output = template.render(context);
return output;
};
/**
* compile str to Function
*
* @param {String} str, template string
* @return {Function}
* @api public
*/
nTenjin.compile = function(str, options) {
var tpl = new nTenjin.Template();
return tpl.convert(str);
};
/*
* node.js
*/
if (typeof module !== 'undefined' && module.exports) {
module.exports = nTenjin;
}