1 | #!/usr/bin/env node
|
2 |
|
3 | /**
|
4 | * Marked CLI
|
5 | * Copyright (c) 2011-2013, Christopher Jeffrey (MIT License)
|
6 | */
|
7 |
|
8 | var fs = require('fs'),
|
9 | path = require('path'),
|
10 | marked = require('../');
|
11 |
|
12 | /**
|
13 | * Man Page
|
14 | */
|
15 |
|
16 | function help() {
|
17 | var spawn = require('child_process').spawn;
|
18 |
|
19 | var options = {
|
20 | cwd: process.cwd(),
|
21 | env: process.env,
|
22 | setsid: false,
|
23 | stdio: 'inherit'
|
24 | };
|
25 |
|
26 | spawn('man', [path.resolve(__dirname, '/../man/marked.1')], options)
|
27 | .on('error', function() {
|
28 | fs.readFile(path.resolve(__dirname, '/../man/marked.1.txt'), 'utf8', function(err, data) {
|
29 | if (err) throw err;
|
30 | console.log(data);
|
31 | });
|
32 | });
|
33 | }
|
34 |
|
35 | /**
|
36 | * Main
|
37 | */
|
38 |
|
39 | function main(argv, callback) {
|
40 | var files = [],
|
41 | options = {},
|
42 | input,
|
43 | output,
|
44 | string,
|
45 | arg,
|
46 | tokens,
|
47 | opt;
|
48 |
|
49 | function getarg() {
|
50 | var arg = argv.shift();
|
51 |
|
52 | if (arg.indexOf('--') === 0) {
|
53 | // e.g. --opt
|
54 | arg = arg.split('=');
|
55 | if (arg.length > 1) {
|
56 | // e.g. --opt=val
|
57 | argv.unshift(arg.slice(1).join('='));
|
58 | }
|
59 | arg = arg[0];
|
60 | } else if (arg[0] === '-') {
|
61 | if (arg.length > 2) {
|
62 | // e.g. -abc
|
63 | argv = arg.substring(1).split('').map(function(ch) {
|
64 | return '-' + ch;
|
65 | }).concat(argv);
|
66 | arg = argv.shift();
|
67 | } else {
|
68 | // e.g. -a
|
69 | }
|
70 | } else {
|
71 | // e.g. foo
|
72 | }
|
73 |
|
74 | return arg;
|
75 | }
|
76 |
|
77 | while (argv.length) {
|
78 | arg = getarg();
|
79 | switch (arg) {
|
80 | case '--test':
|
81 | return require('../test').main(process.argv.slice());
|
82 | case '-o':
|
83 | case '--output':
|
84 | output = argv.shift();
|
85 | break;
|
86 | case '-i':
|
87 | case '--input':
|
88 | input = argv.shift();
|
89 | break;
|
90 | case '-s':
|
91 | case '--string':
|
92 | string = argv.shift();
|
93 | break;
|
94 | case '-t':
|
95 | case '--tokens':
|
96 | tokens = true;
|
97 | break;
|
98 | case '-h':
|
99 | case '--help':
|
100 | return help();
|
101 | default:
|
102 | if (arg.indexOf('--') === 0) {
|
103 | opt = camelize(arg.replace(/^--(no-)?/, ''));
|
104 | if (!marked.defaults.hasOwnProperty(opt)) {
|
105 | continue;
|
106 | }
|
107 | if (arg.indexOf('--no-') === 0) {
|
108 | options[opt] = typeof marked.defaults[opt] !== 'boolean'
|
109 | ? null
|
110 | : false;
|
111 | } else {
|
112 | options[opt] = typeof marked.defaults[opt] !== 'boolean'
|
113 | ? argv.shift()
|
114 | : true;
|
115 | }
|
116 | } else {
|
117 | files.push(arg);
|
118 | }
|
119 | break;
|
120 | }
|
121 | }
|
122 |
|
123 | function getData(callback) {
|
124 | if (!input) {
|
125 | if (files.length <= 2) {
|
126 | if (string) {
|
127 | return callback(null, string);
|
128 | }
|
129 | return getStdin(callback);
|
130 | }
|
131 | input = files.pop();
|
132 | }
|
133 | return fs.readFile(input, 'utf8', callback);
|
134 | }
|
135 |
|
136 | return getData(function(err, data) {
|
137 | if (err) return callback(err);
|
138 |
|
139 | data = tokens
|
140 | ? JSON.stringify(marked.lexer(data, options), null, 2)
|
141 | : marked(data, options);
|
142 |
|
143 | if (!output) {
|
144 | process.stdout.write(data + '\n');
|
145 | return callback();
|
146 | }
|
147 |
|
148 | return fs.writeFile(output, data, callback);
|
149 | });
|
150 | }
|
151 |
|
152 | /**
|
153 | * Helpers
|
154 | */
|
155 |
|
156 | function getStdin(callback) {
|
157 | var stdin = process.stdin,
|
158 | buff = '';
|
159 |
|
160 | stdin.setEncoding('utf8');
|
161 |
|
162 | stdin.on('data', function(data) {
|
163 | buff += data;
|
164 | });
|
165 |
|
166 | stdin.on('error', function(err) {
|
167 | return callback(err);
|
168 | });
|
169 |
|
170 | stdin.on('end', function() {
|
171 | return callback(null, buff);
|
172 | });
|
173 |
|
174 | try {
|
175 | stdin.resume();
|
176 | } catch (e) {
|
177 | callback(e);
|
178 | }
|
179 | }
|
180 |
|
181 | function camelize(text) {
|
182 | return text.replace(/(\w)-(\w)/g, function(_, a, b) {
|
183 | return a + b.toUpperCase();
|
184 | });
|
185 | }
|
186 |
|
187 | function handleError(err) {
|
188 | if (err.code === 'ENOENT') {
|
189 | console.error(`marked: output to ${err.path}: No such directory`);
|
190 | return process.exit(1);
|
191 | }
|
192 | throw err;
|
193 | }
|
194 |
|
195 | /**
|
196 | * Expose / Entry Point
|
197 | */
|
198 |
|
199 | if (!module.parent) {
|
200 | process.title = 'marked';
|
201 | main(process.argv.slice(), function(err, code) {
|
202 | if (err) return handleError(err);
|
203 | return process.exit(code || 0);
|
204 | });
|
205 | } else {
|
206 | module.exports = main;
|
207 | }
|