UNPKG

2.21 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 'add-break-around-tags': getOptAsArray(argv['add-break-around-tags']),
19 'add-remove-attributes': getOptAsArray(argv['add-remove-attributes']),
20 'add-remove-tags': getOptAsArray(argv['add-remove-tags'])
21 };
22
23function getOptAsArray(opt) {
24 if (opt === undefined) {
25 return undefined;
26 }
27
28 if (Array.isArray(opt)) {
29 return opt.map(function (o) {
30 return o.split(',');
31 }).reduce(function (prev, curr) {
32 return prev.concat(curr);
33 });
34 }
35
36 return opt.split(',');
37}
38
39function getOptAsBool(opt) {
40 if (opt === undefined) {
41 return undefined;
42 }
43
44 return opt === true || opt === 'true';
45}
46
47function read(filename, callback) {
48 if (filename) {
49 return fs.readFile(filename, function (err, data) {
50 if (err) {
51 throw err;
52 }
53
54 callback(data);
55 });
56 }
57
58 process.stdin.on('data', function (data) {
59 callback(data);
60 });
61}
62
63function write(html, filename) {
64 if (filename) {
65 return fs.writeFile(filename, html, function (err) {
66 if (err) {
67 throw err;
68 }
69 });
70 }
71
72 process.stdout.write(html + '\n');
73}
74
75read(filename, function (data) {
76 cleaner.clean(data, options, function (html) {
77 if (filename && inPlace) {
78 return write(html, filename);
79 }
80
81 write(html);
82 });
83});