-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonml.js
131 lines (119 loc) · 3.36 KB
/
jsonml.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
#!/usr/bin/env node
/*
jsonml.js
Convert Convert HTML to JSONML or vice versa
Created: 2014-02-14
Copyright (c)2014 Roman Glebsky <[email protected]>
Distributed under The MIT License: http://github.com/Maqentaer/jsonml-cli/raw/master/LICENSE
*/
var fs = require('fs');
var request = require('request');
var html2jsonml = require('html2jsonml');
var json2html = require('./lib/json2html');
var cli = require('commander');
cli.helpInformationOld = cli.helpInformation;
cli.helpInformation = function() {
return '\n ' + cli.description() + '\n' + cli.helpInformationOld();
};
cli.optionHelpOld = cli.optionHelp;
cli.optionHelp = function() {
var help = cli.optionHelpOld().split('\n');
help.push(help.shift());
return help.join('\n');
};
cli
.description('Convert HTML to JSONML or vice versa')
.option('-o, --out <file>', 'output file')
.option('-i, --in <file>', 'input file')
.option('-u, --url <url>', 'input URL\n')
.option('-s, --space [string]', 'adds indentation, white space and line break\n')
.option('-n, --noProcInst', 'don\'t generate processing instructions')
.option('-l, --lowerTagNames', 'tag names in lower case')
.option('-L, --lowerAttrNames', 'attribute names in lower case')
.option('-a, --childrenInArray', 'children in separate array')
.option('-r, --requireAttr', 'HTML -> JSONML: add attributes object in any case')
.option('-e, --decodeEntities', 'HTML -> JSONML: decode Entities\n')
.version(require('./package.json').version, '-v, --version')
.on('--help', function(){
console.log(' Without -i, --in and -u, --url');
console.log(' input from stdin');
console.log('');
console.log(' Without -o, --out');
console.log(' output to stdout');
})
.parse(process.argv);
var showError = function(err) {
process.stderr.write('\n' + err.toString() + '\n');
process.exit(1);
};
var isJson = function(data) {
var re = /^\s*[\[\{]/;
return re.test(data);
};
var saveData = function(data) {
if (cli['out']) {
fs.writeFile(cli['out'], data, function(err) {
if (err) {
showError(err);
}
});
} else {
process.stdout.write(data);
}
};
var parseData = function(data) {
var options = {
noProcessingInstructions: cli.noProcInst,
lowerCaseTags: cli.lowerTagNames,
lowerCaseAttrNames: cli.lowerAttrNames,
childrenInArray: cli.childrenInArray,
requireAttributes: cli.requireAttr,
decodeEntities: cli.decodeEntities,
space: cli.space === true ? '\t' : cli.space
};
if (isJson(data)) {
var jsonML;
try {
jsonML = JSON.parse(data);
} catch(err) {
return showError(err);
}
json2html(jsonML, options, function(err, html) {
if (err) {
return showError(err);
}
saveData(html);
});
} else {
html2jsonml(data, options, function(err, jsonMl) {
if (err) {
return showError(err);
}
saveData(JSON.stringify(jsonMl, null, options.space));
});
}
};
if (cli['url']) {
request(cli['url'], function(err, res, body) {
if (err) {
return showError(err);
}
parseData(body);
});
} else if (cli['in']) {
fs.readFile(cli['in'], 'utf8', function(err, content) {
if (err) {
return showError(err);
}
parseData(content);
});
} else {
var stream = process.openStdin(), stdin = '';
stream.setEncoding('utf8');
stream.on('data', function (chunk) {
stdin += chunk;
});
stream.on('end', function() {
parseData(stdin);
});
}