mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-23 07:26:54 +00:00
Rejigged the link macro to accept child elements
This commit is contained in:
parent
606374e563
commit
ac61e2d251
@ -355,10 +355,10 @@ WikiStore.prototype.renderTiddler = function(targetType,title,asTitle) {
|
|||||||
/*
|
/*
|
||||||
Executes a macro and returns the result
|
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];
|
var macro = this.macros[macroName];
|
||||||
if(macro) {
|
if(macro) {
|
||||||
return macro.handler(targetType,tiddler,this,params);
|
return macro.handler(targetType,tiddler,this,params,content);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -78,8 +78,10 @@ WikiTextParseTree.prototype.pushString = function(s) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) {
|
WikiTextParseTree.prototype.compileMacroCall = function(type,node) {
|
||||||
var macro = this.store.macros[name],
|
var name = node.name,
|
||||||
|
params = node.params,
|
||||||
|
macro = this.store.macros[name],
|
||||||
p,
|
p,
|
||||||
n;
|
n;
|
||||||
if(!macro) {
|
if(!macro) {
|
||||||
@ -93,28 +95,28 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) {
|
|||||||
var macroCall = {
|
var macroCall = {
|
||||||
type: "FunctionCall",
|
type: "FunctionCall",
|
||||||
name: {
|
name: {
|
||||||
"base": {
|
base: {
|
||||||
"base": {
|
base: {
|
||||||
"base": {
|
base: {
|
||||||
"name": "store",
|
name: "store",
|
||||||
"type": "Variable"},
|
type: "Variable"},
|
||||||
"name": "macros",
|
name: "macros",
|
||||||
"type": "PropertyAccess"},
|
type: "PropertyAccess"},
|
||||||
"name": {
|
name: {
|
||||||
"type": "StringLiteral",
|
type: "StringLiteral",
|
||||||
"value": name},
|
value: name},
|
||||||
"type": "PropertyAccess"},
|
type: "PropertyAccess"},
|
||||||
"name": "handler",
|
name: "handler",
|
||||||
"type": "PropertyAccess"},
|
type: "PropertyAccess"},
|
||||||
"arguments": [ {
|
"arguments": [ {
|
||||||
"type": "StringLiteral",
|
type: "StringLiteral",
|
||||||
"value": type
|
value: type
|
||||||
},{
|
},{
|
||||||
"type": "Variable",
|
type: "Variable",
|
||||||
"name": "tiddler"
|
name: "tiddler"
|
||||||
},{
|
},{
|
||||||
"type": "Variable",
|
type: "Variable",
|
||||||
"name": "store"
|
name: "store"
|
||||||
},{
|
},{
|
||||||
type: "ObjectLiteral",
|
type: "ObjectLiteral",
|
||||||
properties: []
|
properties: []
|
||||||
@ -132,6 +134,27 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) {
|
|||||||
value: n
|
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";
|
var wrapperTag = macro.wrapperTag || "div";
|
||||||
if(type === "text/html") {
|
if(type === "text/html") {
|
||||||
this.pushString(utils.stitchElement(wrapperTag,{
|
this.pushString(utils.stitchElement(wrapperTag,{
|
||||||
@ -183,7 +206,7 @@ WikiTextParseTree.prototype.compileSubTreeHtml = function(tree) {
|
|||||||
this.compileElementHtml(tree[t],{selfClosing: true}); // Self closing elements
|
this.compileElementHtml(tree[t],{selfClosing: true}); // Self closing elements
|
||||||
break;
|
break;
|
||||||
case "macro":
|
case "macro":
|
||||||
this.compileMacroCall("text/html",tree[t].name,tree[t].params);
|
this.compileMacroCall("text/html",tree[t]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.compileElementHtml(tree[t]);
|
this.compileElementHtml(tree[t]);
|
||||||
@ -220,7 +243,7 @@ WikiTextParseTree.prototype.compileSubTreePlain = function(tree) {
|
|||||||
this.compileElementPlain(tree[t],{selfClosing: true}); // Self closing elements
|
this.compileElementPlain(tree[t],{selfClosing: true}); // Self closing elements
|
||||||
break;
|
break;
|
||||||
case "macro":
|
case "macro":
|
||||||
this.compileMacroCall("text/plain",tree[t].name,tree[t].params);
|
this.compileMacroCall("text/plain",tree[t]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.compileElementPlain(tree[t]);
|
this.compileElementPlain(tree[t]);
|
||||||
|
@ -463,9 +463,12 @@ var rules = [
|
|||||||
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
|
||||||
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
|
||||||
var e = {type: "macro", name: "link", params: {
|
var e = {type: "macro", name: "link", params: {
|
||||||
target: {type: "string", value: null},
|
target: {type: "string", value: null}
|
||||||
text: {type: "string", value: null}
|
},
|
||||||
}},
|
children: [{
|
||||||
|
type: "text",
|
||||||
|
value: ""
|
||||||
|
}]},
|
||||||
text = lookaheadMatch[1];
|
text = lookaheadMatch[1];
|
||||||
if(lookaheadMatch[3]) {
|
if(lookaheadMatch[3]) {
|
||||||
// Pretty bracketted link
|
// Pretty bracketted link
|
||||||
@ -477,7 +480,7 @@ var rules = [
|
|||||||
e.params.target.value = text;
|
e.params.target.value = text;
|
||||||
w.addDependency(text);
|
w.addDependency(text);
|
||||||
}
|
}
|
||||||
e.params.text.value = text;
|
e.children[0].value = text;
|
||||||
w.output.push(e);
|
w.output.push(e);
|
||||||
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
w.nextMatch = this.lookaheadRegExp.lastIndex;
|
||||||
}
|
}
|
||||||
@ -504,11 +507,12 @@ var rules = [
|
|||||||
}
|
}
|
||||||
if(w.autoLinkWikiWords) {
|
if(w.autoLinkWikiWords) {
|
||||||
var link = {type: "macro", name: "link", params: {
|
var link = {type: "macro", name: "link", params: {
|
||||||
target: {type: "string", value: null},
|
target: {type: "string", value: w.matchText},
|
||||||
text: {type: "string", value: null}
|
},
|
||||||
}};
|
children: [{
|
||||||
link.params.target.value = w.matchText;
|
type: "text",
|
||||||
link.params.text.value = w.source.substring(w.matchStart,w.nextMatch);
|
value: w.source.substring(w.matchStart,w.nextMatch)
|
||||||
|
}]};
|
||||||
w.addDependency(w.matchText);
|
w.addDependency(w.matchText);
|
||||||
w.output.push(link);
|
w.output.push(link);
|
||||||
} else {
|
} else {
|
||||||
@ -523,11 +527,12 @@ var rules = [
|
|||||||
handler: function(w)
|
handler: function(w)
|
||||||
{
|
{
|
||||||
var e = {type: "macro", name: "link", params: {
|
var e = {type: "macro", name: "link", params: {
|
||||||
target: {type: "string", value: null},
|
target: {type: "string", value: w.matchText}
|
||||||
text: {type: "string", value: null}
|
},
|
||||||
}};
|
children: [{
|
||||||
e.params.target.value = w.matchText;
|
type: "text",
|
||||||
e.params.text.value = w.source.substring(w.matchStart,w.nextMatch);
|
value: w.source.substring(w.matchStart,w.nextMatch)
|
||||||
|
}]};
|
||||||
w.addDependency(w.matchText);
|
w.addDependency(w.matchText);
|
||||||
w.output.push(e);
|
w.output.push(e);
|
||||||
}
|
}
|
||||||
|
@ -18,12 +18,14 @@ exports.macro = {
|
|||||||
var encoder = type === "text/html" ? utils.htmlEncode : function(x) {return x;},
|
var encoder = type === "text/html" ? utils.htmlEncode : function(x) {return x;},
|
||||||
parseTree = store.parseTiddler(tiddler.fields.title);
|
parseTree = store.parseTiddler(tiddler.fields.title);
|
||||||
if(parseTree) {
|
if(parseTree) {
|
||||||
|
var r = [];
|
||||||
var d = parseTree.dependencies;
|
var d = parseTree.dependencies;
|
||||||
if(d === null) {
|
if(d === null) {
|
||||||
return encoder("Dependencies: *");
|
r.push(encoder("Dependencies: *"));
|
||||||
} else {
|
} else {
|
||||||
return encoder("Dependencies: " + d.join(", "));
|
r.push(encoder("Dependencies: " + d.join(", ")));
|
||||||
}
|
}
|
||||||
|
return r.join("/n");
|
||||||
} else {
|
} else {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -14,20 +14,18 @@ exports.macro = {
|
|||||||
wrapperTag: "span",
|
wrapperTag: "span",
|
||||||
types: ["text/html","text/plain"],
|
types: ["text/html","text/plain"],
|
||||||
params: {
|
params: {
|
||||||
target: {byName: "default", type: "tiddler", optional: false},
|
target: {byName: "default", type: "tiddler", optional: false}
|
||||||
text: {byName: true, type: "text", optional: true}
|
|
||||||
},
|
},
|
||||||
handler: function(type,tiddler,store,params) {
|
handler: function(type,tiddler,store,params,content) {
|
||||||
var text = params.text || params.target;
|
|
||||||
if(type === "text/html") {
|
if(type === "text/html") {
|
||||||
return utils.stitchElement("a",{
|
return utils.stitchElement("a",{
|
||||||
href: params.target
|
href: params.target
|
||||||
},{
|
},{
|
||||||
content: utils.htmlEncode(text),
|
content: content,
|
||||||
classNames: store.adjustClassesForLink([],params.target)
|
classNames: store.adjustClassesForLink([],params.target)
|
||||||
});
|
});
|
||||||
} else if (type === "text/plain") {
|
} else if (type === "text/plain") {
|
||||||
return text;
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -23,7 +23,7 @@ exports.macro = {
|
|||||||
if(v) {
|
if(v) {
|
||||||
switch(params.format) {
|
switch(params.format) {
|
||||||
case "link":
|
case "link":
|
||||||
return store.renderMacro("link",type,tiddler,{target: v});
|
return store.renderMacro("link",type,tiddler,{target: v},encoder(v));
|
||||||
case "wikified":
|
case "wikified":
|
||||||
return store.renderTiddler(type,tiddler.fields.title);
|
return store.renderTiddler(type,tiddler.fields.title);
|
||||||
case "date":
|
case "date":
|
||||||
|
Loading…
Reference in New Issue
Block a user