-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (128 loc) · 3.63 KB
/
index.js
File metadata and controls
140 lines (128 loc) · 3.63 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
'use strict';
const fs = require('fs');
const path = require('path');
const baseDir = path.join(process.cwd(), 'view');
const ESCAPE_REG = /["&'<>]/;
class RenderClass {
constructor(opt = {}) {
this.opt = Object.assign({
rootName: 'root',
baseDir,
cache: true,
}, opt);
this.cacheKeys = {};
// 不缓存,需要监听文件变化
if (!this.opt.cache) {
console.log('cache: false, 开启view watch:');
fs.watch(this.opt.baseDir, {
recursive: true,
}, (eventType, filename) => {
console.log(`view watch: eventType:${eventType} filename: ${filename}`);
if (filename) {
this.cacheKeys[filename] = null;
}
});
}
}
async include() {
return await this.render(...arguments);
}
async render(file, data) {
file = file.replace(/\//, '');
if (!this.cacheKeys[file] || !this[file]) {
let fileStr = await this.getFileStr(file);
fileStr = fileStr.replace(/\\/g, '\\\\');
// fileStr = fileStr.replace(/\${([^}]+)}/g, function ($1, $2) {
// return `\${this._escape(${$2})\}`;
// });
const funStr = 'const renderFun = async function('+ this.opt.rootName +') {var scope = {}, _ = this, e = this.escape; return `' + fileStr + '`}; return renderFun;';
let renderFun = new Function(funStr);
renderFun = renderFun();
this[file] = renderFun;
this.cacheKeys[file] = true;
console.log(`更新renderFun: ${file}`);
}
const re = this[file](data);
return re;
}
renderString(fileStr, data) {
if (!this.cacheKeys[fileStr] || !this[fileStr]) {
fileStr = fileStr.replace(/\\/g, '\\\\');
// fileStr = fileStr.replace(/\${([^}]+)}/g, function ($1, $2) {
// return `\${this._escape(${$2})\}`;
// });
const funStr = 'const renderFun = async function('+ this.opt.rootName +') {var scope = {}, _ = this, e = this.escape; return `' + fileStr + '`}; return renderFun;';
let renderFun = new Function(funStr);
renderFun = renderFun();
this[fileStr] = renderFun;
this.cacheKeys[fileStr] = true;
console.log(`更新renderFun: ${fileStr}`);
}
const re = this[fileStr](data);
return new Promise((resolve, reject) => {
resolve(re);
});
}
async getFileStr(file) {
file = path.join(this.opt.baseDir, file);
return new Promise((resolve, reject) => {
// 自运行返回Promise
fs.readFile(file, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data.toString('utf-8'));
}
});
});
}
// 编码 HTML 内容
escape(content) {
var html = '' + content;
var regexResult = ESCAPE_REG.exec(html);
if (!regexResult) {
return content;
}
var result = '';
var i = void 0,
lastIndex = void 0,
char = void 0;
for (i = regexResult.index, lastIndex = 0; i < html.length; i++) {
switch (html.charCodeAt(i)) {
case 34:
char = '"';
break;
case 38:
char = '&';
break;
case 39:
char = ''';
break;
case 60:
char = '<';
break;
case 62:
char = '>';
break;
default:
continue;
}
if (lastIndex !== i) {
result += html.substring(lastIndex, i);
}
lastIndex = i + 1;
result += char;
}
if (lastIndex !== i) {
return result + html.substring(lastIndex, i);
} else {
return result;
}
}
}
class SubRender extends RenderClass {
constructor(opt) {
super(opt)
}
}
module.exports = RenderClass;