diff --git a/core/modules/macros/transclude.js b/core/modules/macros/transclude.js index d0e95b4d6..99a0d2228 100644 --- a/core/modules/macros/transclude.js +++ b/core/modules/macros/transclude.js @@ -18,30 +18,33 @@ exports.info = { params: { filter: {byPos: 0, type: "filter"}, title: {byPos: 1, type: "tiddler"}, - templateTitle: {byName: true, type: "text"}, + templateTitle: {byName: true, type: "tiddler"}, templateText: {byName: true, type: "text"}, emptyMessage: {byName: true, type: "text"} } }; -exports.executeMacro = function() { -console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle); - var titles,templateTiddler,templateText,t,title,templateParseTree, - nodes,node,c, - parents = this.parents.slice(0); - parents.push(this.tiddlerTitle); - // Get the tiddlers we're transcluding +/* +Return the list of tiddlers to be transcluded +*/ +exports.getTiddlerList = function() { if(this.hasParameter("filter")) { - titles = this.wiki.filterTiddlers(this.params.filter,this.tiddlerTitle); + return this.wiki.filterTiddlers(this.params.filter,this.tiddlerTitle); } else if(this.hasParameter("title")) { - titles = [this.params.title]; + return [this.params.title]; } else { - titles = [this.tiddlerTitle]; + return [this.tiddlerTitle]; } - // Get the template +}; + +/* +Get the parse tree of the template text to be used + parents: array of tiddler titles of parents in the render tree +*/ +exports.getTemplateParseTree = function(parents) { if(this.hasParameter("templateText")) { // Parse the template - templateParseTree = this.wiki.parseText("text/x-tiddlywiki",this.params.templateText); + return this.wiki.parseText("text/x-tiddlywiki",this.params.templateText); } else { if(this.hasParameter("templateTitle")) { // Check for recursion @@ -49,13 +52,26 @@ console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle); return $tw.Tree.errorNode("Tiddler recursion error in <> macro"); } parents.push(this.params.templateTitle); - templateParseTree = this.wiki.parseTiddler(this.params.templateTitle); + return this.wiki.parseTiddler(this.params.templateTitle); } else { - templateParseTree = this.wiki.parseText("text/x-tiddlywiki","<>"); + return this.wiki.parseText("text/x-tiddlywiki","<>"); } } +}; + +exports.executeMacro = function() { +console.log("Executing transclude macro",this.params.filter,this.tiddlerTitle); + var templateTiddler,templateText,t,title,templateParseTree, + nodes,node,c, + parents = this.parents.slice(0); + // Clear the tiddler list + this.tiddlerList = this.getTiddlerList(); + // Ensure we don't recurse back into ourselves + parents.push(this.tiddlerTitle); + // Get the template + templateParseTree = this.getTemplateParseTree(parents); // Use the empty message if the list is empty - if(titles.length === 0 && this.hasParameter("emptyMessage")) { + if(this.tiddlerList.length === 0 && this.hasParameter("emptyMessage")) { nodes = this.wiki.parseText("text/x-tiddlywiki",this.params.emptyMessage).tree; for(c=0; c