From a415f1c59ee0eb8bd28a320594a71499128d4155 Mon Sep 17 00:00:00 2001 From: Cameron Fischer Date: Mon, 5 Feb 2024 01:15:12 -0500 Subject: [PATCH] Added support for <$transclude $variable ... /> --- .../text/wikitext/conditional.js | 2 +- .../wikitext/html/attributes/transclude.js | 39 +++++++++++++++++++ tiddlers/test/macro.js | 39 ++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 plugins/relink/js/relinkoperations/text/wikitext/html/attributes/transclude.js diff --git a/plugins/relink/js/relinkoperations/text/wikitext/conditional.js b/plugins/relink/js/relinkoperations/text/wikitext/conditional.js index 878cf59..f0581fc 100644 --- a/plugins/relink/js/relinkoperations/text/wikitext/conditional.js +++ b/plugins/relink/js/relinkoperations/text/wikitext/conditional.js @@ -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) { diff --git a/plugins/relink/js/relinkoperations/text/wikitext/html/attributes/transclude.js b/plugins/relink/js/relinkoperations/text/wikitext/html/attributes/transclude.js new file mode 100644 index 0000000..150bded --- /dev/null +++ b/plugins/relink/js/relinkoperations/text/wikitext/html/attributes/transclude.js @@ -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; +}; diff --git a/tiddlers/test/macro.js b/tiddlers/test/macro.js index 629f5fd..7cd3925 100644 --- a/tiddlers/test/macro.js +++ b/tiddlers/test/macro.js @@ -398,7 +398,7 @@ it('$macrocall', function() { testText("<$macrocall $name=test Btitle='from here' other=<> />", true, ['<']); }); -it('$macrocall imposssibles', function() { +it('$macrocall impossibles', function() { testText("<$macrocall $name=test Clist=from />", "<$macrocall $name=test Clist=from />", ['<'], @@ -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, + ['<', '<', '<']); + // 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=<> />", true, ['<']); +}); + +(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, ['<'], {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, ['<'], {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 />", + ['<'], + {from: "from", to: "t ]] o", fails: 1}); + testText("<$transclude $variable=test Clist=from Btitle=from />", + "<$transclude $variable=test Clist=from Btitle='t ]] o' />", + ['<', '<'], + {from: "from", to: "t ]] o", fails: 1}); +}); + it('attribute invocations', function() { testText("Before <$a b=<>/> After", true, ['<$a b=<> />']);