1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-12-08 01:38:06 +00:00

Extend the HTML rendering mechanism to support attributes specified as macro invocations

This commit is contained in:
Jeremy Ruston
2013-06-18 15:37:19 +01:00
parent 18f8b7266e
commit 12b471b8fb
7 changed files with 87 additions and 57 deletions

View File

@@ -95,20 +95,30 @@ var ElementRenderer = function(renderTree,parentRenderer,parseTreeNode) {
};
ElementRenderer.prototype.computeAttributes = function() {
var changedAttributes = {};
var self = this;
var changedAttributes = {},
self = this,
value;
$tw.utils.each(this.parseTreeNode.attributes,function(attribute,name) {
if(attribute.type === "indirect") {
var value = self.renderTree.wiki.getTextReference(attribute.textReference,"",self.tiddlerTitle);
if(self.attributes[name] !== value) {
self.attributes[name] = value;
changedAttributes[name] = true;
value = self.renderTree.wiki.getTextReference(attribute.textReference,"",self.tiddlerTitle);
} else if(attribute.type === "macro") {
// Get the macro definition
var macro = self.renderTree.findMacroDefinition(self.parentRenderer,attribute.value.name);
if(!macro) {
value = "";
} else {
// Substitute the macro parameters
value = self.renderTree.substituteParameters(macro,attribute.value);
// Parse the text and render it as text
value = self.renderTree.wiki.renderText("text/plain","text/vnd.tiddlywiki",value,self.context);
}
} else { // String attribute
if(self.attributes[name] !== attribute.value) {
self.attributes[name] = attribute.value;
changedAttributes[name] = true;
}
value = attribute.value;
}
// Check whether the attribute has changed
if(self.attributes[name] !== value) {
self.attributes[name] = value;
changedAttributes[name] = true;
}
});
return changedAttributes;

View File

@@ -28,7 +28,7 @@ var MacroCallRenderer = function(renderTree,parentRenderer,parseTreeNode) {
childTree = [{type: "text", text: "<<Undefined macro: " + this.parseTreeNode.name + ">>"}];
} else {
// Substitute the macro parameters
var text = this.substituteParameters(macro.text,this.parseTreeNode,macro);
var text = this.renderTree.substituteParameters(macro,this.parseTreeNode);
// Parse the text
childTree = this.renderTree.wiki.parseText("text/vnd.tiddlywiki",text,{parseAsInline: !this.parseTreeNode.isBlock}).tree;
}
@@ -36,39 +36,6 @@ var MacroCallRenderer = function(renderTree,parentRenderer,parseTreeNode) {
this.children = this.renderTree.createRenderers(this,childTree);
};
/*
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],
paramValue = undefined;
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
this.domNode = this.renderTree.document.createElement(this.parseTreeNode.isBlock ? "div" : "span");