-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
239 lines (201 loc) · 6.64 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var htmlparser = require("htmlparser2");
var through = require("through2");
var gutil = require("gulp-util");
var PluginError = gutil.PluginError;
// Consts
const PLUGIN_NAME = "gulp-incremental";
const EXCEPTIONS = ["pre", "script"];
const DUTY = ["evaluate"];
const BREAK_LINE = /(\r\n|\n|\r)/gm;
const NEW_LINE = "String.fromCharCode(10)";
// variables
var _result = [];
var _currentTag = null;
var _formatLevel = 1;
var _options = {
parameterName: "data",
functionName: function(filename, path) {
return filename;
},
template: {
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g
},
escape: /[&<>]/g,
MAP: {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
},
format: true,
ignore: "js",
helpers: {
open: "{%",
close: "%}"
}
};
function escapeHTML(s) {
return s.replace(_options.escape, function (c) {
return _options.MAP[c];
});
}
function flushParser() {
_result.length = 0;
_currentTag = null;
}
function insertTabs(number) {
var i, tabs = '';
if (_options.format)
for (i = 0, tabs += "\n"; i < number; i++) {
tabs += "\t";
}
return tabs;
}
function warpInFunc(name, string) {
var suffix = _options.format ? "\n}" : "}";
return "function " + name + "(" + _options.parameterName + "){" + string + suffix;
}
function extractFileName(path) {
var parts = path.split("/");
var name = parts.pop();
return _options.functionName(name.split(".")[0], parts.join("/"));
}
function decodeTemplates(string, openTag, closeTag) {
var regex = new RegExp(openTag + '(.*?)' + closeTag, 'g');
var prefix = true;
var suffix = true;
var result = string.replace(regex, function (match, p1, index, string) {
if (index !== 0)
p1 = "\'+" + p1;
else
prefix = false;
if ((string.length - (index + match.length)) > 0)
p1 += "+\'";
else
suffix = false;
return p1;
});
return (prefix ? '\'' : '') + result + (suffix ? '\'' : '');
}
function encodeTemplates(string) {
return string
.replace(_options.template.interpolate, function (match, p1) {
return _options.helpers.open + p1 + _options.helpers.close;
})
.replace(_options.template.escape, function (match, p1) {
return _options.helpers.open + escapeHTML(p1) + _options.helpers.close;
})
.replace(_options.template.evaluate, function (match, p1) {
return "<evaluate>" + p1.trim() + "</evaluate>";
});
}
function writeCommand(command, line, noEscape) {
var attribs = "";
if (line.length === 0) // don't write empty string or array
return;
if (typeof line === "string") {
if (noEscape)
attribs = line;
else
attribs = "'" + line.replace("'", "\\'") + "'"; // wrap attribute value
} else { // create formatted string from array
for (var i = 0; i < line.length; i++) {
if (i > 0) // add comma between parameters
attribs += ", ";
attribs += line[i];
}
}
// wrap in command
_result.push(insertTabs(_formatLevel) + command + "(" + attribs + ");");
}
function writeLine(string, noEscape) {
_result.push(noEscape ? string : string.replace(BREAK_LINE, " "));
}
var _handler = {
onopentag: function (name, attribs) {
var args = ["'" + name + "'"];
if (DUTY.indexOf(name) === -1) {
for (var key in attribs) {
if (attribs.hasOwnProperty(key)) {
if (args.length === 1) {
args.push(null);
args.push(null);
}
args.push("'" + key + "'");
args.push(decodeTemplates(attribs[key], _options.helpers.open, _options.helpers.close));
}
}
writeCommand("elementOpen", args);
} else {
writeLine(insertTabs(_formatLevel), true);
}
_currentTag = name;
_formatLevel++;
},
ontext: function (text) {
var line;
if (DUTY.indexOf(_currentTag) !== -1) {
writeLine(text);
} else if (EXCEPTIONS.indexOf(_currentTag) === -1) {
line = text.replace(BREAK_LINE, "").trim();
if (line.length > 0)
writeCommand("text", decodeTemplates(line, _options.helpers.open, _options.helpers.close), true);
} else { // save format (break lines) for exception tags
var lines = text.split(BREAK_LINE);
for (var i = 0; i < lines.length; i++) {
line = lines[i];
if (BREAK_LINE.exec(line))
writeCommand("text", NEW_LINE, true);
else
writeCommand("text", decodeTemplates(line, _options.helpers.open, _options.helpers.close), true);
}
}
},
onclosetag: function (tagname) {
_formatLevel--;
if (DUTY.indexOf(tagname) === -1)
writeCommand("elementClose", tagname);
}
};
// Plugin level function(dealing with files)
function gulpCompiler(newOptions) {
// mix options
for (var key in newOptions) {
if (newOptions.hasOwnProperty(key))
_options[key] = newOptions[key];
}
// Creating a stream through which each file will pass
return through.obj(function (file, enc, cb) {
var buffer, content, parser, extension;
if (file.isNull()) {
// return empty file
return cb(null, file);
}
if (file.isBuffer()) {
extension = file.path.split('.');
extension = extension[extension.length - 1];
if (extension !== _options.ignore) {
parser = new htmlparser.Parser( _handler, {decodeEntities: true});
//get file content as string
buffer = new Buffer(file.contents);
content = buffer.toString();
// parse content
parser.write(encodeTemplates(content));
parser.end();
// wrap parse result in function
content = warpInFunc(extractFileName(file.path), _result.join(""));
file.contents = new Buffer(content, "utf-8");
// clear parser
flushParser();
}
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, "Sorry, stream mode not supported");
}
cb(null, file);
});
}
module.exports = gulpCompiler;