This repository was archived by the owner on Nov 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
70 lines (59 loc) · 2.22 KB
/
index.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
/*
* fis-parser-template
* https://github.com/yanhaijing/fis-parser-template
*/
'use strict';
var template = require('template_js');
module.exports = function(content, file, conf){
template.config(conf);
var global = conf.global || 'template';
var compress = conf.compress || false;
// 获取编译源码
var code = template.__compile(content).toString();
code = '"use strict";var __code__ = "";' + code + ';return __code__';
// __##1##__ 单引号 js语句中的单引号
// __##2##__ 转义单引号 html语句中的双引号
// __##3##__ 双引号 js语句中的双引号
// __##4##__ 转义双引号 html语句中的双引号
code = code
.replace(/\\\'/g, "__##2##__")
.replace(/\\\"/g, '__##4##__')
.replace(/\'/g, '__##1##__')
.replace(/\"/g, '__##3##__')
// \r 针对windows换行符(cr lf) \n 针对unix换行符(lf)
.replace(/[\n\r]/g, '');
var render = function render(data) {
'use strict';
var keyArr = [], valArr = [];
data = data || {};
data.__encodeHTML__ = window['__global__'].__encodeHTML;
for(var key in data) {
keyArr.push('"' + key + '"');
valArr.push(data[key]);
}
var source = 'new Function(' + keyArr.join(',') + ', "__placeholder__")';
try {
var fn = eval(source);
var html = fn.apply(null, valArr);
} catch (e) {
e.name = 'RenderError';
e.tpl = '__tpl__';
window['__global__'].__handelError(e);
return 'template.js error';
}
__compress__
return html;
}
var source = render.toString();
source = source.replace('__compress__', compress ? 'html = window["__global__"].__compress(html);' : '');
source = source.replace('__placeholder__', code);
source = source.replace('__tpl__', file.id);
source = source.replace(/__global__/g, global);
// 将引号替换回来,不要问我为什么知道,试出来的
source = source
.replace(/__##1##__/g, "\\'")
.replace(/__##3##__/g, '\\\\"')
.replace(/__##2##__/g, "\\\\\\'")
.replace(/__##4##__/g, '\\\\\\\\\\\\\\"');
return source;
};