UNPKG

2.42 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var cleaner = require('./index.js'),
4 fs = require('fs'),
5 parseArgs = require('minimist'),
6 argv = parseArgs(process.argv.slice(2)),
7 filename = argv['_'][0],
8 inPlace = getOptAsBool(argv['in-place']),
9 options = {
10 'break-around-comments': getOptAsBool(argv['break-around-comments']),
11 'break-around-tags': getOptAsArray(argv['break-around-tags']),
12 'indent': argv['indent'],
13 'remove-attributes': getOptAsArray(argv['remove-attributes']),
14 'remove-comments': getOptAsBool(argv['remove-comments']),
15 'remove-empty-tags': getOptAsArray(argv['remove-empty-tags']),
16 'remove-tags': getOptAsArray(argv['remove-tags']),
17 'replace-nbsp': getOptAsBool(argv['replace-nbsp']),
18 'wrap': getOptAsInt(argv['wrap']),
19 'add-break-around-tags': getOptAsArray(argv['add-break-around-tags']),
20 'add-remove-attributes': getOptAsArray(argv['add-remove-attributes']),
21 'add-remove-tags': getOptAsArray(argv['add-remove-tags'])
22 };
23
24function getOptAsArray(opt) {
25 if (opt === undefined) {
26 return undefined;
27 }
28
29 if (Array.isArray(opt)) {
30 return opt.map(function (o) {
31 return o.split(',');
32 }).reduce(function (prev, curr) {
33 return prev.concat(curr);
34 });
35 }
36
37 return opt.split(',');
38}
39
40function getOptAsBool(opt) {
41 if (opt === undefined) {
42 return undefined;
43 }
44
45 return opt === true || opt === 'true';
46}
47
48function getOptAsInt(opt) {
49 if (opt === undefined) {
50 return undefined;
51 }
52
53 var val = parseInt(opt);
54
55 return isNaN(val) ? undefined : val;
56}
57
58function read(filename, callback) {
59 if (filename) {
60 return fs.readFile(filename, function (err, data) {
61 if (err) {
62 throw err;
63 }
64
65 callback(data);
66 });
67 }
68
69 process.stdin.on('data', function (data) {
70 callback(data);
71 });
72}
73
74function write(html, filename) {
75 if (filename) {
76 return fs.writeFile(filename, html, function (err) {
77 if (err) {
78 throw err;
79 }
80 });
81 }
82
83 process.stdout.write(html + '\n');
84}
85
86read(filename, function (data) {
87 cleaner.clean(data, options, function (html) {
88 if (filename && inPlace) {
89 return write(html, filename);
90 }
91
92 write(html);
93 });
94});