add method source to parent source; strip leading and trailing newlines

This commit is contained in:
rolux 2011-05-06 20:01:34 +02:00
parent b1d171282c
commit 72d3ec87c2
2 changed files with 28 additions and 14 deletions

View file

@ -86,8 +86,8 @@ Ox.DocPage = function(options, self) {
.css({marginLeft: ((level * 32 + 32) + 'px')}) .css({marginLeft: ((level * 32 + 32) + 'px')})
.html( .html(
'<code><b>&gt;</b>&nbsp;' + '<code><b>&gt;</b>&nbsp;' +
//Ox.encodeHTML(example.statement) Ox.encodeHTML(example.statement)
example.statement //example.statement
.replace(/ /g, '&nbsp;') .replace(/ /g, '&nbsp;')
.replace(/\n/g, '<br/>\n&nbsp;&nbsp;') + .replace(/\n/g, '<br/>\n&nbsp;&nbsp;') +
'</code>' '</code>'
@ -103,18 +103,12 @@ Ox.DocPage = function(options, self) {
}); });
} else if (section == 'source') { } else if (section == 'source') {
var html = ''; var html = '';
var flag = false;
item.source.forEach(function(token) { item.source.forEach(function(token) {
if (token.type != 'linebreak' && token.type != 'whitespace') { html += '<span class="Ox' + Ox.toTitleCase(token.type) + '">' +
flag = true; Ox.encodeHTML(token.source)
} .replace(/ /g, '&nbsp;')
if (flag) { .replace(/\n/g, '<br/>') +
html += '<span class="Ox' + Ox.toTitleCase(token.type) + '">' + '</span>';
Ox.encodeHTML(token.source)
.replace(/ /g, '&nbsp;')
.replace(/\n/g, '<br/>') +
'</span>';
}
}); });
$elements.push($('<div>') $elements.push($('<div>')
.addClass('OxSourceCode ' + className) .addClass('OxSourceCode ' + className)

View file

@ -200,13 +200,33 @@ Ox.doc = (function() {
.replace(re.test, encodeLinebreaks) .replace(re.test, encodeLinebreaks)
.split('\n'); .split('\n');
// create a tree and parse its root node // create a tree and parse its root node
item = Ox.extend(parseNode(parseTree(lines)), {source: tokens[i]}); 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)) {
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, item.source);
} }
}); });
function decodeLinebreaks(match, submatch) { function decodeLinebreaks(match, submatch) {