From 3a98cc5389dc77ff93d70f19d7dc976b7ef21faf Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 7 Jan 2012 18:33:57 +0000 Subject: [PATCH] Added verification of macro type compatibility --- js/WikiTextParseTree.js | 98 ++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/js/WikiTextParseTree.js b/js/WikiTextParseTree.js index ac05bb17d..37f2e22ba 100644 --- a/js/WikiTextParseTree.js +++ b/js/WikiTextParseTree.js @@ -80,61 +80,57 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) { var macro = this.store.macros[name], p, n; - if(macro) { - var macroCall = { - type: "FunctionCall", - name: { + if(!macro) { + this.pushString("{{** Unknown macro '" + name + "' **}}"); + return; + } + if(macro.types.indexOf(type) === -1) { + this.pushString("{{** Macro '" + name + "' cannot render to MIME type '" + type + "'**}}"); + return; + } + var macroCall = { + type: "FunctionCall", + name: { + "base": { "base": { "base": { - "base": { - "name": "store", - "type": "Variable" - }, - "name": "macros", - "type": "PropertyAccess" - }, - "name": { - "type": "StringLiteral", - "value": name - }, - "type": "PropertyAccess" - }, - "name": "code", - "type": "PropertyAccess" - }, - "arguments": [ { - "type": "StringLiteral", - "value": type - }, - { - "type": "Variable", - "name": "tiddler" - }, - { - "type": "Variable", - "name": "store" - }, - { - type: "ObjectLiteral", - properties: [] - }] - }; - for(p in params) { - if(params[p].type === "string") { - n = {type: "StringLiteral", value: params[p].value}; - } else { - n = this.store.jsParser.parse(params[p].value).tree.elements[0]; - } - macroCall["arguments"][3].properties.push({ - type: "PropertyAssignment", - name: p, - value: n - }); + "name": "store", + "type": "Variable"}, + "name": "macros", + "type": "PropertyAccess"}, + "name": { + "type": "StringLiteral", + "value": name}, + "type": "PropertyAccess"}, + "name": "code", + "type": "PropertyAccess"}, + "arguments": [ { + "type": "StringLiteral", + "value": type + },{ + "type": "Variable", + "name": "tiddler" + },{ + "type": "Variable", + "name": "store" + },{ + type: "ObjectLiteral", + properties: [] + }] + }; + for(p in params) { + if(params[p].type === "string") { + n = {type: "StringLiteral", value: params[p].value}; + } else { + n = this.store.jsParser.parse(params[p].value).tree.elements[0]; } - this.output.push(macroCall); - } else { - this.pushString("{{** Unknown macro '" + name + "' **}}"); + macroCall["arguments"][3].properties.push({ + type: "PropertyAssignment", + name: p, + value: n + }); } + this.output.push(macroCall); }; WikiTextParseTree.prototype.compileElementHtml = function(element, options) {