forked from dciccale/node-htmlprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (59 loc) · 1.79 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
/*
* node-htmlprocessor
* https://github.com/dciccale/node-htmlprocessor
*
* Copyright (c) 2013-2016 Denis Ciccale (@dciccale)
* Licensed under the MIT license.
* https://github.com/dciccale/node-htmlprocessor/blob/master/LICENSE-MIT
*/
'use strict';
var path = require('path');
var fs = require('fs');
var HTMLProcessor = require('./lib/htmlprocessor');
var utils = require('./lib/utils');
module.exports = function (files, options) {
var html;
if (options && options.customBlockTypes && options.customBlockTypes.length) {
options.customBlockTypes = options.customBlockTypes.map(function (processor) {
return path.resolve(processor);
});
}
// create output directory if needed
if (files.dest) {
if (path.extname(files.dest)) {
utils.mkdir(path.dirname(files.dest));
} else {
utils.mkdir(files.dest);
}
}
// create options.list directory if needed
if (options && options.list) {
utils.mkdir(path.dirname(options.list));
}
html = new HTMLProcessor(options);
files.src.forEach(function (filePath) {
var content = html.process(filePath);
var dest = getOutputPath(filePath);
fs.writeFileSync(dest, content);
console.log('File', '"' + dest + '"', 'created.');
if (options && options.list) {
console.log('File', '"' + options.list + '"', 'created.');
}
});
function getOutputPath(filePath) {
var dest = files.dest;
var ext;
if (!dest) {
dest = getFileName(filePath, '.processed');
} else if (!path.extname(dest)) {
dest = path.join(dest, getFileName(filePath));
}
return dest;
}
function getFileName(filePath, postfix) {
var ext = path.extname(filePath);
return path.basename(filePath, ext) + (postfix || '') + ext;
}
// return processor instance
return html;
};