-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtjstpl.1.0.js
351 lines (333 loc) · 11.8 KB
/
gtjstpl.1.0.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//-
//- GTJSTpl
//- --- The template grammar, syntax and its engine ---
/*
* (G)enerally-(T)argeted
* (J)ava(S)cript-based HTML (T)em(pl)ate Engine
* 基于JavaScript通用HTML页面模板引擎
* --- 模板语法, 格式及解析引擎 ---
*
* @Born with GWA2, General Web Application Architecture
* @Xenxin@ufqi.com, Wadelau@hotmail.com
* @Since Oct 10, 2018
* @Ver 1.1
*
*** Philosophy:
* God's return to God, Caesar's return to Caesar;
* the backend runs in background, the frontend is executed in foreground.
* 上帝的归上帝, 凯撒的归凯撒; 后端的归后台, 前端的归前台。
*
*** 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, just JavaScript;
...
*** History:
* Nov 24, 2018, +include with scripts
* Dec 02, 2018, +variables, +functions
* Dec 04, 2018, +tpl2code string to array, +foreach
* Dec 08, 2018, +else if
*/
"use strict"; //- we are serious
//- GTJSTpl configs, modify at your conveniences
if(!window){ window = {}; } //- why this?
window.GTJSTplDefault = {
"TplVarTag": "$", //- variables from response starting with this, e.g. $pageTitle
"JsonDataId": "gtjstpljsondata", //- an html element id which holds server response json data
"LogTag": "GTJSTpl", //- inner usage
"ParseTag": "__JSTPL__", //- inner usage
"IncludeScriptTag": "GTJSTpl_INCLUDE_SCRIPT", //- inner usage
"IsDebug": false, //- verbose output in console
};
/*
* GTJSTpl runtime
* ----------------- !!! Please do not edit any code below this line !!! -----------------
*/
//- parent's configs will override defaults
if(window.GTJSTpl){
for(var $k in window.GTJSTpl){
window.GTJSTplDefault[$k] = window.GTJSTpl[$k];
}
}
window.GTJSTpl = window.GTJSTplDefault;
//- ----------------- MAGIC START -----------------
(function(window){ //- anonymous GTJSTpl main func bgn
//- global object
if(!window.GTJSTpl){
var errMsg="GTJSTpl undefined. 201812011128";
console.log(errMsg); return errMsg;
}
//- constants
const parseTag = window.GTJSTpl.ParseTag; const tplVarTag = window.GTJSTpl.TplVarTag;
const jsonDataId = window.GTJSTpl.JsonDataId; const logTag = window.GTJSTpl.LogTag+" ";
const isDebug = window.GTJSTpl.IsDebug;
const includeScriptTag = window.GTJSTpl.IncludeScriptTag;
const includeScriptTagBgn = includeScriptTag + '_BGN';
const includeScriptTagEnd = includeScriptTag + '_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 it into global variables starting with this tplVarTag
var timeCostBgn = (new Date()).getTime();
var pageJsonElement = document.getElementById(jsonDataId);
var tplData = {};
if(pageJsonElement){
var tplDataStr = pageJsonElement.innerText;
try{
tplData = JSON.parse(tplDataStr);
}
catch(e0939){ console.log(e0939);}
if(!tplData['copyright_year']){ tplData['copyright_year'] = (new Date()).getYear(); }
//- parse json keys as global variables
//- variables starting with tplVarTag, i.e., $ as default
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;
//- collect tpl content
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);
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);
}
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, '');
}
matchStr = match[0];
exprStr = match[1];
if(isIncludeScript){
_appendScript(exprStr);
}
if(staticStr.indexOf(includeScriptTagEnd) > -1){
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){
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 tpl tags interpret
var tpl2code, tpl2codeArr; segStr = ''; segi = 0;
tpl2codeArr = []; tpl2codeArr.push("var tpl2js = [];");
var blockBeginRe, tmpmatch, needSemiComma, containsDot, containsBracket;
var tmpArr, containsEqual, tmpIfPos;
for(segi in tplSegment){ //- loop over segments besides originals
segStr = tplSegment[segi];
if(segStr.indexOf(parseTag) == -1){ //- original scripts
tpl2codeArr.push("\n" + segStr);
}
else{
//- mixed tpl content
segStr = segStr.replace(parseTag, '');
lastpos = 0;
//- parse all tpl tags
while(match = tplRe.exec(segStr)){
ipos = match.index;
staticStr = segStr.substring(lastpos, ipos);
staticStr = staticStr.replace(/"/g, '\\"');
if(staticStr != ''){
tpl2codeArr.push("\ttpl2js.push(\""+staticStr+"\");");
}
//console.log(match);
matchStr = match[0]; containsBracket = false;
exprStr = match[1]; containsDot = false; containsEqual = 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(exprStr.indexOf('=') > -1){ containsEqual = true; }
if(containsBracket && !containsDot && !containsEqual){
//- private, $aFunc($a)
exprStr = exprStr.substring(1);
tpl2codeArr.push("\ttpl2js.push("+exprStr+");");
}
else if(containsDot && !containsEqual){
//- built-in, $a.substring(0, 5)
tpl2codeArr.push("\ttpl2js.push("+exprStr+");");
}
else{
//- variables operations, $a++
tpl2codeArr.push(exprStr + ';');
}
}
else{
//- variables access, $a
tpl2codeArr.push("\ttpl2js.push("+exprStr+");");
}
}
else if(exprStr.match(/.*({|;|}).*/gm)
&& exprStr.indexOf('t;') == -1){ // exceptions, > <
tpl2codeArr.push("\ttpl2js.push(\""+matchStr+"\");");
if(isDebug){
console.log(logTag + "illegal tpl sentence:["+matchStr
+"] for containing {, }, ;. skip... 201812012201.");
}
}
else{ //- directives
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('each ') == 0){ //- foreach
tmpArr = tmpmatch[2].substring(5).split(' as ');
tmpmatch[2] = 'var ' + tmpArr[1] + ' in ' + tmpArr[0];
}
if(tmpmatch[2].indexOf('(') == -1){
if(isDebug){
console.log(logTag+"illegal tpl sentence:["
+exprStr+"] but compatible.");
}
exprStr = tmpmatch[1] + '(' + tmpmatch[2] + ')';
}
}
else{
if(isDebug){ console.log("not blockBegin? "+exprStr+""); }
}
exprStr += '{'; needSemiComma = false;
}
else if(exprStr.indexOf('else') == 0){ //- if branch
tmpIfPos = exprStr.indexOf('if ');
if( tmpIfPos > -1 && exprStr.indexOf('(') < 0){
if(isDebug){
console.log(logTag+"illegal tpl sentence:"+exprStr
+" but compatible.");
}
exprStr = exprStr.substr(0, tmpIfPos+3)
+ '(' + exprStr.substr(tmpIfPos+3) + ')';
}
exprStr = '}\n' + exprStr + '{'; needSemiComma = false;
}
else if(exprStr.indexOf('/') == 0){ //- end of a block
exprStr = '}'; needSemiComma = false;
}
if(exprStr.indexOf('t;') > -1){
exprStr = exprStr.replace('>', '>');
exprStr = exprStr.replace('<', '<');
}
if(needSemiComma){ exprStr += ';'; }
tpl2codeArr.push("\n" + exprStr);
}
lastpos = ipos + matchStr.length;
}
//- last static part
staticStr = segStr.substring(lastpos);
staticStr = staticStr.replace(/"/g, '\\"');
if(staticStr != ''){
tpl2codeArr.push("\ttpl2js.push(\""+staticStr+"\");");
}
}
} // end of loop over tplSegment
//- append to tpl2code
tpl2codeArr.push("return tpl2js.join('');");
tpl2code = tpl2codeArr.join("\n"); tpl2codeArr = null;
tpl2code = "try{" + tpl2code + "\n}\ncatch(e1635){ console.log(\""
+ logTag + "code exec failed.\"); console.log(e1635); "
+ " return ''+JSON.stringify(e1635); }\n";
//- merge data and compile
var tplParse = '';
if(isDebug){ console.log(logTag + "tpl2code:"+tpl2code); }
//tplParse = (function(){ return (new Function(tpl2code).apply(window)); }).apply();
tplParse = (new Function(tpl2code)).apply(window);
if(isDebug){ console.log("tplParse:"+tplParse); }
tplObject.innerHTML = tplParse; tpl2code = null;
};
//- invoke the magic GTJSTpl
renderTemplate(window, document, null);
if(isDebug){
console.log(logTag + "parse time \
cost: "+(((new Date()).getTime() - timeCostBgn)/1000) + "s");
}
})(window); //- anonymous GTJSTpl main func end
//- ----------------- MAGIC COMPLETE -----------------