This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
413 lines (379 loc) · 11.6 KB
/
gulpfile.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const minimist = require("minimist");
const gulp = require("gulp");
const cheerio = require("gulp-cheerio");
const imagemin = require("gulp-imagemin");
const cleanCSS = require("gulp-clean-css");
const terser = require("gulp-minify");
const pretty = require("gulp-beautify");
const del = require("del");
const utils = {
getProperty(string) {
return string.split("=")[0]
},
getValue(string) {
return string.split("=")[1].replace(/['"]+/g, "")
}
};
const args = minimist(process.argv.slice(2), {
string: ["config", "input", "output"],
boolean: ["force"],
alias: {c: "config", F: "force", i: "input", o: "output"},
default: {config: "./config.json", force: false, input: "input", output: "output"}
});
const filePaths = {
input: args.input,
output: args.output,
get html() {
return this.output + "/**/*.{xhtml,html}"
},
get css() {
return this.output + "/**/*.css"
},
get js() {
return this.output + "/**/*.js"
},
get images() {
return this.output + "/**/*.{gif,jpg,jpeg,png,svg}"
},
get opf() {
return this.output + "/**/*.opf"
},
get ncx() {
return this.output + "/**/*.ncx"
}
}
const config = require(args.config);
const configOpts = config.options;
const cheerioOpts = {
xmlMode: true,
decodeEntities: false,
lowerCaseTags: true,
lowerCaseAttributeNames: true,
recognizeSelfClosing: true
}
const prettyOpts = {
"indent_size": 2,
"max_preserve_newlines": 1
}
function init() {
console.log("The scope of this config file is: " + config.scope, config.version);
return gulp.src(filePaths.input + "/**")
.pipe(gulp.dest(filePaths.output))
}
function deleteFiles() {
if ((configOpts && configOpts.deleteFiles)) {
let filesToDelete = [];
for (let f in configOpts.deleteFiles) {
const fileToDelete = filePaths.output + "/**/" + configOpts.deleteFiles[f];
filesToDelete.push(fileToDelete);
}
return del(filesToDelete);
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function retag() {
if (config.retag) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let x in config.retag) {
$(config.retag[x].search).each((i, item) => {item.tagName = config.retag[x].replace});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function sanitize() {
if (config.sanitize) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let x in config.sanitize) {
$(config.sanitize[x].search).each(function() {
const xReplace = config.sanitize[x].replace;
if (xReplace === "this") {
$(this).remove();
} else if (xReplace === "unwrap") {
$(this).replaceWith($.html(this.children));
} else {
$(this).removeAttr(xReplace);
}
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function classify() {
if (config.classify) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let x in config.classify) {
$(config.classify[x].search).each(function() {
$(this).removeAttr("class").addClass(config.classify[x].replace);
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function identify() {
if (config.identify) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let x in config.identify) {
$(config.identify[x].search).each(function(i, item) {
const id = config.identify[x].replace + "-" + (i + 1);
$(this).attr("id", id);
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function attributify() {
if (config.attributify) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let x in config.attributify) {
$(config.attributify[x].search).each(function() {
const attribute = utils.getProperty(config.attributify[x].replace);
const value = utils.getValue(config.attributify[x].replace)
$(this).attr(attribute, value);
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function append() {
if (config.append) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let x in config.append) {
$(config.append[x].where).each(function() {
$(this).append(config.append[x].what);
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function handleOPF() {
if (configOpts && configOpts.deleteFiles && configOpts.epub) {
return gulp.src(filePaths.opf, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let f in configOpts.deleteFiles) {
const fileToDelete = configOpts.deleteFiles[f];
$("manifest > item[href$='" + fileToDelete + "']").each(function() {
$("metadata").find("meta[content='" + $(this).attr("id") + "']").remove();
$("spine").find("itemref[idref='" + $(this).attr("id") + "']").remove();
$(this).remove();
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function handleDeleteNCX() {
if (configOpts && configOpts.deleteFiles && configOpts.epub) {
return gulp.src(filePaths.ncx, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let f in configOpts.deleteFiles) {
const fileToDelete = configOpts.deleteFiles[f];
$("navPoint").has("content[src*='" + fileToDelete + "']").each(function() {
$(this).remove();
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function identifyNCX() {
if ((configOpts && configOpts.epub) || args.force) {
return gulp.src(filePaths.ncx, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
$("navPoint").each(function(i, item) {
const id = "navPoint" + (i + 1);
$(this).attr("id", id);
});
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function handleNavDoc() {
if (configOpts && configOpts.deleteFiles && configOpts.epub) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
for (let f in configOpts.deleteFiles) {
const fileToDelete = configOpts.deleteFiles[f];
$("nav[epub\\:type='toc'] li, nav[epub\\:type='landmarks'] li, nav[epub\\:type='page-list'] li").has("a[href*='" + fileToDelete + "']").each(function() {
$(this).remove();
});
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function docOptions() {
if (configOpts && (configOpts.docTitle || configOpts.docLang)) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(cheerio({
run: function ($, file) {
if (configOpts.docTitle) {
const title = $("title");
const headings = $(configOpts.docTitle);
if (headings.length > 0) {
if (title.length > 0) {
title.text(headings.eq(0).text());
} else {
$("head").append("\n\t<title>" + headings.eq(0).text() + "</title>\n")
}
}
}
if (configOpts.docLang) {
$("html").attr("lang", configOpts.docLang);
if (file.path.indexOf(".xhtml") !== -1) {
$("html").attr("xml:lang", configOpts.docLang);
}
}
},
parserOptions: cheerioOpts
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function imageOptim() {
if ((configOpts && configOpts.imageOptim) || args.force) {
return gulp.src(filePaths.images, {base: "./"})
.pipe(imagemin())
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function minifyCSS() {
if ((configOpts && configOpts.minifyCSS) || args.force) {
return gulp.src(filePaths.css, {base: "./"})
.pipe(cleanCSS())
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function minifyJS() {
if ((configOpts && configOpts.minifyJS) || args.force) {
return gulp.src(filePaths.js, {base: "./"})
.pipe(terser({
ext: {
min: ".js"
},
noSource: true
}))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function prettyHTML() {
if ((configOpts && configOpts.prettyHTML) || args.force) {
return gulp.src(filePaths.html, {base: "./"})
.pipe(pretty.html(prettyOpts))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function prettyCSS() {
if ((configOpts && (configOpts.prettyCSS && !configOpts.minifyCSS)) || args.force) {
return gulp.src(filePaths.css, {base: "./"})
.pipe(pretty.css(prettyOpts))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
function prettyJS() {
if ((configOpts && (configOpts.prettyJS && !configOpts.minifyJS)) || args.force) {
return gulp.src(filePaths.js, {base: "./"})
.pipe(pretty.js(prettyOpts))
.pipe(gulp.dest("./"))
} else {
return Promise.resolve("Config doesn’t use this task, it was ignored.");
}
}
const handleNCX = gulp.series(handleDeleteNCX, identifyNCX);
const handleEPUB = gulp.parallel(handleOPF, handleNCX, handleNavDoc);
const handleHTML = gulp.series(docOptions, prettyHTML);
const handleCSS = gulp.series(prettyCSS, minifyCSS);
const handleJS = gulp.series(prettyJS, minifyJS);
const handleOptions = gulp.parallel(handleHTML, imageOptim, handleCSS, handleJS);
exports.init = init;
exports.deleteFiles = deleteFiles;
exports.retag = retag;
exports.sanitize = sanitize;
exports.classify = classify;
exports.identify = identify;
exports.attributify = attributify;
exports.append = append;
exports.handleEPUB = handleEPUB;
exports.identifyNCX = identifyNCX;
exports.imageOptim = imageOptim;
exports.minifyCSS = minifyCSS;
exports.minifyJS = minifyJS;
exports.prettyHTML = prettyHTML;
exports.prettyCSS = prettyCSS;
exports.prettyJS = prettyJS;
exports.handleOptions = handleOptions;
exports.default = gulp.series(init, deleteFiles, retag, sanitize, classify, identify, attributify, append, handleEPUB, handleOptions);