From ac61e2d25135d6900bae4761994c1572b8ad221e Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sun, 15 Jan 2012 18:39:14 +0000 Subject: [PATCH] Rejigged the link macro to accept child elements --- js/WikiStore.js | 4 +-- js/WikiTextParseTree.js | 69 +++++++++++++++++++++++++++-------------- js/WikiTextRules.js | 33 +++++++++++--------- js/macros/info.js | 6 ++-- js/macros/link.js | 10 +++--- js/macros/view.js | 2 +- 6 files changed, 76 insertions(+), 48 deletions(-) diff --git a/js/WikiStore.js b/js/WikiStore.js index 9902e47f5..7d8306554 100755 --- a/js/WikiStore.js +++ b/js/WikiStore.js @@ -355,10 +355,10 @@ WikiStore.prototype.renderTiddler = function(targetType,title,asTitle) { /* Executes a macro and returns the result */ -WikiStore.prototype.renderMacro = function(macroName,targetType,tiddler,params) { +WikiStore.prototype.renderMacro = function(macroName,targetType,tiddler,params,content) { var macro = this.macros[macroName]; if(macro) { - return macro.handler(targetType,tiddler,this,params); + return macro.handler(targetType,tiddler,this,params,content); } else { return null; } diff --git a/js/WikiTextParseTree.js b/js/WikiTextParseTree.js index a9e0d82d8..199326cb2 100644 --- a/js/WikiTextParseTree.js +++ b/js/WikiTextParseTree.js @@ -78,8 +78,10 @@ WikiTextParseTree.prototype.pushString = function(s) { } }; -WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) { - var macro = this.store.macros[name], +WikiTextParseTree.prototype.compileMacroCall = function(type,node) { + var name = node.name, + params = node.params, + macro = this.store.macros[name], p, n; if(!macro) { @@ -93,28 +95,28 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) { var macroCall = { type: "FunctionCall", name: { - "base": { - "base": { - "base": { - "name": "store", - "type": "Variable"}, - "name": "macros", - "type": "PropertyAccess"}, - "name": { - "type": "StringLiteral", - "value": name}, - "type": "PropertyAccess"}, - "name": "handler", - "type": "PropertyAccess"}, + base: { + base: { + base: { + name: "store", + type: "Variable"}, + name: "macros", + type: "PropertyAccess"}, + name: { + type: "StringLiteral", + value: name}, + type: "PropertyAccess"}, + name: "handler", + type: "PropertyAccess"}, "arguments": [ { - "type": "StringLiteral", - "value": type + type: "StringLiteral", + value: type },{ - "type": "Variable", - "name": "tiddler" + type: "Variable", + name: "tiddler" },{ - "type": "Variable", - "name": "store" + type: "Variable", + name: "store" },{ type: "ObjectLiteral", properties: [] @@ -132,6 +134,27 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) { value: n }); } + if(node.children) { + var saveOutput = this.output; + this.output = []; + this.compileSubTreeHtml(node.children); + macroCall["arguments"].push({ + type: "FunctionCall", + name: { + type: "PropertyAccess", + base: { + type: "ArrayLiteral", + elements: this.output + }, + name: "join" + }, + "arguments": [ { + type: "StringLiteral", + value: "" + }] + }); + this.output = saveOutput; + } var wrapperTag = macro.wrapperTag || "div"; if(type === "text/html") { this.pushString(utils.stitchElement(wrapperTag,{ @@ -183,7 +206,7 @@ WikiTextParseTree.prototype.compileSubTreeHtml = function(tree) { this.compileElementHtml(tree[t],{selfClosing: true}); // Self closing elements break; case "macro": - this.compileMacroCall("text/html",tree[t].name,tree[t].params); + this.compileMacroCall("text/html",tree[t]); break; default: this.compileElementHtml(tree[t]); @@ -220,7 +243,7 @@ WikiTextParseTree.prototype.compileSubTreePlain = function(tree) { this.compileElementPlain(tree[t],{selfClosing: true}); // Self closing elements break; case "macro": - this.compileMacroCall("text/plain",tree[t].name,tree[t].params); + this.compileMacroCall("text/plain",tree[t]); break; default: this.compileElementPlain(tree[t]); diff --git a/js/WikiTextRules.js b/js/WikiTextRules.js index 1b3e1ac85..5a457185a 100755 --- a/js/WikiTextRules.js +++ b/js/WikiTextRules.js @@ -463,9 +463,12 @@ var rules = [ var lookaheadMatch = this.lookaheadRegExp.exec(w.source); if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { var e = {type: "macro", name: "link", params: { - target: {type: "string", value: null}, - text: {type: "string", value: null} - }}, + target: {type: "string", value: null} + }, + children: [{ + type: "text", + value: "" + }]}, text = lookaheadMatch[1]; if(lookaheadMatch[3]) { // Pretty bracketted link @@ -477,7 +480,7 @@ var rules = [ e.params.target.value = text; w.addDependency(text); } - e.params.text.value = text; + e.children[0].value = text; w.output.push(e); w.nextMatch = this.lookaheadRegExp.lastIndex; } @@ -504,11 +507,12 @@ var rules = [ } if(w.autoLinkWikiWords) { var link = {type: "macro", name: "link", params: { - target: {type: "string", value: null}, - text: {type: "string", value: null} - }}; - link.params.target.value = w.matchText; - link.params.text.value = w.source.substring(w.matchStart,w.nextMatch); + target: {type: "string", value: w.matchText}, + }, + children: [{ + type: "text", + value: w.source.substring(w.matchStart,w.nextMatch) + }]}; w.addDependency(w.matchText); w.output.push(link); } else { @@ -523,11 +527,12 @@ var rules = [ handler: function(w) { var e = {type: "macro", name: "link", params: { - target: {type: "string", value: null}, - text: {type: "string", value: null} - }}; - e.params.target.value = w.matchText; - e.params.text.value = w.source.substring(w.matchStart,w.nextMatch); + target: {type: "string", value: w.matchText} + }, + children: [{ + type: "text", + value: w.source.substring(w.matchStart,w.nextMatch) + }]}; w.addDependency(w.matchText); w.output.push(e); } diff --git a/js/macros/info.js b/js/macros/info.js index b87b380eb..0815b0132 100644 --- a/js/macros/info.js +++ b/js/macros/info.js @@ -18,12 +18,14 @@ exports.macro = { var encoder = type === "text/html" ? utils.htmlEncode : function(x) {return x;}, parseTree = store.parseTiddler(tiddler.fields.title); if(parseTree) { + var r = []; var d = parseTree.dependencies; if(d === null) { - return encoder("Dependencies: *"); + r.push(encoder("Dependencies: *")); } else { - return encoder("Dependencies: " + d.join(", ")); + r.push(encoder("Dependencies: " + d.join(", "))); } + return r.join("/n"); } else { return ""; } diff --git a/js/macros/link.js b/js/macros/link.js index 93c57ce64..e8cda00e8 100644 --- a/js/macros/link.js +++ b/js/macros/link.js @@ -14,20 +14,18 @@ exports.macro = { wrapperTag: "span", types: ["text/html","text/plain"], params: { - target: {byName: "default", type: "tiddler", optional: false}, - text: {byName: true, type: "text", optional: true} + target: {byName: "default", type: "tiddler", optional: false} }, - handler: function(type,tiddler,store,params) { - var text = params.text || params.target; + handler: function(type,tiddler,store,params,content) { if(type === "text/html") { return utils.stitchElement("a",{ href: params.target },{ - content: utils.htmlEncode(text), + content: content, classNames: store.adjustClassesForLink([],params.target) }); } else if (type === "text/plain") { - return text; + return ""; } } }; diff --git a/js/macros/view.js b/js/macros/view.js index 364eb6d6f..8e4f160be 100644 --- a/js/macros/view.js +++ b/js/macros/view.js @@ -23,7 +23,7 @@ exports.macro = { if(v) { switch(params.format) { case "link": - return store.renderMacro("link",type,tiddler,{target: v}); + return store.renderMacro("link",type,tiddler,{target: v},encoder(v)); case "wikified": return store.renderTiddler(type,tiddler.fields.title); case "date":