fix issues with leading and trailing whitespace and linebreaks in Ox.doc
This commit is contained in:
parent
9bd5568579
commit
745aac2609
2 changed files with 27 additions and 10 deletions
|
@ -22,7 +22,7 @@ Ox.load('UI', {
|
||||||
},
|
},
|
||||||
getSection: function(item) {
|
getSection: function(item) {
|
||||||
var file = item.file.replace(Ox.PATH, '');
|
var file = item.file.replace(Ox.PATH, '');
|
||||||
return item.section || file.split('/')[2];
|
return item.section || file.split('/')[2].split('.')[0];
|
||||||
},
|
},
|
||||||
path: Ox.PATH
|
path: Ox.PATH
|
||||||
}).bindEvent({
|
}).bindEvent({
|
||||||
|
|
|
@ -133,19 +133,35 @@ Ox.doc = (function() {
|
||||||
result: lines[1].trim()
|
result: lines[1].trim()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
function parseTokens(tokens, includeLeading) {
|
function parseTokens(tokens, includeLeadingLinebreaks) {
|
||||||
// fixme: do not strip whitespace from the beginning of the first line of the items' source
|
var isLeading = true,
|
||||||
var leading = [],
|
isTrailing = false,
|
||||||
tokens_ = [];
|
tokens_ = [],
|
||||||
|
types = ['linebreak', 'whitespace'];
|
||||||
tokens.forEach(function(token) {
|
tokens.forEach(function(token) {
|
||||||
if (['linebreak', 'whitespace'].indexOf(token.type) > -1) {
|
if (isLeading && types.indexOf(token.type) > -1) {
|
||||||
includeLeading && leading.push(token);
|
if (token.type == 'linebreak') {
|
||||||
|
if (includeLeadingLinebreaks) {
|
||||||
|
tokens_.push(token);
|
||||||
|
} else {
|
||||||
|
tokens_ = [];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tokens_.push(token);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
tokens_ = Ox.merge(tokens_, leading, [token]);
|
tokens_.push(token);
|
||||||
leading = [];
|
isLeading = false;
|
||||||
includeLeading = true;
|
if (types.indexOf(token.type) == -1) {
|
||||||
|
isTrailing = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (isTrailing) {
|
||||||
|
while (types.indexOf(tokens_[tokens_.length - 1].type) > -1) {
|
||||||
|
tokens_.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
return tokens_;
|
return tokens_;
|
||||||
}
|
}
|
||||||
function parseTree(lines) {
|
function parseTree(lines) {
|
||||||
|
@ -262,6 +278,7 @@ Ox.doc = (function() {
|
||||||
}
|
}
|
||||||
if (/^[A-Z]/.test(item.name)) {
|
if (/^[A-Z]/.test(item.name)) {
|
||||||
// main item
|
// main item
|
||||||
|
// include leading whitespace
|
||||||
item.source = parseTokens(tokens[i]);
|
item.source = parseTokens(tokens[i]);
|
||||||
item.line = source.substr(0, item.source[0].offset)
|
item.line = source.substr(0, item.source[0].offset)
|
||||||
.split('\n').length;
|
.split('\n').length;
|
||||||
|
|
Loading…
Reference in a new issue