art-template 4.13.2 → 4.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -2
- package/README.md +6 -12
- package/index.d.ts +2 -2
- package/lib/template-web.js +2 -2
- package/package.json +3 -3
- package/CHANGELOG.md +0 -216
- package/lib/compile/adapter/caches.js +0 -19
- package/lib/compile/adapter/extend.js +0 -37
- package/lib/compile/adapter/html-minifier.js +0 -29
- package/lib/compile/adapter/include.js +0 -21
- package/lib/compile/adapter/loader.js +0 -22
- package/lib/compile/adapter/onerror.js +0 -13
- package/lib/compile/adapter/resolve-filename.js +0 -36
- package/lib/compile/adapter/rule.art.js +0 -187
- package/lib/compile/adapter/rule.native.js +0 -34
- package/lib/compile/compiler.js +0 -470
- package/lib/compile/defaults.js +0 -95
- package/lib/compile/error.js +0 -60
- package/lib/compile/es-tokenizer.js +0 -26
- package/lib/compile/index.js +0 -136
- package/lib/compile/runtime.js +0 -99
- package/lib/compile/tpl-tokenizer.js +0 -98
- package/lib/defaults.js +0 -3
- package/lib/extension.js +0 -21
- package/lib/index.js +0 -26
- package/lib/precompile.js +0 -242
- package/lib/render.js +0 -16
- package/lib/runtime.js +0 -3
package/lib/compile/defaults.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var runtime = require('./runtime');
|
|
4
|
-
var extend = require('./adapter/extend');
|
|
5
|
-
var include = require('./adapter/include');
|
|
6
|
-
var onerror = require('./adapter/onerror');
|
|
7
|
-
var caches = require('./adapter/caches');
|
|
8
|
-
var loader = require('./adapter/loader');
|
|
9
|
-
var artRule = require('./adapter/rule.art');
|
|
10
|
-
var nativeRule = require('./adapter/rule.native');
|
|
11
|
-
var htmlMinifier = require('./adapter/html-minifier');
|
|
12
|
-
var resolveFilename = require('./adapter/resolve-filename');
|
|
13
|
-
|
|
14
|
-
var detectNode = typeof window === 'undefined';
|
|
15
|
-
|
|
16
|
-
/** 模板编译器默认配置 */
|
|
17
|
-
var settings = {
|
|
18
|
-
// 模板内容。如果没有此字段,则会根据 filename 来加载模板内容
|
|
19
|
-
source: null,
|
|
20
|
-
|
|
21
|
-
// 模板名
|
|
22
|
-
filename: null,
|
|
23
|
-
|
|
24
|
-
// 模板语法规则列表
|
|
25
|
-
rules: [nativeRule, artRule],
|
|
26
|
-
|
|
27
|
-
// 是否开启对模板输出语句自动编码功能。为 false 则关闭编码输出功能
|
|
28
|
-
// escape 可以防范 XSS 攻击
|
|
29
|
-
escape: true,
|
|
30
|
-
|
|
31
|
-
// 启动模板引擎调试模式。如果为 true: {cache:false, minimize:false, compileDebug:true}
|
|
32
|
-
debug: detectNode ? process.env.NODE_ENV !== 'production' : false,
|
|
33
|
-
|
|
34
|
-
// bail 如果为 true,编译错误与运行时错误都会抛出异常
|
|
35
|
-
bail: true,
|
|
36
|
-
|
|
37
|
-
// 是否开启缓存
|
|
38
|
-
cache: true,
|
|
39
|
-
|
|
40
|
-
// 是否开启压缩。它会运行 htmlMinifier,将页面 HTML、CSS、CSS 进行压缩输出
|
|
41
|
-
// 如果模板包含没有闭合的 HTML 标签,请不要打开 minimize,否则可能被 htmlMinifier 修复或过滤
|
|
42
|
-
minimize: true,
|
|
43
|
-
|
|
44
|
-
// 是否编译调试版
|
|
45
|
-
compileDebug: false,
|
|
46
|
-
|
|
47
|
-
// 模板路径转换器
|
|
48
|
-
resolveFilename: resolveFilename,
|
|
49
|
-
|
|
50
|
-
// 子模板编译适配器
|
|
51
|
-
include: include,
|
|
52
|
-
|
|
53
|
-
// HTML 压缩器。仅在 NodeJS 环境下有效
|
|
54
|
-
htmlMinifier: htmlMinifier,
|
|
55
|
-
|
|
56
|
-
// HTML 压缩器配置。参见 https://github.com/kangax/html-minifier
|
|
57
|
-
htmlMinifierOptions: {
|
|
58
|
-
collapseWhitespace: true,
|
|
59
|
-
minifyCSS: true,
|
|
60
|
-
minifyJS: true,
|
|
61
|
-
// 运行时自动合并:rules.map(rule => rule.test)
|
|
62
|
-
ignoreCustomFragments: []
|
|
63
|
-
},
|
|
64
|
-
|
|
65
|
-
// 错误事件。仅在 bail 为 false 时生效
|
|
66
|
-
onerror: onerror,
|
|
67
|
-
|
|
68
|
-
// 模板文件加载器
|
|
69
|
-
loader: loader,
|
|
70
|
-
|
|
71
|
-
// 缓存中心适配器(依赖 filename 字段)
|
|
72
|
-
caches: caches,
|
|
73
|
-
|
|
74
|
-
// 模板根目录。如果 filename 字段不是本地路径,则在 root 查找模板
|
|
75
|
-
root: '/',
|
|
76
|
-
|
|
77
|
-
// 默认后缀名。如果没有后缀名,则会自动添加 extname
|
|
78
|
-
extname: '.art',
|
|
79
|
-
|
|
80
|
-
// 忽略的变量。被模板编译器忽略的模板变量列表
|
|
81
|
-
ignore: [],
|
|
82
|
-
|
|
83
|
-
// 导入的模板变量
|
|
84
|
-
imports: runtime
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
function Defaults() {
|
|
88
|
-
this.$extend = function (options) {
|
|
89
|
-
options = options || {};
|
|
90
|
-
return extend(options, options instanceof Defaults ? options : this);
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
Defaults.prototype = settings;
|
|
94
|
-
|
|
95
|
-
module.exports = new Defaults();
|
package/lib/compile/error.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
4
|
-
|
|
5
|
-
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
6
|
-
|
|
7
|
-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 模板错误处理类
|
|
11
|
-
* @param {Object} options
|
|
12
|
-
*/
|
|
13
|
-
var TemplateError = function (_Error) {
|
|
14
|
-
_inherits(TemplateError, _Error);
|
|
15
|
-
|
|
16
|
-
function TemplateError(options) {
|
|
17
|
-
_classCallCheck(this, TemplateError);
|
|
18
|
-
|
|
19
|
-
var _this = _possibleConstructorReturn(this, (TemplateError.__proto__ || Object.getPrototypeOf(TemplateError)).call(this, options.message));
|
|
20
|
-
|
|
21
|
-
_this.name = 'TemplateError';
|
|
22
|
-
_this.message = formatMessage(options);
|
|
23
|
-
if (Error.captureStackTrace) {
|
|
24
|
-
Error.captureStackTrace(_this, _this.constructor);
|
|
25
|
-
}
|
|
26
|
-
return _this;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return TemplateError;
|
|
30
|
-
}(Error);
|
|
31
|
-
|
|
32
|
-
function formatMessage(_ref) {
|
|
33
|
-
var name = _ref.name,
|
|
34
|
-
source = _ref.source,
|
|
35
|
-
path = _ref.path,
|
|
36
|
-
line = _ref.line,
|
|
37
|
-
column = _ref.column,
|
|
38
|
-
generated = _ref.generated,
|
|
39
|
-
message = _ref.message;
|
|
40
|
-
|
|
41
|
-
if (!source) {
|
|
42
|
-
return message;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
var lines = source.split(/\n/);
|
|
46
|
-
var start = Math.max(line - 3, 0);
|
|
47
|
-
var end = Math.min(lines.length, line + 3);
|
|
48
|
-
|
|
49
|
-
// Error context
|
|
50
|
-
var context = lines.slice(start, end).map(function (code, index) {
|
|
51
|
-
var number = index + start + 1;
|
|
52
|
-
var left = number === line ? ' >> ' : ' ';
|
|
53
|
-
return '' + left + number + '| ' + code;
|
|
54
|
-
}).join('\n');
|
|
55
|
-
|
|
56
|
-
// Alter exception message
|
|
57
|
-
return (path || 'anonymous') + ':' + line + ':' + column + '\n' + (context + '\n\n') + (name + ': ' + message) + (generated ? '\n generated: ' + generated : '');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
module.exports = TemplateError;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isKeyword = require('is-keyword-js');
|
|
4
|
-
var jsTokens = require('js-tokens').default;
|
|
5
|
-
var matchToToken = require('js-tokens').matchToToken;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 将逻辑表达式解释为 Tokens
|
|
9
|
-
* @param {string} code
|
|
10
|
-
* @return {Object[]}
|
|
11
|
-
*/
|
|
12
|
-
var esTokenizer = function esTokenizer(code) {
|
|
13
|
-
var tokens = code.match(jsTokens).map(function (value) {
|
|
14
|
-
jsTokens.lastIndex = 0;
|
|
15
|
-
return matchToToken(jsTokens.exec(value));
|
|
16
|
-
}).map(function (token) {
|
|
17
|
-
if (token.type === 'name' && isKeyword(token.value)) {
|
|
18
|
-
token.type = 'keyword';
|
|
19
|
-
}
|
|
20
|
-
return token;
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
return tokens;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
module.exports = esTokenizer;
|
package/lib/compile/index.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var Compiler = require('./compiler');
|
|
4
|
-
var defaults = require('./defaults');
|
|
5
|
-
var TemplateError = require('./error');
|
|
6
|
-
|
|
7
|
-
var debugRender = function debugRender(error, options) {
|
|
8
|
-
options.onerror(error, options);
|
|
9
|
-
var render = function render() {
|
|
10
|
-
return '{Template Error}';
|
|
11
|
-
};
|
|
12
|
-
render.mappings = [];
|
|
13
|
-
render.sourcesContent = [];
|
|
14
|
-
return render;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 编译模版
|
|
19
|
-
* @param {string|Object} source 模板内容
|
|
20
|
-
* @param {?Object} options 编译选项
|
|
21
|
-
* @return {function}
|
|
22
|
-
*/
|
|
23
|
-
var compile = function compile(source) {
|
|
24
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
25
|
-
|
|
26
|
-
if (typeof source !== 'string') {
|
|
27
|
-
options = source;
|
|
28
|
-
} else {
|
|
29
|
-
options.source = source;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// 合并默认配置
|
|
33
|
-
options = defaults.$extend(options);
|
|
34
|
-
source = options.source;
|
|
35
|
-
|
|
36
|
-
// debug 模式
|
|
37
|
-
/* istanbul ignore if */
|
|
38
|
-
if (options.debug === true) {
|
|
39
|
-
options.cache = false;
|
|
40
|
-
options.minimize = false;
|
|
41
|
-
options.compileDebug = true;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (options.compileDebug) {
|
|
45
|
-
options.minimize = false;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// 转换成绝对路径
|
|
49
|
-
if (options.filename) {
|
|
50
|
-
options.filename = options.resolveFilename(options.filename, options);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
var filename = options.filename;
|
|
54
|
-
var cache = options.cache;
|
|
55
|
-
var caches = options.caches;
|
|
56
|
-
|
|
57
|
-
// 匹配缓存
|
|
58
|
-
if (cache && filename) {
|
|
59
|
-
var _render = caches.get(filename);
|
|
60
|
-
if (_render) {
|
|
61
|
-
return _render;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// 加载外部模板
|
|
66
|
-
if (!source) {
|
|
67
|
-
try {
|
|
68
|
-
source = options.loader(filename, options);
|
|
69
|
-
options.source = source;
|
|
70
|
-
} catch (e) {
|
|
71
|
-
var error = new TemplateError({
|
|
72
|
-
name: 'CompileError',
|
|
73
|
-
path: filename,
|
|
74
|
-
message: 'template not found: ' + e.message,
|
|
75
|
-
stack: e.stack
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
if (options.bail) {
|
|
79
|
-
throw error;
|
|
80
|
-
} else {
|
|
81
|
-
return debugRender(error, options);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
var fn = void 0;
|
|
87
|
-
var compiler = new Compiler(options);
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
fn = compiler.build();
|
|
91
|
-
} catch (error) {
|
|
92
|
-
error = new TemplateError(error);
|
|
93
|
-
if (options.bail) {
|
|
94
|
-
throw error;
|
|
95
|
-
} else {
|
|
96
|
-
return debugRender(error, options);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
var render = function render(data, blocks) {
|
|
101
|
-
try {
|
|
102
|
-
return fn(data, blocks);
|
|
103
|
-
} catch (error) {
|
|
104
|
-
// 运行时出错以调试模式重载
|
|
105
|
-
if (!options.compileDebug) {
|
|
106
|
-
options.cache = false;
|
|
107
|
-
options.compileDebug = true;
|
|
108
|
-
return compile(options)(data, blocks);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
error = new TemplateError(error);
|
|
112
|
-
|
|
113
|
-
if (options.bail) {
|
|
114
|
-
throw error;
|
|
115
|
-
} else {
|
|
116
|
-
return debugRender(error, options)();
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
render.mappings = fn.mappings;
|
|
122
|
-
render.sourcesContent = fn.sourcesContent;
|
|
123
|
-
render.toString = function () {
|
|
124
|
-
return fn.toString();
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
if (cache && filename) {
|
|
128
|
-
caches.set(filename, render);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return render;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
compile.Compiler = Compiler;
|
|
135
|
-
|
|
136
|
-
module.exports = compile;
|
package/lib/compile/runtime.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/*! art-template@runtime | https://github.com/aui/art-template */
|
|
4
|
-
|
|
5
|
-
var globalThis = typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {};
|
|
6
|
-
|
|
7
|
-
var runtime = Object.create(globalThis);
|
|
8
|
-
var ESCAPE_REG = /["&'<>]/;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 编码模板输出的内容
|
|
12
|
-
* @param {any} content
|
|
13
|
-
* @return {string}
|
|
14
|
-
*/
|
|
15
|
-
runtime.$escape = function (content) {
|
|
16
|
-
return xmlEscape(toString(content));
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 迭代器,支持数组与对象
|
|
21
|
-
* @param {array|Object} data
|
|
22
|
-
* @param {function} callback
|
|
23
|
-
*/
|
|
24
|
-
runtime.$each = function (data, callback) {
|
|
25
|
-
if (Array.isArray(data)) {
|
|
26
|
-
for (var i = 0, len = data.length; i < len; i++) {
|
|
27
|
-
callback(data[i], i);
|
|
28
|
-
}
|
|
29
|
-
} else {
|
|
30
|
-
for (var _i in data) {
|
|
31
|
-
callback(data[_i], _i);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
// 将目标转成字符
|
|
37
|
-
function toString(value) {
|
|
38
|
-
if (typeof value !== 'string') {
|
|
39
|
-
if (value === undefined || value === null) {
|
|
40
|
-
value = '';
|
|
41
|
-
} else if (typeof value === 'function') {
|
|
42
|
-
value = toString(value.call(value));
|
|
43
|
-
} else {
|
|
44
|
-
value = JSON.stringify(value);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 编码 HTML 内容
|
|
52
|
-
function xmlEscape(content) {
|
|
53
|
-
var html = '' + content;
|
|
54
|
-
var regexResult = ESCAPE_REG.exec(html);
|
|
55
|
-
if (!regexResult) {
|
|
56
|
-
return content;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
var result = '';
|
|
60
|
-
var i = void 0,
|
|
61
|
-
lastIndex = void 0,
|
|
62
|
-
char = void 0;
|
|
63
|
-
for (i = regexResult.index, lastIndex = 0; i < html.length; i++) {
|
|
64
|
-
switch (html.charCodeAt(i)) {
|
|
65
|
-
case 34:
|
|
66
|
-
char = '"';
|
|
67
|
-
break;
|
|
68
|
-
case 38:
|
|
69
|
-
char = '&';
|
|
70
|
-
break;
|
|
71
|
-
case 39:
|
|
72
|
-
char = ''';
|
|
73
|
-
break;
|
|
74
|
-
case 60:
|
|
75
|
-
char = '<';
|
|
76
|
-
break;
|
|
77
|
-
case 62:
|
|
78
|
-
char = '>';
|
|
79
|
-
break;
|
|
80
|
-
default:
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (lastIndex !== i) {
|
|
85
|
-
result += html.substring(lastIndex, i);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
lastIndex = i + 1;
|
|
89
|
-
result += char;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (lastIndex !== i) {
|
|
93
|
-
return result + html.substring(lastIndex, i);
|
|
94
|
-
} else {
|
|
95
|
-
return result;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
module.exports = runtime;
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var TYPE_STRING = 'string';
|
|
4
|
-
var TYPE_EXPRESSION = 'expression';
|
|
5
|
-
var TYPE_RAW = 'raw';
|
|
6
|
-
var TYPE_ESCAPE = 'escape';
|
|
7
|
-
|
|
8
|
-
function wrapString(token) {
|
|
9
|
-
var value = new String(token.value);
|
|
10
|
-
value.line = token.line;
|
|
11
|
-
value.start = token.start;
|
|
12
|
-
value.end = token.end;
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function Token(type, value, prevToken) {
|
|
17
|
-
this.type = type;
|
|
18
|
-
this.value = value;
|
|
19
|
-
this.script = null;
|
|
20
|
-
|
|
21
|
-
if (prevToken) {
|
|
22
|
-
this.line = prevToken.line + prevToken.value.split(/\n/).length - 1;
|
|
23
|
-
if (this.line === prevToken.line) {
|
|
24
|
-
this.start = prevToken.end;
|
|
25
|
-
} else {
|
|
26
|
-
this.start = prevToken.value.length - prevToken.value.lastIndexOf('\n') - 1;
|
|
27
|
-
}
|
|
28
|
-
} else {
|
|
29
|
-
this.line = 0;
|
|
30
|
-
this.start = 0;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
this.end = this.start + this.value.length;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 将模板转换为 Tokens
|
|
38
|
-
* @param {string} source
|
|
39
|
-
* @param {Object[]} rules @see defaults.rules
|
|
40
|
-
* @param {Object} context
|
|
41
|
-
* @return {Object[]}
|
|
42
|
-
*/
|
|
43
|
-
var tplTokenizer = function tplTokenizer(source, rules) {
|
|
44
|
-
var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
45
|
-
|
|
46
|
-
var tokens = [new Token(TYPE_STRING, source)];
|
|
47
|
-
|
|
48
|
-
for (var i = 0; i < rules.length; i++) {
|
|
49
|
-
var rule = rules[i];
|
|
50
|
-
var flags = rule.test.ignoreCase ? 'ig' : 'g';
|
|
51
|
-
var regexp = new RegExp(rule.test.source, flags);
|
|
52
|
-
|
|
53
|
-
for (var _i = 0; _i < tokens.length; _i++) {
|
|
54
|
-
var token = tokens[_i];
|
|
55
|
-
var prevToken = tokens[_i - 1];
|
|
56
|
-
|
|
57
|
-
if (token.type !== TYPE_STRING) {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
var match = void 0,
|
|
62
|
-
index = 0;
|
|
63
|
-
var substitute = [];
|
|
64
|
-
var value = token.value;
|
|
65
|
-
|
|
66
|
-
while ((match = regexp.exec(value)) !== null) {
|
|
67
|
-
if (match.index > index) {
|
|
68
|
-
prevToken = new Token(TYPE_STRING, value.slice(index, match.index), prevToken);
|
|
69
|
-
substitute.push(prevToken);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
prevToken = new Token(TYPE_EXPRESSION, match[0], prevToken);
|
|
73
|
-
match[0] = wrapString(prevToken);
|
|
74
|
-
prevToken.script = rule.use.apply(context, match);
|
|
75
|
-
substitute.push(prevToken);
|
|
76
|
-
|
|
77
|
-
index = match.index + match[0].length;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (index < value.length) {
|
|
81
|
-
prevToken = new Token(TYPE_STRING, value.slice(index), prevToken);
|
|
82
|
-
substitute.push(prevToken);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
tokens.splice.apply(tokens, [_i, 1].concat(substitute));
|
|
86
|
-
_i += substitute.length - 1;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return tokens;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
tplTokenizer.TYPE_STRING = TYPE_STRING;
|
|
94
|
-
tplTokenizer.TYPE_EXPRESSION = TYPE_EXPRESSION;
|
|
95
|
-
tplTokenizer.TYPE_RAW = TYPE_RAW;
|
|
96
|
-
tplTokenizer.TYPE_ESCAPE = TYPE_ESCAPE;
|
|
97
|
-
|
|
98
|
-
module.exports = tplTokenizer;
|
package/lib/defaults.js
DELETED
package/lib/extension.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var templatePath = require.resolve('./index.js');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* require.extensions 扩展注册函数
|
|
7
|
-
* 使用动态编译机制
|
|
8
|
-
* @param {Object} module
|
|
9
|
-
* @param {string} flnm
|
|
10
|
-
*/
|
|
11
|
-
var extension = function extension(module, flnm) {
|
|
12
|
-
var filename = flnm || module.filename;
|
|
13
|
-
var imports = 'var template=require(' + JSON.stringify(templatePath) + ')';
|
|
14
|
-
var options = JSON.stringify({
|
|
15
|
-
filename: filename
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
module._compile(imports + '\n' + 'module.exports = template.compile(' + options + ');', filename);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
module.exports = extension;
|
package/lib/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var render = require('./render');
|
|
4
|
-
var compile = require('./compile');
|
|
5
|
-
var defaults = require('./defaults');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 模板引擎
|
|
9
|
-
* @param {string} filename 模板名
|
|
10
|
-
* @param {Object|string} content 数据或模板内容
|
|
11
|
-
* @return {string|function} 如果 content 为 string 则编译并缓存模板,否则渲染模板
|
|
12
|
-
*/
|
|
13
|
-
var template = function template(filename, content) {
|
|
14
|
-
return content instanceof Object ? render({
|
|
15
|
-
filename: filename
|
|
16
|
-
}, content) : compile({
|
|
17
|
-
filename: filename,
|
|
18
|
-
source: content
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
template.render = render;
|
|
23
|
-
template.compile = compile;
|
|
24
|
-
template.defaults = defaults;
|
|
25
|
-
|
|
26
|
-
module.exports = template;
|