From 745aac2609520cbf991146d57f93cc630737295a Mon Sep 17 00:00:00 2001 From: rlx <0x0073@0x2620.org> Date: Thu, 6 Oct 2011 01:56:04 +0000 Subject: [PATCH] fix issues with leading and trailing whitespace and linebreaks in Ox.doc --- demos/doc2/js/doc.js | 2 +- source/Ox/js/JavaScript.js | 35 ++++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/demos/doc2/js/doc.js b/demos/doc2/js/doc.js index 0bd33171..2d9a045f 100644 --- a/demos/doc2/js/doc.js +++ b/demos/doc2/js/doc.js @@ -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({ diff --git a/source/Ox/js/JavaScript.js b/source/Ox/js/JavaScript.js index c9d2661e..c4e8da28 100644 --- a/source/Ox/js/JavaScript.js +++ b/source/Ox/js/JavaScript.js @@ -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_ = []; + } + } else { + tokens_.push(token); + } } else { - tokens_ = Ox.merge(tokens_, leading, [token]); - leading = []; - includeLeading = true; + 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;