From f10d58ed2bed9306f2805c98a2106d545d444b81 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 17 Jan 2012 15:12:59 +0000 Subject: [PATCH] Refactored WikiTextParseTree to handle the output array better --- js/WikiTextParseTree.js | 77 ++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/js/WikiTextParseTree.js b/js/WikiTextParseTree.js index 199326cb2..b4a6cbbac 100644 --- a/js/WikiTextParseTree.js +++ b/js/WikiTextParseTree.js @@ -23,11 +23,11 @@ var WikiTextParseTree = function(tree,dependencies,store) { // representation of the tree WikiTextParseTree.prototype.compile = function(type,treenode) { treenode = treenode || this.tree; - this.output = []; + var output = []; if(type === "text/html") { - this.compileSubTreeHtml(treenode); + this.compileSubTreeHtml(output,treenode); } else if(type === "text/plain") { - this.compileSubTreePlain(treenode); + this.compileSubTreePlain(output,treenode); } else { return null; } @@ -47,7 +47,7 @@ WikiTextParseTree.prototype.compile = function(type,treenode) { type: "PropertyAccess", base: { type: "ArrayLiteral", - elements: this.output + elements: output }, name: "join" }, @@ -62,34 +62,33 @@ WikiTextParseTree.prototype.compile = function(type,treenode) { } ]); var r = parseTree.render(); - this.output = null; return r; }; -WikiTextParseTree.prototype.pushString = function(s) { - var last = this.output[this.output.length-1]; - if(this.output.length > 0 && last.type === "StringLiterals") { +WikiTextParseTree.prototype.pushString = function(output,s) { + var last = output[output.length-1]; + if(output.length > 0 && last.type === "StringLiterals") { last.value.push(s); - } else if (this.output.length > 0 && last.type === "StringLiteral") { + } else if (output.length > 0 && last.type === "StringLiteral") { last.type = "StringLiterals"; last.value = [last.value,s]; } else { - this.output.push({type: "StringLiteral", value: s}); + output.push({type: "StringLiteral", value: s}); } }; -WikiTextParseTree.prototype.compileMacroCall = function(type,node) { +WikiTextParseTree.prototype.compileMacroCall = function(output,type,node) { var name = node.name, params = node.params, macro = this.store.macros[name], p, n; if(!macro) { - this.pushString("{{** Unknown macro '" + name + "' **}}"); + this.pushString(output,"{{** Unknown macro '" + name + "' **}}"); return; } if(macro.types.indexOf(type) === -1) { - this.pushString("{{** Macro '" + name + "' cannot render to MIME type '" + type + "'**}}"); + this.pushString(output,"{{** Macro '" + name + "' cannot render to MIME type '" + type + "'**}}"); return; } var macroCall = { @@ -135,16 +134,15 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,node) { }); } if(node.children) { - var saveOutput = this.output; - this.output = []; - this.compileSubTreeHtml(node.children); + var subOutput = []; + this.compileSubTreeHtml(subOutput,node.children); macroCall["arguments"].push({ type: "FunctionCall", name: { type: "PropertyAccess", base: { type: "ArrayLiteral", - elements: this.output + elements: subOutput }, name: "join" }, @@ -153,21 +151,20 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,node) { value: "" }] }); - this.output = saveOutput; } var wrapperTag = macro.wrapperTag || "div"; if(type === "text/html") { - this.pushString(utils.stitchElement(wrapperTag,{ + this.pushString(output,utils.stitchElement(wrapperTag,{ "data-tw-macro": name })); } - this.output.push(macroCall); + output.push(macroCall); if(type === "text/html") { - this.pushString(""); + this.pushString(output,""); } }; -WikiTextParseTree.prototype.compileElementHtml = function(element, options) { +WikiTextParseTree.prototype.compileElementHtml = function(output,element,options) { options = options || {}; var tagBits = [element.type]; if(element.attributes) { @@ -183,70 +180,70 @@ WikiTextParseTree.prototype.compileElementHtml = function(element, options) { tagBits.push(a + "=\"" + utils.htmlEncode(r) + "\""); } } - this.pushString("<" + tagBits.join(" ") + (options.selfClosing ? " /" : "") + ">"); + this.pushString(output,"<" + tagBits.join(" ") + (options.selfClosing ? " /" : "") + ">"); if(!options.selfClosing) { if(element.children) { - this.compileSubTreeHtml(element.children); + this.compileSubTreeHtml(output,element.children); } - this.pushString(""); + this.pushString(output,""); } }; -WikiTextParseTree.prototype.compileSubTreeHtml = function(tree) { +WikiTextParseTree.prototype.compileSubTreeHtml = function(output,tree) { for(var t=0; t