Skip to content

Commit af1bee7

Browse files
committed
refactor bin/marked and man page.
1 parent 8873d66 commit af1bee7

File tree

2 files changed

+93
-61
lines changed

2 files changed

+93
-61
lines changed

bin/marked

+49-48
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* Marked CLI
5-
* Copyright (c) 2011-2012, Christopher Jeffrey (MIT License)
5+
* Copyright (c) 2011-2013, Christopher Jeffrey (MIT License)
66
*/
77

88
var fs = require('fs')
@@ -13,7 +13,7 @@ var fs = require('fs')
1313
* Man Page
1414
*/
1515

16-
var help = function() {
16+
function help() {
1717
var spawn = require('child_process').spawn;
1818

1919
var options = {
@@ -26,29 +26,28 @@ var help = function() {
2626
spawn('man',
2727
[__dirname + '/../man/marked.1'],
2828
options);
29-
};
29+
}
3030

3131
/**
3232
* Main
3333
*/
3434

35-
var main = function(argv) {
35+
function main(argv) {
3636
var files = []
3737
, options = {}
38-
, data = ''
3938
, input
4039
, output
4140
, arg
4241
, tokens;
4342

44-
var getarg = function() {
43+
function getarg() {
4544
var arg = argv.shift();
4645
arg = arg.split('=');
4746
if (arg.length > 1) {
4847
argv.unshift(arg.slice(1).join('='));
4948
}
5049
return arg[0];
51-
};
50+
}
5251

5352
while (argv.length) {
5453
arg = getarg();
@@ -65,65 +64,67 @@ var main = function(argv) {
6564
case '--tokens':
6665
tokens = true;
6766
break;
68-
case '--gfm':
69-
options.gfm = true;
70-
break;
71-
case '--tables':
72-
options.tables = true;
73-
break;
74-
case '--breaks':
75-
options.breaks = true;
76-
break;
77-
case '--sanitize':
78-
options.sanitize = true;
79-
break;
80-
case '--pedantic':
81-
options.pedantic = true;
82-
break;
8367
case '-h':
8468
case '--help':
8569
return help();
8670
default:
87-
files.push(arg);
71+
if (arg.indexOf('--') === 0) {
72+
arg = arg.substring(2);
73+
if (arg.indexOf('no-') === 0) {
74+
arg = arg.substring(3);
75+
options[arg] = false;
76+
} else {
77+
options[arg] = true;
78+
}
79+
} else {
80+
files.push(arg);
81+
}
8882
break;
8983
}
9084
}
9185

92-
if (!input) {
93-
if (files.length <= 2) {
94-
var stdin = process.stdin;
86+
function getData(callback) {
87+
var data = '';
9588

96-
stdin.setEncoding('utf8');
97-
stdin.resume();
89+
if (!input) {
90+
if (files.length <= 2) {
91+
var stdin = process.stdin;
9892

99-
stdin.on('data', function(text) {
100-
data += text;
101-
});
93+
stdin.setEncoding('utf8');
94+
stdin.resume();
10295

103-
stdin.on('end', write);
96+
stdin.on('data', function(text) {
97+
data += text;
98+
});
10499

105-
return;
100+
stdin.on('error', function(err) {
101+
return callback(err);
102+
});
103+
104+
stdin.on('end', function() {
105+
return callback(null, data);
106+
});
107+
108+
return;
109+
}
110+
input = files.pop();
106111
}
107-
input = files.pop();
108-
}
109112

110-
data = fs.readFileSync(input, 'utf8');
111-
write();
113+
return fs.readFile(input, 'utf8', callback);
114+
}
112115

113-
function write() {
114-
marked.setOptions(options);
116+
return getData(function(err, data) {
117+
if (err) throw err;
115118

116119
data = tokens
117-
? JSON.stringify(marked.lexer(data), null, 2)
118-
: marked(data);
120+
? JSON.stringify(marked.lexer(data, options), null, 2)
121+
: marked(data, options);
119122

120-
if (!output) {
121-
process.stdout.write(data + '\n');
122-
} else {
123-
fs.writeFileSync(output, data);
124-
}
125-
}
126-
};
123+
return !output
124+
? process.stdout.write(data + '\n')
125+
: fs.writeFileSync(output, data);
126+
});
127+
}
127128

128129
if (!module.parent) {
129130
process.title = 'marked';

man/marked.1

+44-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
.ds q \N'34'
22
.TH marked 1
3+
34
.SH NAME
45
marked \- a javascript markdown parser
6+
57
.SH SYNOPSIS
6-
.nf
7-
.B marked [\-o output] [\-i input] [\-th]
8-
.fi
8+
.B marked
9+
[\-o output] [\-i input] [\-\-help]
10+
[\-\-tokens] [\-\-pedantic] [\-\-gfm]
11+
[\-\-breaks] [\-\-tables] [\-\-sanitize]
12+
[\-\-silent] [input]
13+
914
.SH DESCRIPTION
1015
.B marked
1116
is a full-featured javascript markdown parser, built for speed. It also includes
1217
multiple GFM features.
18+
19+
.SH EXAMPLES
20+
.TP
21+
cat in.md | marked > out.html
22+
.TP
23+
echo "hello *world*" | marked
24+
.TP
25+
marked -o out.html in.md --gfm
26+
.TP
27+
marked --output="hello world.html" -i in.md --no-breaks
28+
1329
.SH OPTIONS
1430
.TP
1531
.BI \-o,\ \-\-output\ [output]
@@ -29,21 +45,36 @@ markdown bugs.
2945
.BI \-\-gfm
3046
Enable github flavored markdown.
3147
.TP
32-
.BI \-\-sanitize
33-
Sanitize output. Ignore any HTML input.
48+
.BI \-\-breaks
49+
Enable GFM line breaks. Only works with the gfm option.
3450
.TP
35-
.BI \-h,\ \-\-help
36-
Display help information.
37-
.SH EXAMPLES
51+
.BI \-\-tables
52+
Enable GFM tables. Only works with the gfm option.
3853
.TP
39-
cat in.md | marked > out.html
54+
.BI \-\-sanitize
55+
Sanitize output. Ignore any HTML input.
4056
.TP
41-
echo "hello *world*" | marked
57+
.BI \-\-no\-sanitize,\ \-no-etc...
58+
The inverse of any of the marked options above.
4259
.TP
43-
marked -o out.html in.md
60+
.BI \-\-silent
61+
Silence error output.
4462
.TP
45-
marked --output="hello world.html" -i in.md
63+
.BI \-h,\ \-\-help
64+
Display help information.
65+
66+
.SH CONFIGURATION
67+
For configuring and running programmatically.
68+
69+
.B Example
70+
71+
require('marked')('*foo*', { gfm: true });
72+
4673
.SH BUGS
4774
Please report any bugs to https://github.com/chjj/marked.
75+
4876
.SH LICENSE
49-
Copyright (c) 2011-2012, Christopher Jeffrey (MIT License)
77+
Copyright (c) 2011-2013, Christopher Jeffrey (MIT License)
78+
79+
.SH SEE ALSO
80+
.BR markdown(1)

0 commit comments

Comments
 (0)