an even cleaner way to handle methods and newlines
This commit is contained in:
parent
1ef127d8f4
commit
ae80b6db54
1 changed files with 17 additions and 21 deletions
38
source/Ox.js
38
source/Ox.js
|
@ -201,33 +201,15 @@ Ox.doc = (function() {
|
||||||
.split('\n');
|
.split('\n');
|
||||||
// create a tree and parse its root node
|
// create a tree and parse its root node
|
||||||
item = parseNode(parseTree(lines));
|
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)) {
|
if (/^[A-Z]/.test(item.name)) {
|
||||||
|
item.source = parseTokens(tokens[i]);
|
||||||
items.push(item);
|
items.push(item);
|
||||||
} else {
|
} else {
|
||||||
lastItem = items[items.length - 1];
|
lastItem = items[items.length - 1];
|
||||||
lastItem.properties = lastItem.properties || [];
|
lastItem.properties = lastItem.properties || [];
|
||||||
lastItem.properties.push(item);
|
lastItem.properties.push(item);
|
||||||
lastItem.source = Ox.merge(lastItem.source, tokenBuffer, item.source);
|
// include leading linebreaks and whitespace
|
||||||
delete item.source;
|
lastItem.source = Ox.merge(lastItem.source, parseTokens(tokens[i], true));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
function decodeLinebreaks(match, submatch) {
|
function decodeLinebreaks(match, submatch) {
|
||||||
|
@ -312,6 +294,20 @@ Ox.doc = (function() {
|
||||||
result: lines[1].trim()
|
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) {
|
function parseTree(lines) {
|
||||||
// parses indented lines into a tree structure, like
|
// parses indented lines into a tree structure, like
|
||||||
// {line: "...", nodes: [{line: "...", nodes: [...]}]}
|
// {line: "...", nodes: [{line: "...", nodes: [...]}]}
|
||||||
|
|
Loading…
Reference in a new issue