-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (147 loc) · 4.39 KB
/
index.js
File metadata and controls
186 lines (147 loc) · 4.39 KB
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
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
function debug(str) {
if(gulpRegexReplace.debug == true) {
console.log(str);
}
}
function findMatch(input, regexOptions) {
var result = [input];
var regex;
while((regex = regexOptions.shift()) != null) {
result = findSingleMatch(input, regex);
input = result.join(' ');
}
return result;
}
function regexMatch(input, regex) {
var result = [];
if (regex == void 0) {
return null;
}
var regexp = new RegExp(regex, "g");
var match = regexp.exec(input);
while (match != null && match[0] != '') {
var index, v;
if (match.length == 1) {
index = 0;
} else { index = 1; }
while (index < match.length) {
v = match[index];
result.push(v)
index++;
}
match = regexp.exec(input);
}
return result;
}
function findSingleMatch(input, regex) {
var result = [];
if (regex == void 0) { return null; }
var include = regex, exclude;
if (regex.exclude != void 0) { exclude = regex.exclude; }
if (regex.include != void 0) { include = regex.include; }
var excludeList = regexMatch(input, exclude);
var includeList = regexMatch(input, include);
if(excludeList != null) {
return includeList.filter(function(element) {return excludeList.indexOf(element) < 0;});
} else {
return includeList;
}
}
function isWord(str) {
if ((str.indexOf(' ') === -1) &&
(str.indexOf('.') === -1) &&
(str.indexOf(',') === -1) &&
(str.indexOf('=') === -1) &&
(str.indexOf(';') === -1)) {
return true;
}
return false;
}
function shouldExclude(element, globalExclude) {
var result = false;
globalExclude.forEach(function(exclude) {
if(exclude != void 0 && exclude != null && exclude != '') {
var regex = new RegExp(exclude, 'g');
var match = regex.exec(element);
var index;
if (match != null) {
if (match.length == 1) {
index = 0;
} else { index = 1; }
while (index < match.length) {
if (element == match[index]) {
result = true;
}
index++;
}
}
}
});
return result;
}
function convertString(input, regexOptions, replace, globalExclude) {
if(!(regexOptions instanceof Array)) { regexOptions = [regexOptions]; }
if(!(globalExclude instanceof Array)) { globalExclude = [globalExclude]; }
if (regexOptions[0] == void 0) { return input; }
var result = input;
var matches = findMatch(input, regexOptions);
if (matches != null) {
matches.forEach(function(element, index, array) {
var r = replace;
if (typeof(replace) == 'function') {
r = replace(element);
}
var regexReplace;
if (!shouldExclude(element, globalExclude)) {
if (isWord(element)) {
debug('replacing word: ' + element + ' with ' + r);
regexReplace = new RegExp('\\b' + element + '\\b', 'g');
} else {
debug('replacing: ' + element + ' with ' + r);
regexReplace = new RegExp(element, 'g');
}
result = result.replace(regexReplace, r);
// also replace all future replace strings
matches.forEach(function(element, index, array) {
array[index] = element.replace(regexReplace, r);
});
}
});
}
return result;
};
var gulpRegexReplace = function(options) {
if(!(options instanceof Array)) {
options = [options];
}
options.forEach(function(element, index, array) {
if (element == void 0) { element = {}; }
if (element.replace == void 0) { element.replace = ''; }
});
return through.obj(function(file, enc, callback) {
if (!('contents' in file)) {
this.push(file);
return callback();
} else if (file.isNull()) {
this.push(file);
return callback();
} else if (file.isStream()) {
throw new gutil.PluginError('gulp-regex-replace', 'streams not implemented');
} else if (file.isBuffer()) {
var contents = String(file.contents);
options.forEach(function(element, index, array) {
if(contents == null) {
throw new gutil.PluginError('gulp-regex-replace', 'contents is null');
}
contents = convertString(contents, element.regex, element.replace, element.exclude);
});
file.contents = new Buffer(contents);
}
this.push(file);
return callback();
});
};
module.exports = gulpRegexReplace;