art-template 4.13.1 → 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.
@@ -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;
@@ -1,98 +0,0 @@
1
- 'use strict';
2
-
3
- /*! art-template@runtime | https://github.com/aui/art-template */
4
-
5
- var detectNode = typeof window === 'undefined';
6
- var runtime = Object.create(detectNode ? global : window);
7
- var ESCAPE_REG = /["&'<>]/;
8
-
9
- /**
10
- * 编码模板输出的内容
11
- * @param {any} content
12
- * @return {string}
13
- */
14
- runtime.$escape = function (content) {
15
- return xmlEscape(toString(content));
16
- };
17
-
18
- /**
19
- * 迭代器,支持数组与对象
20
- * @param {array|Object} data
21
- * @param {function} callback
22
- */
23
- runtime.$each = function (data, callback) {
24
- if (Array.isArray(data)) {
25
- for (var i = 0, len = data.length; i < len; i++) {
26
- callback(data[i], i);
27
- }
28
- } else {
29
- for (var _i in data) {
30
- callback(data[_i], _i);
31
- }
32
- }
33
- };
34
-
35
- // 将目标转成字符
36
- function toString(value) {
37
- if (typeof value !== 'string') {
38
- if (value === undefined || value === null) {
39
- value = '';
40
- } else if (typeof value === 'function') {
41
- value = toString(value.call(value));
42
- } else {
43
- value = JSON.stringify(value);
44
- }
45
- }
46
-
47
- return value;
48
- }
49
-
50
- // 编码 HTML 内容
51
- function xmlEscape(content) {
52
- var html = '' + content;
53
- var regexResult = ESCAPE_REG.exec(html);
54
- if (!regexResult) {
55
- return content;
56
- }
57
-
58
- var result = '';
59
- var i = void 0,
60
- lastIndex = void 0,
61
- char = void 0;
62
- for (i = regexResult.index, lastIndex = 0; i < html.length; i++) {
63
- switch (html.charCodeAt(i)) {
64
- case 34:
65
- char = '&#34;';
66
- break;
67
- case 38:
68
- char = '&#38;';
69
- break;
70
- case 39:
71
- char = '&#39;';
72
- break;
73
- case 60:
74
- char = '&#60;';
75
- break;
76
- case 62:
77
- char = '&#62;';
78
- break;
79
- default:
80
- continue;
81
- }
82
-
83
- if (lastIndex !== i) {
84
- result += html.substring(lastIndex, i);
85
- }
86
-
87
- lastIndex = i + 1;
88
- result += char;
89
- }
90
-
91
- if (lastIndex !== i) {
92
- return result + html.substring(lastIndex, i);
93
- } else {
94
- return result;
95
- }
96
- }
97
-
98
- 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
@@ -1,3 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = require('./compile/defaults');
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;
package/lib/precompile.js DELETED
@@ -1,242 +0,0 @@
1
- 'use strict';
2
-
3
- var path = require('path');
4
- var acorn = require('acorn');
5
- var escodegen = require('escodegen');
6
- var estraverse = require('estraverse');
7
- var sourceMap = require('source-map');
8
- var mergeSourceMap = require('merge-source-map');
9
- var compile = require('./compile');
10
- var defaults = require('./defaults');
11
- var runtimePath = require.resolve('./runtime');
12
-
13
- var CONSTS = compile.Compiler.CONSTS;
14
- var LOCAL_MODULE = /^\.+\//;
15
-
16
- // 获取默认设置
17
- var getDefaults = function getDefaults(options) {
18
- // new defaults
19
- var setting = {
20
- imports: runtimePath,
21
- bail: true,
22
- cache: false,
23
- debug: false,
24
-
25
- sourceMap: false,
26
- sourceRoot: options.sourceRoot
27
- };
28
-
29
- for (var name in options) {
30
- setting[name] = options[name];
31
- }
32
-
33
- return defaults.$extend(setting);
34
- };
35
-
36
- // 转换外部模板文件引入语句的 filename 参数节点
37
- // 所有绝对路径都转换成相对路径
38
- var convertFilenameNode = function convertFilenameNode(node, options) {
39
- if (node.type === 'Literal') {
40
- var resolvePath = options.resolveFilename(node.value, options);
41
- var dirname = path.dirname(options.filename);
42
- var relativePath = path.relative(dirname, resolvePath);
43
-
44
- if (LOCAL_MODULE.test(relativePath)) {
45
- node.value = relativePath;
46
- } else {
47
- node.value = './' + relativePath;
48
- }
49
-
50
- delete node.raw;
51
- }
52
-
53
- return node;
54
- };
55
-
56
- // 获取原始渲染函数的 sourceMap
57
- var getOldSourceMap = function getOldSourceMap(mappings, _ref) {
58
- var sourceRoot = _ref.sourceRoot,
59
- source = _ref.source,
60
- file = _ref.file;
61
-
62
- var oldSourceMap = new sourceMap.SourceMapGenerator({
63
- file: file,
64
- sourceRoot: sourceRoot
65
- });
66
-
67
- mappings.forEach(function (mapping) {
68
- mapping.source = source;
69
- oldSourceMap.addMapping(mapping);
70
- });
71
-
72
- return oldSourceMap.toJSON();
73
- };
74
-
75
- /**
76
- * 预编译模版,将模板编译成 javascript 代码
77
- * 使用静态分析,将模板内部之间依赖转换成 `require()`
78
- * @param {Object} options 编译选项
79
- * @return {Object}
80
- */
81
- var precompile = function precompile() {
82
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
83
-
84
- if (typeof options.filename !== 'string') {
85
- throw Error('template.precompile(): "options.filename" required');
86
- }
87
-
88
- options = getDefaults(options);
89
-
90
- var code = null;
91
- var sourceMap = null;
92
- var ast = null;
93
- var imports = options.imports;
94
- var functions = [CONSTS.INCLUDE, CONSTS.EXTEND];
95
-
96
- if (typeof imports !== 'string') {
97
- throw Error('template.precompile(): "options.imports" is a file. Example:\n' + 'options: { imports: require.resolve("art-template/lib/runtime") }\n');
98
- } else {
99
- options.imports = require(imports);
100
- }
101
-
102
- var isLocalModule = LOCAL_MODULE.test(imports);
103
- var tplImportsPath = isLocalModule ? imports : path.relative(path.dirname(options.filename), imports);
104
- var fn = compile(options);
105
-
106
- code = '(' + fn.toString() + ')';
107
- ast = acorn.parse(code, {
108
- locations: options.sourceMap
109
- });
110
-
111
- var extendNode = null;
112
- var enter = function enter(node) {
113
- if (node.type === 'VariableDeclarator' && functions.indexOf(node.id.name) !== -1) {
114
- // TODO 对变量覆盖进行抛错
115
- if (node.id.name === CONSTS.INCLUDE) {
116
- node['init'] = {
117
- type: 'FunctionExpression',
118
- params: [{
119
- type: 'Identifier',
120
- name: 'content'
121
- }],
122
- body: {
123
- type: 'BlockStatement',
124
- body: [{
125
- type: 'ExpressionStatement',
126
- expression: {
127
- type: 'AssignmentExpression',
128
- operator: '+=',
129
- left: {
130
- type: 'Identifier',
131
- name: CONSTS.OUT
132
- },
133
- right: {
134
- type: 'Identifier',
135
- name: 'content'
136
- }
137
- }
138
- }, {
139
- type: 'ReturnStatement',
140
- argument: {
141
- type: 'Identifier',
142
- name: CONSTS.OUT
143
- }
144
- }]
145
- }
146
- };
147
- return node;
148
- } else {
149
- this.remove();
150
- }
151
- } else if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && functions.indexOf(node.callee.name) !== -1) {
152
- var replaceNode = void 0;
153
- switch (node.callee.name) {
154
- case CONSTS.EXTEND:
155
- extendNode = convertFilenameNode(node.arguments[0], options);
156
- replaceNode = {
157
- type: 'AssignmentExpression',
158
- operator: '=',
159
- left: {
160
- type: 'Identifier',
161
- name: CONSTS.FROM
162
- },
163
- right: {
164
- type: 'Literal',
165
- value: true
166
- }
167
- };
168
-
169
- break;
170
-
171
- case CONSTS.INCLUDE:
172
- var filename = node.arguments.shift();
173
- var filenameNode = filename.name === CONSTS.FROM ? extendNode : convertFilenameNode(filename, options);
174
- var paramNodes = node.arguments.length ? node.arguments : [{
175
- type: 'Identifier',
176
- name: CONSTS.DATA
177
- }];
178
-
179
- replaceNode = node;
180
- replaceNode['arguments'] = [{
181
- type: 'CallExpression',
182
- callee: {
183
- type: 'CallExpression',
184
- callee: {
185
- type: 'Identifier',
186
- name: 'require'
187
- },
188
- arguments: [filenameNode]
189
- },
190
- arguments: paramNodes
191
- }];
192
-
193
- break;
194
- }
195
-
196
- return replaceNode;
197
- }
198
- };
199
-
200
- ast = estraverse.replace(ast, {
201
- enter: enter
202
- });
203
-
204
- if (options.sourceMap) {
205
- var sourceRoot = options.sourceRoot;
206
- var source = path.relative(sourceRoot, options.filename);
207
- var file = path.basename(source);
208
- var gen = escodegen.generate(ast, {
209
- sourceMap: source,
210
- file: file,
211
- sourceMapRoot: sourceRoot,
212
- sourceMapWithCode: true
213
- });
214
- code = gen.code;
215
-
216
- var newSourceMap = gen.map.toJSON();
217
- var oldSourceMap = getOldSourceMap(fn.mappings, {
218
- sourceRoot: sourceRoot,
219
- source: source,
220
- file: file
221
- });
222
- sourceMap = mergeSourceMap(oldSourceMap, newSourceMap);
223
- sourceMap.file = file;
224
- sourceMap.sourcesContent = fn.sourcesContent;
225
- } else {
226
- code = escodegen.generate(ast);
227
- }
228
-
229
- code = code.replace(/^\(|\)[;\s]*?$/g, '');
230
- code = 'var ' + CONSTS.IMPORTS + ' = require(' + JSON.stringify(tplImportsPath) + ');\n' + 'module.exports = ' + code + ';';
231
-
232
- return {
233
- code: code,
234
- ast: ast,
235
- sourceMap: sourceMap,
236
- toString: function toString() {
237
- return code;
238
- }
239
- };
240
- };
241
-
242
- module.exports = precompile;
package/lib/render.js DELETED
@@ -1,16 +0,0 @@
1
- 'use strict';
2
-
3
- var compile = require('./compile');
4
-
5
- /**
6
- * 渲染模板
7
- * @param {string|Object} source 模板内容
8
- * @param {Object} data 数据
9
- * @param {?Object} options 选项
10
- * @return {string} 渲染好的字符串
11
- */
12
- var render = function render(source, data, options) {
13
- return compile(source, options)(data);
14
- };
15
-
16
- module.exports = render;
package/lib/runtime.js DELETED
@@ -1,3 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = require('./compile/runtime');