UNPKG

975 BJavaScriptView Raw
1var colors = require('colors');
2
3/**
4 * 字符截取
5 *
6 * @param {string} content 文本内容
7 * @param {Object} attrs 属性
8 * @param {string} attrs.pattern 表达式
9 * @param {string} attrs.replacement 替换内容
10 * @param {Object} scope 作用域
11 * @param {Function} scope.execImport 导入数据
12 * @param {Function} scope.compile 变量 jdists 文本
13 */
14module.exports = function processor(content, attrs, scope) {
15 var pattern = scope.execImport(attrs.pattern);
16 var replacement = scope.execImport(attrs.replacement);
17 var regex;
18 if (/^\s*\/.*\/([img]{0,3})\s*$/.test(pattern) ||
19 /^(['"]).*\1$/.test(pattern)) {
20 try {
21 /*jslint evil: true */
22 regex = new Function('return (' + pattern + ')')();
23 }
24 catch (ex) {
25 console.error(colors.red(ex.message));
26 return content;
27 }
28 }
29 else {
30 regex = pattern;
31 }
32 if (!regex) {
33 return content;
34 }
35 return scope.compile(content.replace(regex, replacement || ''));
36};
\No newline at end of file