-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
69 lines (48 loc) · 1.86 KB
/
test.js
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
/**
* Created by Rodey on 2015/11/9.
*/
var fs = require('fs'),
path = require('path'),
through = require('through2'),
util = require('util'),
Tool = require('./lib/tools');
//正则匹配
var tagName = 'includ',
includerRegx = new RegExp('<' + tagName + '\\s+([\\s\\S]*?)>([\\s\\S]*?)<\\/' + tagName + '>', 'gi'),
srcRegx = new RegExp('\\s*src="([\\s\\S]*?)"', 'gi'),
attrReg = new RegExp('\\s+(\\S+)="([\\s\\S]*?)"', 'gi');
var file = './example/src/tagname.html';
var includer = function(options){
options = options || { tagAttr: true };
var content = Tool.getFileContent(file);
//console.log(content);
if(options.tagName){
includerRegx = new RegExp('<' + options.tagName + '\\s+([\\s\\S]*?)>([\\s\\S]*?)<\\/' + options.tagName + '>', 'gi');
}
content = content.replace(includerRegx, function($1){
//console.log($1);
var ms = srcRegx.exec($1),
src = ms[1] || '';
srcRegx.lastIndex = 0;
//console.log(path.dirname(file));
src = path.normalize(path.dirname(file) + path.sep + src);
var htmlContent = Tool.getFileContent(src);
//=========标签内容属性替换
/**
* exp: <includ src="assets/layout/header.html" title="html页面已引入" css="index.css"></includ>
*/
htmlContent = Tool.extractTagAttr(htmlContent, $1, attrReg);
//=========内容属性替换
/**
* <includ src="assets/layout/header.html">
* @title = html页面已引入
* @css = index.css
* </includ>
*/
htmlContent = Tool.extractTagContent(htmlContent, includerRegx, $1);
//console.log(htmlContent);
return Tool.rtrim(htmlContent);
});
console.log(content);
};
includer({ tagName: 'require_once' });