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
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (51 loc) · 1.39 KB
/
index.js
File metadata and controls
59 lines (51 loc) · 1.39 KB
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
var ast = require('mkast')
, Parser = require('./lib/parser');
/**
* Execute processing instructions found in the AST.
*
* Instructions are removed from the AST by default, use `preserve` to keep
* them in the output.
*
* When no `input` and no `output` are specified the parser stream
* is returned and `cb` is ignored.
*
* @function pi
* @param {Object} [opts] processing options.
* @param {Function} [cb] callback function.
*
* @option {Readable} [input] input stream.
* @option {Writable} [output] output stream.
* @option {Object} [grammar] grammar macro functions.
* @option {Boolean=false} [preserve] keep processing instructions in the AST.
* @option {Boolean=false} [safe] disable command and code execution.
*
* @returns an output stream.
*/
function pi(opts, cb) {
opts = opts || {};
opts.input = opts.input;
opts.output = opts.output;
var serialize = ast.stringify()
, options = {
grammar: opts.grammar,
serializer: serialize,
preserve: opts.preserve,
safe: opts.safe
}
, parser = new Parser(options);
if(!opts.input && !opts.output) {
return parser;
}
ast.parser(opts.input)
.pipe(parser)
.pipe(serialize)
.pipe(opts.output);
if(cb) {
parser.once('error', cb)
opts.output
.once('error', cb)
.once('finish', cb);
}
return opts.output;
}
module.exports = pi;