fix issues with leading and trailing whitespace and linebreaks in Ox.doc

This commit is contained in:
rlx 2011-10-06 01:56:04 +00:00
parent 9bd5568579
commit 745aac2609
2 changed files with 27 additions and 10 deletions

View file

@ -22,7 +22,7 @@ Ox.load('UI', {
},
getSection: function(item) {
var file = item.file.replace(Ox.PATH, '');
return item.section || file.split('/')[2];
return item.section || file.split('/')[2].split('.')[0];
},
path: Ox.PATH
}).bindEvent({

View file

@ -133,19 +133,35 @@ Ox.doc = (function() {
result: lines[1].trim()
};
}
function parseTokens(tokens, includeLeading) {
// fixme: do not strip whitespace from the beginning of the first line of the items' source
var leading = [],
tokens_ = [];
function parseTokens(tokens, includeLeadingLinebreaks) {
var isLeading = true,
isTrailing = false,
tokens_ = [],
types = ['linebreak', 'whitespace'];
tokens.forEach(function(token) {
if (['linebreak', 'whitespace'].indexOf(token.type) > -1) {
includeLeading && leading.push(token);
if (isLeading && types.indexOf(token.type) > -1) {
if (token.type == 'linebreak') {
if (includeLeadingLinebreaks) {
tokens_.push(token);
} else {
tokens_ = Ox.merge(tokens_, leading, [token]);
leading = [];
includeLeading = true;
tokens_ = [];
}
} else {
tokens_.push(token);
}
} else {
tokens_.push(token);
isLeading = false;
if (types.indexOf(token.type) == -1) {
isTrailing = true;
}
}
});
if (isTrailing) {
while (types.indexOf(tokens_[tokens_.length - 1].type) > -1) {
tokens_.pop();
}
}
return tokens_;
}
function parseTree(lines) {
@ -262,6 +278,7 @@ Ox.doc = (function() {
}
if (/^[A-Z]/.test(item.name)) {
// main item
// include leading whitespace
item.source = parseTokens(tokens[i]);
item.line = source.substr(0, item.source[0].offset)
.split('\n').length;