2
2
3
3
/**
4
4
* Marked CLI
5
- * Copyright (c) 2011-2012 , Christopher Jeffrey (MIT License)
5
+ * Copyright (c) 2011-2013 , Christopher Jeffrey (MIT License)
6
6
*/
7
7
8
8
var fs = require ( 'fs' )
@@ -13,7 +13,7 @@ var fs = require('fs')
13
13
* Man Page
14
14
*/
15
15
16
- var help = function ( ) {
16
+ function help ( ) {
17
17
var spawn = require ( 'child_process' ) . spawn ;
18
18
19
19
var options = {
@@ -26,29 +26,28 @@ var help = function() {
26
26
spawn ( 'man' ,
27
27
[ __dirname + '/../man/marked.1' ] ,
28
28
options ) ;
29
- } ;
29
+ }
30
30
31
31
/**
32
32
* Main
33
33
*/
34
34
35
- var main = function ( argv ) {
35
+ function main ( argv ) {
36
36
var files = [ ]
37
37
, options = { }
38
- , data = ''
39
38
, input
40
39
, output
41
40
, arg
42
41
, tokens ;
43
42
44
- var getarg = function ( ) {
43
+ function getarg ( ) {
45
44
var arg = argv . shift ( ) ;
46
45
arg = arg . split ( '=' ) ;
47
46
if ( arg . length > 1 ) {
48
47
argv . unshift ( arg . slice ( 1 ) . join ( '=' ) ) ;
49
48
}
50
49
return arg [ 0 ] ;
51
- } ;
50
+ }
52
51
53
52
while ( argv . length ) {
54
53
arg = getarg ( ) ;
@@ -65,65 +64,67 @@ var main = function(argv) {
65
64
case '--tokens' :
66
65
tokens = true ;
67
66
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 ;
83
67
case '-h' :
84
68
case '--help' :
85
69
return help ( ) ;
86
70
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
+ }
88
82
break ;
89
83
}
90
84
}
91
85
92
- if ( ! input ) {
93
- if ( files . length <= 2 ) {
94
- var stdin = process . stdin ;
86
+ function getData ( callback ) {
87
+ var data = '' ;
95
88
96
- stdin . setEncoding ( 'utf8' ) ;
97
- stdin . resume ( ) ;
89
+ if ( ! input ) {
90
+ if ( files . length <= 2 ) {
91
+ var stdin = process . stdin ;
98
92
99
- stdin . on ( 'data' , function ( text ) {
100
- data += text ;
101
- } ) ;
93
+ stdin . setEncoding ( 'utf8' ) ;
94
+ stdin . resume ( ) ;
102
95
103
- stdin . on ( 'end' , write ) ;
96
+ stdin . on ( 'data' , function ( text ) {
97
+ data += text ;
98
+ } ) ;
104
99
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 ( ) ;
106
111
}
107
- input = files . pop ( ) ;
108
- }
109
112
110
- data = fs . readFileSync ( input , 'utf8' ) ;
111
- write ( ) ;
113
+ return fs . readFile ( input , 'utf8' , callback ) ;
114
+ }
112
115
113
- function write ( ) {
114
- marked . setOptions ( options ) ;
116
+ return getData ( function ( err , data ) {
117
+ if ( err ) throw err ;
115
118
116
119
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 ) ;
119
122
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
+ }
127
128
128
129
if ( ! module . parent ) {
129
130
process . title = 'marked' ;
0 commit comments