tag: tablerow

This commit is contained in:
harttle
2016-06-17 14:59:30 +08:00
parent 0b7da2abf7
commit b478c798fd
10 changed files with 234 additions and 38 deletions
+26 -19
View File
@@ -1,16 +1,18 @@
const lexical = require('./lexical.js');
const error = require('./error.js');
const TokenizationError = require('./error.js').TokenizationError;
function parse(html){
function parse(html) {
var tokens = [];
if(!html) return tokens;
if (!html) return tokens;
var syntax = /({%(.*?)%})|({{(.*?)}})/g;
var result, htmlFragment, token;
var idx = 0;
var _idx = -1;
var _count = 0;
while ((result = syntax.exec(html)) !== null) {
if(result.index > idx){
if (result.index > idx) {
htmlFragment = html.slice(idx, result.index);
tokens.push({
type: 'html',
@@ -18,23 +20,25 @@ function parse(html){
});
}
if(result[1]){
if (result[1]) {
token = factory('tag', 1, result);
var match = token.value.match(lexical.tagLine);
if (!match) error('illegal tag', token);
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{
} else {
token = factory('output', 3, result);
tokens.push(token);
}
idx = syntax.lastIndex;
}
if(html.length > idx){
if (html.length > idx) {
htmlFragment = html.slice(idx, html.length);
tokens.push({
type: 'html',
@@ -43,23 +47,26 @@ function parse(html){
}
return tokens;
function factory(type, offset, match){
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 = {
type,
type: type,
raw: match[offset],
value: match[offset + 1].trim(),
line: crCount(match.index) + 1
line: _count + 1,
input: input
};
return token;
}
var lastIdx = -1;
var lastCount = 0;
function crCount(idx){
lastCount += html.slice(lastIdx+1, idx);
lastIdx = idx;
return lastCount;
}
}
exports.parse = parse;