1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-01 00:10:45 +00:00

Rejigged the link macro to accept child elements

This commit is contained in:
Jeremy Ruston 2012-01-15 18:39:14 +00:00
parent 606374e563
commit ac61e2d251
6 changed files with 76 additions and 48 deletions

View File

@ -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;
}

View File

@ -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]);

View File

@ -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);
}

View File

@ -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 "";
}

View File

@ -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 "";
}
}
};

View File

@ -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":