filters: all

This commit is contained in:
harttle
2016-06-18 23:18:46 +08:00
parent b478c798fd
commit 4595e2872e
21 changed files with 627 additions and 211 deletions
+24 -16
View File
@@ -12,6 +12,7 @@ function parse(html) {
var _count = 0;
while ((result = syntax.exec(html)) !== null) {
// passed html fragments
if (result.index > idx) {
htmlFragment = html.slice(idx, result.index);
tokens.push({
@@ -19,25 +20,29 @@ function parse(html) {
value: htmlFragment
});
}
// tag appeared
if (result[1]) {
token = factory('tag', 1, result);
var match = token.value.match(lexical.tagLine);
if (!match) {
throw new TokenizationError(`illegal tag: ${token.raw}`,
token.input, token.line);
}
token.name = match[1];
token.args = match[2];
tokens.push(token);
} else {
}
// output
else {
token = factory('output', 3, result);
tokens.push(token);
}
idx = syntax.lastIndex;
}
// remaining html
if (html.length > idx) {
htmlFragment = html.slice(idx, html.length);
tokens.push({
@@ -48,25 +53,28 @@ function parse(html) {
return tokens;
function factory(type, offset, match) {
var lines = match.input.slice(_idx + 1, match.index).split('\n');
var idx1 = match.input.lastIndexOf('\n', match.index);
var idx2 = match.input.indexOf('\n', match.index);
if(idx2 === -1) idx2 = match.input.length;
var input = match.input.slice(idx1 + 1, idx2);
_count += lines.length - 1;
_idx = match.index;
var token = {
return {
type: type,
raw: match[offset],
value: match[offset + 1].trim(),
line: _count + 1,
input: input
line: getLineNum(match),
input: getLineContent(match)
};
return token;
}
function getLineContent(match) {
var idx1 = match.input.lastIndexOf('\n', match.index);
var idx2 = match.input.indexOf('\n', match.index);
if (idx2 === -1) idx2 = match.input.length;
return match.input.slice(idx1 + 1, idx2);
}
function getLineNum(match) {
var lines = match.input.slice(_idx + 1, match.index).split('\n');
_count += lines.length - 1;
_idx = match.index;
return _count + 1;
}
}
exports.parse = parse;