mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 18:17:20 +00:00
Improved the tiddler macro
Now you can transclude tiddlers as templates
This commit is contained in:
parent
6a81ed786e
commit
205987d280
@ -349,7 +349,7 @@ WikiStore.prototype.renderTiddler = function(targetType,title,asTitle,options) {
|
||||
return {};
|
||||
});
|
||||
if(tiddler) {
|
||||
if(asTitle) {
|
||||
if(asTitle && asTitle !== title) {
|
||||
var asTiddler = this.getTiddler(asTitle);
|
||||
return stitcher("div",{
|
||||
"data-tw-render-tiddler": title,
|
||||
|
@ -49,23 +49,38 @@ exports.macro = {
|
||||
types: ["text/html","text/plain"],
|
||||
cascadeParams: true, // Cascade names of named parameters to following anonymous parameters
|
||||
params: {
|
||||
target: {byName: "default", type: "tiddler", optional: false},
|
||||
target: {byName: "default", type: "tiddler", optional: true},
|
||||
template: {byName: true, type: "tiddler", optional: true},
|
||||
"with": {byName: true, type: "text", optional: true, dependentAll: true}
|
||||
},
|
||||
handler: function(type,tiddler,store,params) {
|
||||
var renderTitle = params.template,
|
||||
renderAs = params.target;
|
||||
// Slightly bewildering logic to sort out the optional parameters
|
||||
if(renderAs === undefined) {
|
||||
if(renderTitle === undefined) {
|
||||
return "";
|
||||
} else {
|
||||
renderAs = tiddler.title;
|
||||
}
|
||||
} else {
|
||||
if(renderTitle === undefined) {
|
||||
renderTitle = renderAs;
|
||||
}
|
||||
}
|
||||
if(params["with"]) {
|
||||
// Parameterised transclusion
|
||||
var targetTiddler = store.getTiddler(params.target),
|
||||
var targetTiddler = store.getTiddler(renderTitle),
|
||||
text = targetTiddler.text;
|
||||
var withTokens = [params["with"]];
|
||||
for(var t=0; t<withTokens.length; t++) {
|
||||
var placeholderRegExp = new RegExp("\\$"+(t+1),"mg");
|
||||
text = text.replace(placeholderRegExp,withTokens[t]);
|
||||
}
|
||||
return store.renderText(targetTiddler.type,text,type,params.target);
|
||||
return store.renderText(targetTiddler.type,text,type,renderAs);
|
||||
} else {
|
||||
// There's no parameterisation, so we can just render the target tiddler directly
|
||||
return store.renderTiddler(type,params.target);
|
||||
return store.renderTiddler(type,renderTitle,renderAs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
1
test/wikitests/EighthTiddler.html
Normal file
1
test/wikitests/EighthTiddler.html
Normal file
@ -0,0 +1 @@
|
||||
<div data-tw-render-tiddler='EighthTiddler'><div data-tw-macro='tiddler' data-tw-render-step='1'><div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='2'><div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='3'><div data-tw-render-tiddler='SixthTiddler' data-tw-render-as='EighthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>EighthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'></div><br><div data-tw-macro='view' data-tw-render-step='3'></div><br></div></div><br><div data-tw-macro='tiddler' data-tw-render-step='4'><div data-tw-render-tiddler='SixthTiddler' data-tw-render-as='FirstTiddler'><div data-tw-macro='view' data-tw-render-step='1'>FirstTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'></div><br><div data-tw-macro='view' data-tw-render-step='3'></div><br></div></div><br></div>
|
6
test/wikitests/EighthTiddler.tid
Normal file
6
test/wikitests/EighthTiddler.tid
Normal file
@ -0,0 +1,6 @@
|
||||
title: EighthTiddler
|
||||
|
||||
<<tiddler SixthTiddler>>
|
||||
<<tiddler target:SixthTiddler>>
|
||||
<<tiddler template:SixthTiddler>>
|
||||
<<tiddler template:SixthTiddler target:FirstTiddler>>
|
1
test/wikitests/EighthTiddler.txt
Normal file
1
test/wikitests/EighthTiddler.txt
Normal file
@ -0,0 +1 @@
|
||||
SixthTiddler11 February 2011JermoleneSixthTiddler11 February 2011JermoleneEighthTiddlerFirstTiddler
|
@ -1 +1 @@
|
||||
<div data-tw-render-tiddler='Fourth Tiddler'>An image <div data-tw-macro='image' data-tw-render-step='1'><img href='Something.jpg'></div> and a $1 couple of Ç Ç "HTML Entity" and macro <div data-tw-macro='version' data-tw-render-step='2'>5.0.0</div></div>
|
||||
<div data-tw-render-tiddler='Fourth Tiddler'>A missing image <div data-tw-macro='image' data-tw-render-step='1'><img href='Something.jpg'></div> and a $1 couple of Ç Ç "HTML Entity"</div>
|
@ -1,3 +1,3 @@
|
||||
title: Fourth Tiddler
|
||||
|
||||
An image [img[Something.jpg]] and a $1 couple of Ç Ç "HTML Entity" and macro <<version>>
|
||||
A missing image [img[Something.jpg]] and a $1 couple of Ç Ç "HTML Entity"
|
@ -1 +1 @@
|
||||
An image and a $1 couple of Ç Ç "HTML Entity" and macro 5.0.0
|
||||
A missing image and a $1 couple of Ç Ç "HTML Entity"
|
@ -5,7 +5,3 @@ title: SeventhTiddler
|
||||
<<echo {{tiddler.title}}>>
|
||||
|
||||
<<echo {{"window"}}>>
|
||||
|
||||
<<tiddler SixthTiddler>>
|
||||
|
||||
<<list all>>
|
||||
|
@ -1 +1 @@
|
||||
<div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br><div data-tw-macro='list' data-tw-render-step='4'><ul><li><div data-tw-macro='view' data-tw-render-step='1'><a href='Fifth Tiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>Fifth Tiddler</a></div></li><li><div data-tw-macro='view' data-tw-render-step='1'><a href='FirstTiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>FirstTiddler</a></div></li><li><div data-tw-macro='view' data-tw-render-step='1'><a href='Fourth Tiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>Fourth Tiddler</a></div></li><li><div data-tw-macro='view' data-tw-render-step='1'><a href='SecondTiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>SecondTiddler</a></div></li><li><div data-tw-macro='view' data-tw-render-step='1'><a href='SeventhTiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>SeventhTiddler</a></div></li><li><div data-tw-macro='view' data-tw-render-step='1'><a href='SixthTiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>SixthTiddler</a></div></li><li><div data-tw-macro='view' data-tw-render-step='1'><a href='ThirdTiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>ThirdTiddler</a></div></li></ul></div><br></div>
|
||||
<div data-tw-render-tiddler='SixthTiddler'><div data-tw-macro='view' data-tw-render-step='1'>SixthTiddler</div><br><div data-tw-macro='view' data-tw-render-step='2'>11 February 2011</div><br><div data-tw-macro='view' data-tw-render-step='3'><a href='Jermolene' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-missing'>Jermolene</a></div><br></div>
|
@ -1 +1 @@
|
||||
SixthTiddler11 February 2011JermoleneFifth TiddlerFirstTiddlerFourth TiddlerSecondTiddlerSeventhTiddlerSixthTiddlerThirdTiddler
|
||||
SixthTiddler11 February 2011Jermolene
|
@ -1 +1 @@
|
||||
<div data-tw-render-tiddler='ThirdTiddler'>An explicit link <span data-tw-macro='link' data-tw-render-step='1'><a href='Fourth Tiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>Fourth Tiddler</a></span> and <span data-tw-macro='link' data-tw-render-step='2'><a href='Fourth Tiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>a pretty link</a></span> and a transclusion <div data-tw-macro='tiddler' data-tw-render-step='3'>An image <div data-tw-macro='image' data-tw-render-step='1'><img href='Something.jpg'></div> and a Stringy couple of Ç Ç "HTML Entity" and macro <div data-tw-macro='version' data-tw-render-step='2'>5.0.0</div></div><br><br><div data-tw-macro='image' data-tw-render-step='4'><img href='http://placehold.it/350x150'></div><br></div>
|
||||
<div data-tw-render-tiddler='ThirdTiddler'>An explicit link <span data-tw-macro='link' data-tw-render-step='1'><a href='Fourth Tiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>Fourth Tiddler</a></span> and <span data-tw-macro='link' data-tw-render-step='2'><a href='Fourth Tiddler' class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves'>a pretty link</a></span> and a transclusion <div data-tw-macro='tiddler' data-tw-render-step='3'>A missing image <div data-tw-macro='image' data-tw-render-step='1'><img href='Something.jpg'></div> and a Stringy couple of Ç Ç "HTML Entity"</div><br><br><div data-tw-macro='image' data-tw-render-step='4'><img href='http://placehold.it/350x150'></div><br></div>
|
@ -1 +1 @@
|
||||
An explicit link Fourth Tiddler and a pretty link and a transclusion An image and a Stringy couple of Ç Ç "HTML Entity" and macro 5.0.0
|
||||
An explicit link Fourth Tiddler and a pretty link and a transclusion A missing image and a Stringy couple of Ç Ç "HTML Entity"
|
Loading…
Reference in New Issue
Block a user