UNPKG

1.06 kBJavaScriptView Raw
1var jade = require('jade');
2
3/**
4 * jade 模板渲染
5 *
6 * @param {string} content 文本内容
7 * @param {Object} attrs 属性
8 * @param {string} attrs.data 数据项
9 * @param {Object} scope 作用域
10 * @param {Function} scope.execImport 导入数据
11 */
12module.exports = function processor(content, attrs, scope) {
13 if (!content) {
14 return content;
15 }
16 var match = content.match(/^[^\n\S]+/);
17 var space;
18 if (match) {
19 space = match[0].length;
20 /*jslint evil: true */
21 var regex = new Function('return (/^[^\\n\\S]{' + space + '}/gm)')();
22 content = content.replace(regex, '');
23 space = match[0];
24 }
25 render = jade.compile(content, {
26 pretty: true
27 });
28 var data;
29 if (attrs.data) {
30 /*jslint evil: true */
31 data = scope.execImport(attrs.data);
32 if (typeof data === 'string') {
33 data = new Function(
34 'return (' + data + ');'
35 )();
36 }
37 }
38 else {
39 data = null;
40 }
41 content = render(data);
42 if (space) { // 需要补空白
43 content = content.replace(/^/gm, space);
44 }
45 return content;
46};
\No newline at end of file