This repository was archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (107 loc) · 2.75 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
119
120
121
122
123
124
125
126
127
var through = require('through3')
, ast = require('mkast')
, commonmark = ast.commonmark
, deserialize = ast.deserialize
, Node = ast.Node
, types = {
markdown: './lib/markdown',
yaml: './lib/yaml',
json: './lib/json',
text: './lib/text',
man: './lib/man',
xml: commonmark.XmlRenderer,
html: commonmark.HtmlRenderer
}
, NOOP = 'noop';
function load(type) {
var info = types[type];
if(info instanceof Function) {
return info;
}
return require(info);
}
function Render(opts) {
opts = opts || {};
this.renderer = opts.renderer;
}
function render(chunk, encoding, cb) {
if(!Node.is(chunk, Node.EOF)) {
/*
* this caters for when a stream is being piped directly and
* has not been deserialized to a real AST node
*/
if(!chunk._type) {
chunk = Node.deserialize(chunk);
}
this.push(this.renderer.render(chunk));
}
cb();
}
var RenderStream = through.transform(render, {ctor: Render});
/**
* Print via a renderer to an output stream.
*
* @function out
* @param {Object} [opts] processing options.
* @param {Function} [cb] callback function.
*
* @option {String} [type] output type.
* @option {Readable=process.stdin} [input] input stream.
* @option {Writable=process.stdout} [output] output stream.
* @option {Object} [render] renderer options.
*
* @returns an output stream.
*/
function out(opts, cb) {
opts = opts || {};
opts.input = opts.input;
opts.output = opts.output;
opts.type = opts.type || 'markdown';
opts.render = opts.render || {};
function writer(doc) {
opts.output.write(renderer.render(doc));
}
if(opts.type === NOOP) {
opts.input.pipe(opts.output);
}else{
if(!types[opts.type]) {
return cb(new Error('unknown output type: ' + opts.type));
}
var Type = load(opts.type)
, renderer = new Type(opts.render)
, deserializer
, render = new RenderStream({renderer: renderer});
deserializer = deserialize(opts.input);
if(opts.cli !== true) {
return render;
}
// handle output stream
if(opts.output) {
render.pipe(opts.output);
}
// ensure callback is called when the input stream ends
if(opts.input && opts.output && opts.output !== process.stdout) {
opts.input.once('end', function() {
// close the output stream when the input ends
opts.output.end();
})
}
deserializer
.on('eof', writer)
.on('fragment', writer);
}
if(cb) {
opts.output
.once('error', cb)
.once('finish', cb);
}
return opts.output;
}
// supported renderer types
out.types = types;
out.NOOP = NOOP;
// assign type constants
Object.keys(types).forEach(function(k) {
out[k.toUpperCase()] = k;
})
module.exports = out;