2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/rendertree/renderers/macrocall.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: wikirenderer
|
|
|
|
|
|
|
|
Macro call renderer
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Macro call renderer
|
|
|
|
*/
|
2013-05-15 16:32:17 +00:00
|
|
|
var MacroCallRenderer = function(renderTree,parentRenderer,parseTreeNode) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Store state information
|
|
|
|
this.renderTree = renderTree;
|
2013-05-15 16:32:17 +00:00
|
|
|
this.parentRenderer = parentRenderer;
|
2012-12-13 21:34:31 +00:00
|
|
|
this.parseTreeNode = parseTreeNode;
|
|
|
|
// Find the macro definition
|
2013-05-15 16:32:17 +00:00
|
|
|
var macro = this.renderTree.findMacroDefinition(this.parentRenderer,this.parseTreeNode.name);
|
2012-12-13 21:34:31 +00:00
|
|
|
// Insert an error message if we couldn't find the macro
|
2012-12-29 23:17:09 +00:00
|
|
|
var childTree;
|
2012-12-13 21:34:31 +00:00
|
|
|
if(!macro) {
|
|
|
|
childTree = [{type: "text", text: "<<Undefined macro: " + this.parseTreeNode.name + ">>"}];
|
|
|
|
} else {
|
|
|
|
// Substitute the macro parameters
|
|
|
|
var text = this.substituteParameters(macro.text,this.parseTreeNode,macro);
|
|
|
|
// Parse the text
|
2012-12-28 22:08:32 +00:00
|
|
|
childTree = this.renderTree.wiki.parseText("text/vnd.tiddlywiki",text,{parseAsInline: !this.parseTreeNode.isBlock}).tree;
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
// Create the renderers for the child nodes
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderTree.createRenderers(this,childTree);
|
2012-12-29 23:17:09 +00:00
|
|
|
};
|
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
/*
|
|
|
|
Expand the parameters in a block of text
|
|
|
|
*/
|
|
|
|
MacroCallRenderer.prototype.substituteParameters = function(text,macroCallParseTreeNode,macroDefinition) {
|
|
|
|
var nextAnonParameter = 0; // Next candidate anonymous parameter in macro call
|
|
|
|
// Step through each of the parameters in the macro definition
|
|
|
|
for(var p=0; p<macroDefinition.params.length; p++) {
|
|
|
|
// Check if we've got a macro call parameter with the same name
|
|
|
|
var paramInfo = macroDefinition.params[p],
|
2012-12-20 09:21:44 +00:00
|
|
|
paramValue = undefined;
|
2012-12-13 21:34:31 +00:00
|
|
|
for(var m=0; m<macroCallParseTreeNode.params.length; m++) {
|
|
|
|
if(macroCallParseTreeNode.params[m].name === paramInfo.name) {
|
|
|
|
paramValue = macroCallParseTreeNode.params[m].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If not, use the next available anonymous macro call parameter
|
|
|
|
if(!paramValue && macroCallParseTreeNode.params.length > 0) {
|
|
|
|
while(macroCallParseTreeNode.params[nextAnonParameter].name && nextAnonParameter < macroCallParseTreeNode.params.length-1) {
|
|
|
|
nextAnonParameter++;
|
|
|
|
}
|
|
|
|
if(!macroCallParseTreeNode.params[nextAnonParameter].name) {
|
|
|
|
paramValue = macroCallParseTreeNode.params[nextAnonParameter].value;
|
|
|
|
nextAnonParameter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we've still not got a value, use the default, if any
|
|
|
|
paramValue = paramValue || paramInfo["default"] || "";
|
|
|
|
// Replace any instances of this parameter
|
|
|
|
text = text.replace(new RegExp("\\$" + $tw.utils.escapeRegExp(paramInfo.name) + "\\$","mg"),paramValue);
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
MacroCallRenderer.prototype.renderInDom = function() {
|
|
|
|
// Create the element
|
2013-05-17 09:12:25 +00:00
|
|
|
this.domNode = this.renderTree.document.createElement(this.parseTreeNode.isBlock ? "div" : "span");
|
2012-12-13 21:34:31 +00:00
|
|
|
this.domNode.setAttribute("data-macro-name",this.parseTreeNode.name);
|
|
|
|
// Render any child nodes
|
|
|
|
var self = this;
|
|
|
|
$tw.utils.each(this.children,function(node,index) {
|
|
|
|
if(node.renderInDom) {
|
|
|
|
self.domNode.appendChild(node.renderInDom());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Return the dom node
|
|
|
|
return this.domNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.macrocall = MacroCallRenderer
|
|
|
|
|
|
|
|
})();
|