forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
236 lines (182 loc) · 6 KB
/
tasks.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
'use strict';
var _ = require('lodash');
var del = require('del');
var gear = require('gear');
var path = require('path');
var fs = require('fs');
var parseHeader = require('./utility').parseHeader;
var tasks = require('gear-lib');
tasks.clean = function(directories, blobs, done) {
directories = _.isString(directories) ? [directories] : directories;
del(directories, function(err) {
return done(err, blobs);
});
};
tasks.clean.type = 'collect';
tasks.reorderDeps = function(options, blobs, done) {
var buffer = {},
newBlobOrder = [];
_.each(blobs, function(blob) {
var basename = path.basename(blob.name),
fileInfo = parseHeader(blob.result),
extra = {blob: blob, processed: false};
buffer[basename] = _.merge(extra, fileInfo || {});
});
function pushInBlob(object) {
if(!object.processed) {
object.processed = true;
newBlobOrder.push(object.blob);
}
}
_.each(buffer, function(buf) {
var object;
if(buf.Requires) {
_.each(buf.Requires, function(language) {
object = buffer[language];
pushInBlob(object);
});
}
pushInBlob(buf);
});
done(null, newBlobOrder);
};
tasks.reorderDeps.type = 'collect';
tasks.template = function(options, blob, done) {
options = options || {};
var content = blob.result.trim(),
filename = path.basename(blob.name),
basename = filename.replace(/\.js$/, ''),
data, hasTemplate, newBlob, template;
if(_.isString(options)) options = { template: options };
if(basename !== options.skip) {
data = {
name: basename,
filename: filename,
content: content
};
hasTemplate = _.contains(_.keys(options), basename);
template = hasTemplate ? options[basename] : options.template;
content = _.template(template)(data);
newBlob = new gear.Blob(content, blob);
} else {
newBlob = blob;
}
return done(null, newBlob);
};
tasks.templateAll = function(template, blobs, done) {
var names, content;
names = _.map(blobs, function(blob) {
return path.basename(blob.name, '.js');
});
content = _.template(template)({ names: names });
return done(null, [new blobs[0].constructor(content, blobs)]);
};
tasks.templateAll.type = 'collect';
tasks.rename = function(options, blob, done) {
options = options || {};
var name = blob.name,
ext = new RegExp(path.extname(name) + '$');
name = name.replace(ext, options.extname);
return done(null, new gear.Blob(blob.result, {name: name}));
};
tasks.buildPackage = function(json, blob, done) {
var contributors = [],
lines = blob.result.split(/\r?\n/),
regex = /^- (.*) <(.*)>$/,
result;
_.each(lines, function(line) {
var matches = line.match(regex);
if(matches) {
contributors.push({
name: matches[1],
email: matches[2]
});
}
});
json.contributors = contributors;
result = JSON.stringify(json, null, ' ');
return done(null, new gear.Blob(result, blob));
};
tasks.replaceSkippingStrings = function(params, blob, done) {
var content = blob.result,
length = content.length,
offset = 0,
replace = params.replace || '',
regex = params.regex,
starts = /\/\/|['"\/]/,
result = [],
chunk, end, match, start, terminator;
while(offset < length) {
chunk = content.slice(offset);
match = chunk.match(starts);
end = match ? match.index : length;
chunk = content.slice(offset, end + offset);
result.push(chunk.replace(regex, replace));
offset += end;
if(match) {
// We found a starter sequence: either a `//` or a "quote"
// In the case of `//` our terminator is the end of line.
// Otherwise it's either a matching quote or an escape symbol.
terminator = match[0] !== '//' ? new RegExp('[' + match[0] + '\\\\]') : /$/m;
start = offset;
offset += 1;
while(true) {
chunk = content.slice(offset);
match = chunk.match(terminator);
if(!match) {
return done('Unmatched quote');
}
if(match[0] === '\\') {
offset += match.index + 2;
} else {
offset += match.index + 1;
result.push(content.slice(start, offset));
break;
}
}
}
}
return done(null, new gear.Blob(result.join(''), blob));
};
tasks.filter = function(callback, blobs, done) {
var filteredBlobs = _.filter(blobs, callback);
// Re-add in blobs required from header definition
_.each(filteredBlobs, function(blob) {
var dirname = path.dirname(blob.name),
content = blob.result,
fileInfo = parseHeader(content);
if(fileInfo && fileInfo.Requires) {
_.each(fileInfo.Requires, function(language) {
var filename = dirname + '/' + language,
fileFound = _.find(filteredBlobs, { name: filename });
if(!fileFound) {
filteredBlobs.push(
_.find(blobs, { name: filename }));
}
});
}
});
return done(null, filteredBlobs);
};
tasks.filter.type = 'collect';
tasks.readSnippet = function(options, blob, done) {
var name = path.basename(blob.name, '.js'),
fileInfo = parseHeader(blob.result),
snippetName = path.join(dir.root, 'test', 'detect', name, 'default.txt');
function onRead(error, blob) {
if (error !== null) return done(null, null); // ignore missing snippets
var meta = {name: name + '.js', fileInfo: fileInfo};
blob = new gear.Blob(blob.result, meta);
return done(error, blob);
}
gear.Blob.readFile(snippetName, 'utf8', onRead, false);
};
tasks.templateDemo = function(options, blobs, done) {
var name = path.join(dir.root, 'demo', 'index.html'),
template = fs.readFileSync(name, 'utf8'),
blobs = _.filter(blobs, Boolean), // drop missing blobs
content = _.template(template)({ path: path, blobs: blobs });
return done(null, [new gear.Blob(content)]);
};
tasks.templateDemo.type = 'collect';
module.exports = new gear.Registry({ tasks: tasks });