forked from dave-kennedy/clean-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
executable file
·94 lines (78 loc) · 2.36 KB
/
cmd.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
#!/usr/bin/env node
var cleaner = require('./index.js'),
fs = require('fs'),
parseArgs = require('minimist'),
argv = parseArgs(process.argv.slice(2)),
filename = argv['_'][0],
inPlace = getOptAsBool(argv['in-place']),
options = {
'break-around-comments': getOptAsBool(argv['break-around-comments']),
'break-around-tags': getOptAsArray(argv['break-around-tags']),
'indent': argv['indent'],
'remove-attributes': getOptAsArray(argv['remove-attributes']),
'remove-comments': getOptAsBool(argv['remove-comments']),
'remove-empty-tags': getOptAsArray(argv['remove-empty-tags']),
'remove-tags': getOptAsArray(argv['remove-tags']),
'replace-nbsp': getOptAsBool(argv['replace-nbsp']),
'wrap': getOptAsInt(argv['wrap']),
'add-break-around-tags': getOptAsArray(argv['add-break-around-tags']),
'add-remove-attributes': getOptAsArray(argv['add-remove-attributes']),
'add-remove-tags': getOptAsArray(argv['add-remove-tags'])
};
function getOptAsArray(opt) {
if (opt === undefined) {
return undefined;
}
if (Array.isArray(opt)) {
return opt.map(function (o) {
return o.split(',');
}).reduce(function (prev, curr) {
return prev.concat(curr);
});
}
return opt.split(',');
}
function getOptAsBool(opt) {
if (opt === undefined) {
return undefined;
}
return opt === true || opt === 'true';
}
function getOptAsInt(opt) {
if (opt === undefined) {
return undefined;
}
var val = parseInt(opt);
return isNaN(val) ? undefined : val;
}
function read(filename, callback) {
if (filename) {
return fs.readFile(filename, function (err, data) {
if (err) {
throw err;
}
callback(data);
});
}
process.stdin.on('data', function (data) {
callback(data);
});
}
function write(html, filename) {
if (filename) {
return fs.writeFile(filename, html, function (err) {
if (err) {
throw err;
}
});
}
process.stdout.write(html + '\n');
}
read(filename, function (data) {
cleaner.clean(data, options, function (html) {
if (filename && inPlace) {
return write(html, filename);
}
write(html);
});
});