/
2html.js
72 lines (68 loc) · 2.36 KB
/
2html.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
exports.main = function() {
var sys = require('sys'),
fs = require('fs'),
args = process.argv.slice(2),
arg,
options = {};
while(args.length) {
arg = args.shift();
switch (arg) {
case '-h':
case '--help':
sys.puts([
'Usage: ohl2html [options]',
'',
'Options:',
' -i, --input : pecifies filename to read the input source, if omit use STDIN',
' -o, --output : specifies filename to write the output, if omit use STDOUT',
' -h, --help : Output help information'
].join('\n'));
process.exit(1);
break;
case '-i':
case '--input':
options.input = args.shift();
break;
case '-o':
case '--output':
options.output = args.shift();
break;
}
}
(options.input ?
// if input file
function(inputFn) {
fs.readFile(options.input, 'utf8', function(err, input){
if (err) throw err;
inputFn(input);
});
} :
// if STDIN
function(inputFn) {
var input = '';
process.openStdin()
.on('data', function(s) { input += s })
.on('end', function() { inputFn(input) });
})(function(input){
try {
var shmakowiki = require('shmakowiki'),
result = shmakowiki.ShmakoWikiToHtml.match(
shmakowiki.ShmakoWiki.matchAll(input, 'topLevel'),
'topLevel'
) + '\n';
options.output ?
fs.writeFile(options.output, result, 'utf8', function(err) {
if (err) throw err;
sys.error(' create : ' + options.output);
}) :
process.stdout.write(result);
} catch (e) {
e.errorPos != undefined &&
sys.error(
input.slice(0, e.errorPos) +
"\n--- Parse error ->" +
input.slice(e.errorPos) + '\n');
throw e
}
});
};