-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjstpl.1.3.js
313 lines (296 loc) · 10.1 KB
/
jstpl.1.3.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
312
313
//- JSTpl
/*
* JavaScript-based HTML Template Engine , born with GWA2
* @Xenxin@ufqi.com, Wadelau@hotmail.com
* @Since Nov 11, 2018
* @Ver 1.1
* Pros:
1) Runtime in client-side, reduce computing render in server-side;
2) Language-independent, not-bound with backend scripts/languages;
3) Totally-isolated between MVC, data transfer with JSON;
4) Full-support with built-in logic and customerized JavaScript functions;
5) No more tags language to be learned;
...
*/
"use strict";
//- JSTpl config, modify at your desire
if(!window){ window = {}; }
window.JSTplDefault = {
"TplVarTag":"$", //- variables from response starting with this, e.g. $pageTitle
"JsonDataId":"jstpljsondata", //- an html element id which holds server response json data
"LogTag":"JSTpl", //- inner usage
"ParseTag":"__JSTPL__", //- inner usage
"IsDebug": false, //- verbose output in console
};
/*
* JSTpl runtime
* !!! Please do not edit any code below this line !!!
*/
//- parent's configs will override defaults
if(window.JSTpl){
for(var $k in window.JSTpl){
window.JSTplDefault[$k] = window.JSTpl[$k];
}
}
window.JSTpl = window.JSTplDefault;
//- MAGIC START
(function(window){ //- anonymous JSTpl main func bgn
if(!window.JSTpl){ console.log("JSTpl undefined. 201812011128"); return ''; }
const parseTag = window.JSTpl.ParseTag; const tplVarTag = window.JSTpl.TplVarTag;
const jsonDataId = window.JSTpl.JsonDataId; const logTag = window.JSTpl.LogTag+" ";
const isDebug = window.JSTpl.IsDebug;
const includeScriptTagBgn = 'JSTpl_INCLUDE_SCRIPT_BGN';
const includeScriptTagEnd = 'JSTpl_INCLUDE_SCRIPT_END';
//- inner methods
//- append embedded scripts into current runtime
var _appendScript = function(myCode) {
var s = document.createElement('script');
s.type = 'text/javascript';
var code = myCode;
try{
s.appendChild(document.createTextNode(code));
document.body.appendChild(s);
}
catch (e) {
s.text = code;
document.body.appendChild(s);
}
if(isDebug){
console.log('_appendScript: '+myCode+' has been appended.');
}
};
//- server response in json, parse into global variables starting with tplVarTag
var timeCostBgn = (new Date()).getTime();
var pageJsonElement = document.getElementById(jsonDataId);
var tplData = {};
if(pageJsonElement){
var tplDataStr = pageJsonElement.innerText;
tplData = JSON.parse(tplDataStr);
if(!tplData['copyright_year']){ tplData['copyright_year'] = (new Date()).getYear(); }
//- parse json keys as global variables
for(var $k in tplData){
//console.log("k:"+$k+" v:"+tplData[$k]);
if($k != null && $k != ''){
var $v = tplData[$k];
$k = tplVarTag + $k;
if(window){ window[$k] = $v; }
else{ console.log('window undefined error. 201812011122.'); }
}
}
//- hide raw data
//pageJsonElement.style.height = '0px';
//pageJsonElement.style.visibility = 'hidden'; // hide json data element
}
else{ console.log(logTag+'pageJsonElement:['+jsonDataId+'] has error. 201812010927'); }
//- parse all tag blocks
//console.log("aft parse copyright_year:"+$copyright_year);
var renderTemplate = function(window, document, tplHTML){
//var re = /\{([^\}]+)\}/gm, match, tplRaw, tplObject;
var tplRe = /\{((for|if|while|else|switch|break|case|\$|\/|var|let)[^}]*)\}/gm;
var match, tplRaw, tplObject;
tplObject = document.body || document;
if(!tplHTML){
tplRaw = tplObject.innerHTML;
var tmppos = tplRaw.indexOf(' id="'+jsonDataId+'"');
if(tmppos == -1){
tmppos = tplRaw.indexOf(' id="'+jsonDataId+'"');
}
if(tmppos > -1){
tplRaw = tplRaw.substring(0, tmppos); // discard json data
tmppos = tplRaw.lastIndexOf('<');
tplRaw = tplRaw.substring(0, tmppos); // remove json data tag
}
else{
console.log(logTag + "jsonDataId:["+jsonDataId+"] has error.201812011028");
}
}
else{
tplRaw = tplHTML;
}
//console.log(tplRaw);
tplRaw = tplRaw.replace(/[\n|\r]/g, '');
var tplSegment = []; var lastpos = 0;
var staticStr, ipos, matchStr, exprStr;
//- parse include parts
var includeRe = /\{include [file|content]*="([^\}]*?)"\}/gm;
var segi, segStr, tplRawNew, tmpCont;
lastpos = 0; tplRawNew = tplRaw;
while(match = includeRe.exec(tplRaw)){
//console.log(match);
//ipos = match.index;
//staticStr = tplRaw.substring(lastpos, ipos);
matchStr = match[0]; exprStr = match[1];
tmpCont = (new Function("return "+exprStr+";")).apply();
tmpCont = tmpCont.replace(/[\n|\r]/g, '');
if(tmpCont.indexOf('<script') > -1){
tmpCont = includeScriptTagBgn + tmpCont + includeScriptTagEnd;
}
tplRawNew = tplRawNew.replace(matchStr, tmpCont);
//console.log("updt tmpCont:"+tmpCont);
//lastpos = ipos + matchStr.length;
}
tplRaw = tplRawNew;
//console.log(tplRaw);
//- parse original scripts
var scriptRe = /<script[^>]*>(.*?)<\/script>/gm;
var hasScript = false; var isIncludeScript = false;
while(match = scriptRe.exec(tplRaw)){
//console.log(match);
ipos = match.index;
staticStr = tplRaw.substring(lastpos, ipos);
if(staticStr.indexOf(includeScriptTagBgn) > -1){
isIncludeScript = true;
staticStr = staticStr.replace(includeScriptTagBgn, '');
//console.log('includeScript bgn:');
}
matchStr = match[0];
exprStr = match[1];
if(isIncludeScript){
_appendScript(exprStr);
}
if(staticStr.indexOf(includeScriptTagEnd) > -1){
//console.log("includeScript end:");
isIncludeScript = false;
staticStr = staticStr.replace(includeScriptTagEnd, '');
}
tplSegment.push(parseTag + staticStr);
tplSegment.push(exprStr);
lastpos = ipos + matchStr.length;
hasScript = true;
}
if(hasScript){
staticStr = tplRaw.substring(lastpos); // remainings
if(staticStr.indexOf(includeScriptTagEnd) > -1){
//console.log("includeScript end:");
isIncludeScript = false;
staticStr = staticStr.replace(includeScriptTagEnd, '');
}
tplSegment.push(parseTag + staticStr);
}
else{
if(isDebug){ console.log(logTag + "no scripts:"+tplRaw); }
tplSegment.push(parseTag + tplRaw);
}
//console.log(tplSegment);
//- main body for tags interpret
var tpl2code = "var tpl2js = [];\n"; segStr = ''; segi = 0;
var blockBeginRe, tmpmatch, needSemiComma, containsDot, containsBracket;
for(segi in tplSegment){ //- loop over segments besides originals
segStr = tplSegment[segi];
if(segStr.indexOf(parseTag) == -1){ //- original scripts
tpl2code += "\n" + segStr + "\n";
}
else{
segStr = segStr.replace(parseTag, '');
lastpos = 0;
//- parse all tpl tags
while(match = tplRe.exec(segStr)){
ipos = match.index;
staticStr = segStr.substring(lastpos, ipos);
//console.log("bfr staticStr:"+staticStr);
staticStr = staticStr.replace(/"/g, '\\"');
if(staticStr != ''){
tpl2code += "\ttpl2js.push(\""+staticStr+"\");\n";
}
//console.log(match);
matchStr = match[0]; containsBracket = false;
exprStr = match[1]; containsDot = false;
if(exprStr.indexOf(tplVarTag) == 0){
//- functions call
if(exprStr.match(/(\+|\-|\*|\/|=|~|!|\()/gm)){
if(exprStr.indexOf('(') > -1){ containsBracket = true;}
if(exprStr.indexOf('.') > -1){ containsDot = true; }
if(containsBracket && !containsDot){
//- private, aFunc($a)
exprStr = exprStr.substring(1);
tpl2code += "\ttpl2js.push("+exprStr+");\n";
}
else if(containsDot){
//- built-in, $a.substring(0, 5)
tpl2code += "\ttpl2js.push("+exprStr+");\n";
}
else{
tpl2code += "" + exprStr + ";\n";
}
}
else{
tpl2code += "\ttpl2js.push("+exprStr+");\n";
}
}
else if(exprStr.match(/.*({|;|}).*/gm)
&& exprStr.indexOf('t;') == -1){ // exceptions, > <
tpl2code += "\ttpl2js.push(\""+matchStr+"\");\n";
if(isDebug){
console.log(logTag + "illegal tpl sentence:["+matchStr
+"] for containing {, }, ;. skip... 201812012201.");
}
}
else{
needSemiComma = true;
if(exprStr.match(/(^( )?(if|for|while|switch|case|break))(.*)?/g)){
blockBeginRe = /^(if|for|while|switch)(.*)/gm; // why re-init?
if(tmpmatch = blockBeginRe.exec(exprStr)){
//console.log(tmpmatch);
if(tmpmatch[2].indexOf('(') == -1){
if(isDebug){
console.log(logTag+"illegal tpl sentence:["
+exprStr+"] but compatible.");
}
exprStr = tmpmatch[1] + '(' + tmpmatch[2] + ')';
}
}
else{
console.log("not blockBegin? "+exprStr+"");
}
exprStr += '{'; needSemiComma = false;
}
else if(exprStr.indexOf('else') == 0){
exprStr = '}\n' + exprStr + '{'; needSemiComma = false;
}
else if(exprStr.indexOf('/') == 0){
exprStr = '}'; needSemiComma = false;
}
if(exprStr.indexOf('t;') > -1){
exprStr = exprStr.replace('>', '>');
exprStr = exprStr.replace('<', '<');
}
if(needSemiComma){ exprStr += ';'; }
tpl2code += "\n" + exprStr + "\n";
}
lastpos = ipos + matchStr.length;
}
//- last static part
staticStr = segStr.substring(lastpos);
staticStr = staticStr.replace(/"/g, '\\"');
if(staticStr != ''){
tpl2code += "\ttpl2js.push(\""+staticStr+"\");\n";
}
}
} // end of segment loop
//- append to tpl2code
tpl2code += "return tpl2js.join('');\n";
tpl2code = "try{"+tpl2code+"\n}\ncatch(e1635){ console.log(\""
+logTag+"code exec failed.\"); console.log(e1635); return ''+e1635; }\n";
//- merge data
var tplParse = '';
//try{
if(isDebug){ console.log(logTag + "tpl2code:"+tpl2code); }
//tplParse = (function(){ return (new Function(tpl2code).apply(window)); }).apply();
tplParse = (new Function(tpl2code)).apply(window);
//}
//catch(e0930){
//console.log(logTag + "merge data failed.");
//console.log(e0930);
//console.log(logTag + "tpl2code:"+tpl2code);
//}
if(isDebug){ console.log("tplParse:"+tplParse); }
tplObject.innerHTML = tplParse;
};
//- compile
renderTemplate(window, document, null);
if(isDebug){
console.log(logTag + "parse time \
cost: "+(((new Date()).getTime() - timeCostBgn)/1000) + "s");
}
})(window); //- anonymous JSTpl main func end
//- MAGIC COMPLETE