an even cleaner way to handle methods and newlines

This commit is contained in:
rolux 2011-05-06 20:47:43 +02:00
parent 1ef127d8f4
commit ae80b6db54

View file

@ -201,33 +201,15 @@ Ox.doc = (function() {
.split('\n');
// create a tree and parse its root node
item = parseNode(parseTree(lines));
///*
item.source = [];
var tokenBuffer = [], tokenFlag = false;
tokens[i].forEach(function(token) {
if (['linebreak', 'whitespace'].indexOf(token.type) > -1) {
tokenBuffer.push(token);
} else {
if (!tokenFlag) {
item.source.push(token);
tokenFlag = true;
} else {
item.source = Ox.merge(item.source, tokenBuffer, [token]);
}
tokenBuffer = [];
}
});
//*/
if (/^[A-Z]/.test(item.name)) {
item.source = parseTokens(tokens[i]);
items.push(item);
} else {
lastItem = items[items.length - 1];
lastItem.properties = lastItem.properties || [];
lastItem.properties.push(item);
lastItem.source = Ox.merge(lastItem.source, tokenBuffer, item.source);
delete item.source;
// include leading linebreaks and whitespace
lastItem.source = Ox.merge(lastItem.source, parseTokens(tokens[i], true));
}
});
function decodeLinebreaks(match, submatch) {
@ -312,6 +294,20 @@ Ox.doc = (function() {
result: lines[1].trim()
};
}
function parseTokens(tokens, includeLeading) {
var leading = [],
tokens_ = [];
tokens.forEach(function(token) {
if (['linebreak', 'whitespace'].indexOf(token.type) > -1) {
includeLeading && leading.push(token);
} else {
tokens_ = Ox.merge(tokens_, leading, [token]);
leading = [];
includeLeading = true;
}
});
return tokens_;
}
function parseTree(lines) {
// parses indented lines into a tree structure, like
// {line: "...", nodes: [{line: "...", nodes: [...]}]}