1 | 'use strict';
|
---|
2 |
|
---|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
---|
4 |
|
---|
5 | var fs = _interopDefault(require('fs'));
|
---|
6 | var argparse = _interopDefault(require('argparse'));
|
---|
7 | var index = require('./index.js');
|
---|
8 | require('autolinker');
|
---|
9 | var linkify = require('./linkify.js');
|
---|
10 |
|
---|
11 | var version = "2.0.1";
|
---|
12 |
|
---|
13 | ////////////////////////////////////////////////////////////////////////////////
|
---|
14 |
|
---|
15 | var cli = new argparse.ArgumentParser({
|
---|
16 | prog: 'remarkable',
|
---|
17 | version: version,
|
---|
18 | addHelp: true
|
---|
19 | });
|
---|
20 |
|
---|
21 | cli.addArgument([ 'file' ], {
|
---|
22 | help: 'File to read',
|
---|
23 | nargs: '?',
|
---|
24 | defaultValue: '-'
|
---|
25 | });
|
---|
26 |
|
---|
27 | var options = cli.parseArgs();
|
---|
28 |
|
---|
29 |
|
---|
30 | function readFile(filename, encoding, callback) {
|
---|
31 | if (options.file === '-') {
|
---|
32 | var chunks = [];
|
---|
33 |
|
---|
34 | // read from stdin
|
---|
35 | process.stdin.on('data', function(chunk) {
|
---|
36 | chunks.push(chunk);
|
---|
37 | });
|
---|
38 |
|
---|
39 | process.stdin.on('end', function() {
|
---|
40 | return callback(null, Buffer.concat(chunks).toString(encoding));
|
---|
41 | });
|
---|
42 | } else {
|
---|
43 | fs.readFile(filename, encoding, callback);
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | ////////////////////////////////////////////////////////////////////////////////
|
---|
49 |
|
---|
50 | readFile(options.file, 'utf8', function (err, input) {
|
---|
51 | var output, md;
|
---|
52 |
|
---|
53 | if (err) {
|
---|
54 | if (err.code === 'ENOENT') {
|
---|
55 | console.error('File not found: ' + options.file);
|
---|
56 | process.exit(2);
|
---|
57 | }
|
---|
58 |
|
---|
59 | console.error(err.stack || err.message || String(err));
|
---|
60 | process.exit(1);
|
---|
61 | }
|
---|
62 |
|
---|
63 | md = new index.Remarkable('full', {
|
---|
64 | html: true,
|
---|
65 | xhtmlOut: true,
|
---|
66 | typographer: true,
|
---|
67 | }).use(linkify.linkify);
|
---|
68 |
|
---|
69 | try {
|
---|
70 | output = md.render(input);
|
---|
71 | } catch (e) {
|
---|
72 | console.error(e.stack || e.message || String(e));
|
---|
73 | process.exit(1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | process.stdout.write(output);
|
---|
77 | process.exit(0);
|
---|
78 | });
|
---|