-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
70 lines (59 loc) · 1.83 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
'use strict';
const stripBom = require('strip-bom-string');
const Snapdragon = require('snapdragon');
const tags = require('./lib/tags');
const args = require('./lib/args');
class Converter extends Snapdragon {
constructor(options) {
super(options);
this.stash = this.options.stash || [];
this.stack = this.options.stack || [];
this.variables = [];
}
parse(str, options) {
const opts = Object.assign({}, this.options, options);
this.parser.use(tags.parsers(opts, this));
return super.parse(stripBom(str), opts);
}
compile(ast, options) {
const opts = Object.assign({}, this.options, options);
opts.stack = this.stack;
opts.stash = this.stash;
this.compiler.use(tags.compilers(opts, this));
this.compiler.tagNames = [];
return super.compile(ast, opts);
}
convert(str, options) {
const opts = Object.assign({}, this.options, options);
const ast = this.parse(str, opts);
this.variables = [];
const res = this.compile(ast, opts);
return res.output;
}
parseArgs(str, options) {
const opts = Object.assign({}, this.options, options);
const parser = new Snapdragon.Parser(opts);
parser.use(args.parsers(opts, this));
return parser.parse(str, opts);
}
compileArgs(ast, options) {
const opts = Object.assign({}, this.options, options);
const compiler = new Snapdragon.Compiler(opts);
compiler.use(args.compilers(opts, this));
const res = compiler.compile(ast, opts);
return res.output;
}
convertArgs(str, options) {
const opts = Object.assign({}, this.options, options);
const ast = this.parseArgs(str, opts);
return this.compileArgs(ast, opts);
}
static convert(str, options) {
const converter = new this(options);
return converter.convert(str);
}
}
/**
* Expose `convert`
*/
module.exports = Converter;