Skip to content

Commit

Permalink
Added support for <$transclude $variable ... />
Browse files Browse the repository at this point in the history
  • Loading branch information
flibbles committed Feb 5, 2024
1 parent 0db7fd9 commit a415f1c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ exports.relink = function(text, fromTitle, toTitle, options) {
var endOfFilter = this.terminateIfMatch.index;
while (true) {
if (filter) {
entry = filterRelinker.relink(filter, fromTitle, toTitle, options);
var entry = filterRelinker.relink(filter, fromTitle, toTitle, options);
if (entry) {
if (entry.output) {
if (entry.output.indexOf('%>') > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*\
Handles replacement in $transclude widgets
\*/

exports.name = "transclude";

exports.getHandler = function(element, attribute, options) {
if (element.tag === "$transclude") {
var name = attribute.name;
if (name[0] === '$') {
if (name[1] === '$') {
name = name.substr(1);
} else {
// This is a reserved attribute
return;
}
}
var nameAttr = element.attributes["$variable"];
if (nameAttr) {
var setting = options.settings.getMacro(nameAttr.value);
return setting && setting[name];
}
}
};

exports.formBlurb = function(element, attribute, blurb, options) {
var nameAttr = element.attributes["$variable"];
var name = attribute.name;
if (name[0] === '$') {
name = name.substr(1);
}
var newBlurb = '<' + nameAttr.value + ' ' + name;
if (blurb) {
newBlurb += '=' + blurb;
}
return newBlurb;
};
39 changes: 38 additions & 1 deletion tiddlers/test/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ it('$macrocall', function() {
testText("<$macrocall $name=test Btitle='from here' other=<<anything>> />", true, ['<<test Btitle />']);
});

it('$macrocall imposssibles', function() {
it('$macrocall impossibles', function() {
testText("<$macrocall $name=test Clist=from />",
"<$macrocall $name=test Clist=from />",
['<<test Clist />'],
Expand All @@ -409,6 +409,43 @@ it('$macrocall imposssibles', function() {
{from: "from", to: "t ]] o", fails: 1});
});

(utils.atLeastVersion('5.3.0') ? it : xit)('$transclude', function() {
testText("<$transclude $variable=test A=stuff Btitle='from here' Clist='[[from here]]' Dref='from here##index' />", true,
['<<test Btitle />', '<<test Clist />', '<<test Dref="##index" />']);
// not having $name shouldn't cause a crash
testText("<$transclude Btitle='from here' />", false, undefined);
// unmanaged macros shouldn't cause problems either
testText("<$transclude $variable=none value='from here' />", false, undefined);
// leaves other attributes alone
// Unreported Issue: Relink would change unrelated macro parameters too.
// if they came after something that got relinked.
testText("<$transclude $variable=test Btitle='from here' other=<<anything>> />", true, ['<<test Btitle />']);
});

(utils.atLeastVersion('5.3.0') ? it : xit)('$transclude and $reserved attributes', function() {
var wiki = new $tw.Wiki();
wiki.addTiddlers([
utils.macroConf("test", "$title"),
utils.macroConf("test", "$mode"),
{title: "testMacro", tags: "$:/tags/Macro",
text: "\\procedure test($title, $mode) stuff\n"}]);
testText("<$transclude $variable=test $$title='from here' />", true, ['<<test $title />'], {wiki: wiki});
testText("<$transclude $variable=test $title='from here' />", false, undefined, {wiki: wiki});
testText("<$transclude $variable=test title='from here' />", false, undefined, {wiki: wiki});
testText("<$transclude $variable=test $$mode='from here' />", true, ['<<test $mode />'], {wiki: wiki});
testText("<$transclude $variable=test $mode='from here' />", false, undefined, {wiki: wiki});
});
(utils.atLeastVersion('5.3.0') ? it : xit)('$transclude impossibles', function() {
testText("<$transclude $variable=test Clist=from />",
"<$transclude $variable=test Clist=from />",
['<<test Clist />'],
{from: "from", to: "t ]] o", fails: 1});
testText("<$transclude $variable=test Clist=from Btitle=from />",
"<$transclude $variable=test Clist=from Btitle='t ]] o' />",
['<<test Clist />', '<<test Btitle />'],
{from: "from", to: "t ]] o", fails: 1});
});

it('attribute invocations', function() {
testText("Before <$a b=<<test stuff 'from here'>>/> After", true,
['<$a b=<<test Btitle>> />']);
Expand Down

0 comments on commit a415f1c

Please sign in to comment.