Repository navigation
Expand file tree
/
Copy pathendskin.js
More file actions
262 lines (225 loc) · 7.39 KB
/
Copy pathendskin.js
File metadata and controls
262 lines (225 loc) · 7.39 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
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
let RootPath = '';
const fs = require('fs');
const path = require('path');
module.exports.setRoot = function(p) {
RootPath = p;
};
module.exports.create = function(f) {
return new EndSkin(f);
};
exports.cache = {};
function EndSkin(tmpId) {
if (!tmpId) {
throw new Error('EndSkin: no template name passed');
return;
}
let codeBlocks = [];
this.templateReader = function(f,cb) {
let file = path.resolve(RootPath, f);
return fs.readFileSync(file,'utf-8');
};
this.getTemplateString = function(id) {
let s = this.templateReader(id), ms;
if (!s) {
throw new Error('EndSkin: template "'+id+'" not found');
return;
}
if (ms = s.match(/\{include (.*?)\}/gi)) {
for(let i=0,m;m=ms[i]; i++) {
let _ms = m.match(/\{include (.*?)\}/i);
s = s.replace(m,this.getTemplateString(_ms[1]));
}
}
if (ms = s.match(/\<\!\-\-\s*include (.*?)\s*\-\-\>/gi)) {
for(let i=0,m;m=ms[i]; i++) {
let _ms = m.match(/\<\!\-\-\s*include (.*?)\s*\-\-\>/i);
s = s.replace(m,this.getTemplateString(_ms[1]));
}
}
return s;
}
this.data = {};
this.vars = {};
this.assign = function(key,val) {
if (key && val) {
this.data[key] = val;
} else if (typeof key == 'object') {
for(let k in key) this.data[k] = key[k];
}
return this;
};
this.compile = function(page) {
let ms = null,i=0,m,_ms;
if (ms = page.match(/\{(\$[a-zA-Z\_][a-zA-Z0-9\_\.\[\]\'\"]*)\}/ig)) {
for(i=0; m=ms[i]; i++) {
let _ms = m.match(/\{(\$[a-zA-Z\_][a-zA-Z0-9\_\.\[\]\'\"]*)\}/i);
let code = 'output.push('+this._replace_var_name(_ms[1])+');';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
}
}
let foreachBegins = 0;
if (ms = page.match(/\{(foreach\([^}]+\))\}/ig)) {
for(i=0; m=ms[i]; i++) {
if (_ms = m.match(/\{foreach\(\s*([a-z0-9\.\_\$]+)\s+as\s+([a-z0-9\_\$]+)\)\}/i)) {
let variable = this._replace_var_name(_ms[1]);
let item = _ms[2].replace(/^\$/,'');
let code = '(function()\n{\nvar __var = '+variable+';\nfor(var __key in __var)\n{\nthis.data[\"'+item+'\"] = __var[__key]; \n';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
continue;
} else {
if (_ms = m.match(/\{foreach\(\s*([a-z0-9\.\_\$]+)\s+as\s+([a-z0-9\_\$]+)\s*\=>\s*([a-z0-9\_\$]+)\)\}/i)) {
let variable = this._replace_var_name(_ms[1]);
let _k_item = _ms[2].replace(/^\$/,'');
let _v_item = _ms[3].replace(/^\$/,'');
let code = '(function()\n{\nvar __var = '+variable+';\nfor(var __key in __var)\n{\nthis.data[\"'+_k_item+'\"] = __key;\nthis.data[\"'+_v_item+'\"] = __var[__key]; \n';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
continue;
}
}
foreachBegins++;
}
}
let ifBegins = 0;
if (ms = page.match(/\{(else\s*)?if\(.*?\)\}/ig)) {
for(i=0; m=ms[i]; i++) {
let _ms = m.match(/\{if\((.*?)\)\}/i);
if (_ms) {
let cond = this._replace_var_name(_ms[1]);
let code = 'if ('+cond+')\n{\n';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
ifBegins++;
} else if (_ms = m.match(/\{else\s*if\((.*?)\)\}/i)) {
let cond = this._replace_var_name(_ms[1]);
let code = '}\nelse if ('+cond+')\n{\n';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
}
}
}
let ifEnds = 0;
if (ms = page.match(/\{\/if\}/ig)) {
for(i=0; m=ms[i]; i++) {
let code = '}\n';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
ifEnds++;
}
}
let foreachEnds = 0;
if (ms = page.match(/\{\/foreach\}/ig)) {
for(i=0; m=ms[i]; i++) {
let code = '}\n}).call(this);\n';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
foreachEnds++;
}
}
if (ms = page.match(/\{else\}/ig)) {
for(i=0; m=ms[i]; i++) {
let code = '}\nelse\n{\n';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
}
}
if (ms = page.match(/\{[a-z0-9\$\.\_]+\([^\}]*\)\}/ig)) {
for(i=0; m=ms[i]; i++) {
let _ms = m.match(/\{([a-z0-9\$\.\_]+)\(([^\}]*)\)\}/i);
let code = 'output.push('+_ms[1]+'('+this._replace_var_name(_ms[2])+'));';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
}
}
if (ms = page.match(/<\%\=([\s\S]*?)\%>/ig)) {
for(i=0; m=ms[i]; i++) {
let _ms = m.match(/<\%\=([\s\S]*?)\%>/i);
let code = _ms[1].replace(/^\s+|\s+$/g,'').replace(/\;$/,'');
code = 'output.push('+code+');';
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
}
}
if (ms = page.match(/<\%([\s\S]*?)\%>/ig)) {
for(i=0; m=ms[i]; i++) {
let _ms = m.match(/<\%([\s\S]*?)\%>/i);
let code = _ms[1];
codeBlocks.push(code);
page = page.replace(m,'{{{{EndSkin.codeblock['+(codeBlocks.length-1)+']}}}}');
}
}
let arr = page.split(/\{\{\{\{EndSkin\.codeblock\[\d+\]\}\}\}\}/);
ms = page.match(/\{\{\{\{EndSkin\.codeblock\[(\d+)\]\}\}\}\}/g);
let codes = ['var output = [];'];
for(let key in this.vars) {
codes.push(`var ${key}=this.vars.${key};`);
}
for(i=0;i<arr.length-1; i++) {
codes.push('output.push('+JSON.stringify(arr[i])+');\n');
let _ms = ms[i].match(/\{\{\{\{EndSkin\.codeblock\[(\d+)\]\}\}\}\}/);
codes.push(codeBlocks[parseInt(_ms[1])]+'\n');
}
codes.push('output.push('+JSON.stringify(arr.pop())+');\n');
codes.push("return output.join('');");
try {
return new Function(codes.join(''));
} catch(e) {
let err = [];
if (foreachBegins > foreachEnds) err.push('missing '+(foreachBegins - foreachEnds)+' {/foreach}');
if (foreachBegins < foreachEnds) err.push('too much {/foreach}');
if (ifBegins > ifEnds) err.push('missing '+(ifBegins - ifEnds)+' {/if}');
if (ifBegins < ifEnds) err.push('too much {/if}');
let re = 'EndSkin Compile Error: \nview file:'+tmpId+'\n'+e.toString() + ';\n' + err.join(';\n');
throw new Error(re);
return re;
}
}
this._replace_var_name = function(s) {
s = s.replace(/\$[a-z\_0-9\.]+/ig,function(s) {
return 'this.show_val(\"' + s.replace('$','') + '\")';
});
return s;
}
this.show_val = function(s) {
let arr = s.split('.');
let val = this.data;
for(let i=0;i<arr.length;i++) {
let key = arr[i];
if (val[key] == undefined) return '';
val = val[key];
}
if (typeof val == 'function') val = val();
return val;
}
let s = this.getTemplateString(tmpId);
if (!s) return;
let cached_func = null;
this.html = function() {
if (!cached_func) cached_func = this.compile(s);
if (typeof cached_func == 'function') {
let output = '';
try {
output = cached_func.call(this);
} catch (err) {
let raw = cached_func.toString();
err.raw = raw;
if (err.stack) {
let ms = err.stack.match(/at.+anonymous\>\:(\d+)\:/);
let line = parseInt(ms[1] || 0, 10);
if (line > 0) {
let err2 = new Error(err.message + ' in ' + tmpId + ' near ' + raw.split(/\n/)[line-1]);
err2.raw = raw.split(/\n/)[line-1];
throw err2;
}
}
throw err;
}
return output;
}
else
return cached_func.toString();
};
return this;
};