-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (102 loc) · 2.58 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// #!/usr/bin/env node
var encoding = 'utf-8';
var data;
const process_html = (html) => {
// const html2md = new TurndownService(options.mdconfig);
var md = require('./lib/html2md')(options.mdconfig).turndown(html);
if (options.show_html) {
console.log(html);
} else {
if (options.save_html) { let html_out = options.page.html }
if (options.save_md) { let md_out = options.page.md }
// console.log(md);
render_md(md);
}
return 0;
}
const render_md = (md) => {
if (options.pretty === 'yes' ) {
const marked = require('marked');
const TerminalRenderer = require('marked-terminal');
marked.setOptions({
renderer: new TerminalRenderer()
});
console.log( marked(md));
} else {
console.log(md);
}
}
// BOILERPLATE CLI FUNCTIONS
// =====================================================
// PROCESS ENV
const parse_options = () => {
runtime_options = process.env.MANDOWN_ENV;
options = Object.assign({}, defaults,JSON.parse(runtime_options));
if (options.debug) {
console.log('process env', process.env );
console.log( 'current options', options );
}
};
// HANDLE STDIN AND PROCESS
var read_file_sync = (filepath) => {
const fs = require('fs');
console.log('Filepath: ', filepath);
return fs.readFile(filepath,
function read(err, filedata) {
if (err) {
console.log(filedata ,err);
throw err;
}
var filedata= Buffer.from(filedata, encoding).toString('ascii');
return filedata;
}
)
}
var process_file = (filename) => {
const filepath = Buffer.from(args,'utf8').toString('ascii');
var filedata = read_file_sync(filepath);
process_html(filedata);
};
var process_data = () => {
const content = Buffer.from(data,'utf8').toString('ascii');
process_html(content);
};
// MAIN
var encoding = 'utf-8';
var data;
let options = {};
const defaults = {
page: "manpage",
pretty: false,
debug: false,
mdconfig: {
codeBlockStyle: 'fenced',
linkStyle: 'inlined',
linkReferenceStyle: 'full',
headingStyle: 'atx',
strongDelimiter: '**',
emDelimiter: '_',
fence: '```',
bulletListMarker: '*',
hr: '---'
}
}
parse_options();
if (process.stdin.isTTY) {
// INPUT AS ARGUMENT
console.log('reading from file' + process.argv[2] );
process_file(process.argv[2]);
} else {
// PIPED INPUT
data = '';
process.stdin
.setEncoding(encoding)
.on('readable', function() {
var chunk;
while ( chunk = process.stdin.read() ){ data += chunk; }
})
.on('end', function () {
data = data.replace(/\n$/, '');
process_data();
});
}