-
Notifications
You must be signed in to change notification settings - Fork 191
/
parser.ts
90 lines (71 loc) · 2.48 KB
/
parser.ts
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
import { parse } from "handlebars/compiler/base";
import * as syntax from "./syntax";
import EventedTokenizer from "simple-html-tokenizer/evented-tokenizer";
import EntityParser from "simple-html-tokenizer/entity-parser";
import namedCharRefs from "simple-html-tokenizer/html5-named-char-refs";
import handlebarsNodeVisitors from "./parser/handlebars-node-visitors";
import tokenizerEventHandlers from "./parser/tokenizer-event-handlers";
export function preprocess(html, options?) {
let ast = (typeof html === 'object') ? html : parse(html);
let combined = new Parser(html, options).acceptNode(ast);
if (options && options.plugins && options.plugins.ast) {
for (let i = 0, l = options.plugins.ast.length; i < l; i++) {
let plugin = new options.plugins.ast[i](options);
plugin.syntax = syntax;
combined = plugin.transform(combined);
}
}
return combined;
}
export default preprocess;
const entityParser = new EntityParser(namedCharRefs);
export function Parser(source, options) {
this.options = options || {};
this.elementStack = [];
this.tokenizer = new EventedTokenizer(this, entityParser);
this.currentNode = null;
this.currentAttribute = null;
if (typeof source === 'string') {
this.source = source.split(/(?:\r\n?|\n)/g);
}
}
for (let key in handlebarsNodeVisitors) {
Parser.prototype[key] = handlebarsNodeVisitors[key];
}
for (let key in tokenizerEventHandlers) {
Parser.prototype[key] = tokenizerEventHandlers[key];
}
Parser.prototype.acceptNode = function(node) {
return this[node.type](node);
};
Parser.prototype.currentElement = function() {
return this.elementStack[this.elementStack.length - 1];
};
Parser.prototype.sourceForMustache = function(mustache) {
let firstLine = mustache.loc.start.line - 1;
let lastLine = mustache.loc.end.line - 1;
let currentLine = firstLine - 1;
let firstColumn = mustache.loc.start.column + 2;
let lastColumn = mustache.loc.end.column - 2;
let string = [];
let line;
if (!this.source) {
return '{{' + mustache.path.id.original + '}}';
}
while (currentLine < lastLine) {
currentLine++;
line = this.source[currentLine];
if (currentLine === firstLine) {
if (firstLine === lastLine) {
string.push(line.slice(firstColumn, lastColumn));
} else {
string.push(line.slice(firstColumn));
}
} else if (currentLine === lastLine) {
string.push(line.slice(0, lastColumn));
} else {
string.push(line);
}
}
return string.join('\n');
};