/*\ title: $:/core/modules/macros/transclude.js type: application/javascript module-type: macro Transclude macro \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; exports.info = { name: "transclude", dependentAll: true, // Tiddlers containing <> macro are dependent on every tiddler params: { filter: {byPos: 0, type: "filter"}, title: {byPos: 1, type: "tiddler"}, templateTitle: {byName: true, type: "tiddler"}, templateText: {byName: true, type: "text"}, emptyMessage: {byName: true, type: "text"} } }; /* Return the list of tiddlers to be transcluded */ exports.getTiddlerList = function() { if(this.hasParameter("filter")) { return this.wiki.filterTiddlers(this.params.filter,this.tiddlerTitle); } else if(this.hasParameter("title")) { return [this.params.title]; } else { return [this.tiddlerTitle]; } }; /* 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 return this.wiki.parseText("text/x-tiddlywiki",this.params.templateText); } else { if(this.hasParameter("templateTitle")) { // Check for recursion if(parents.indexOf(this.params.templateTitle) !== -1) { return $tw.Tree.errorNode("Tiddler recursion error in <> macro"); } parents.push(this.params.templateTitle); return this.wiki.parseTiddler(this.params.templateTitle); } else { 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(this.tiddlerList.length === 0 && this.hasParameter("emptyMessage")) { nodes = this.wiki.parseText("text/x-tiddlywiki",this.params.emptyMessage).tree; for(c=0; c