This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
/
parser.js
90 lines (71 loc) · 2.5 KB
/
parser.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
import { parse } from "./handlebars/compiler/base";
import * as syntax from "../htmlbars-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) {
var ast = (typeof html === 'object') ? html : parse(html);
var combined = new Parser(html, options).acceptNode(ast);
if (options && options.plugins && options.plugins.ast) {
for (var i = 0, l = options.plugins.ast.length; i < l; i++) {
var 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) {
var firstLine = mustache.loc.start.line - 1;
var lastLine = mustache.loc.end.line - 1;
var currentLine = firstLine - 1;
var firstColumn = mustache.loc.start.column + 2;
var lastColumn = mustache.loc.end.column - 2;
var string = [];
var 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');
};