1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 20:10:03 +00:00

Refactor macro parameter substitution

Now macros can also include references to variables as `$(variable)$`
This commit is contained in:
Jeremy Ruston 2013-10-13 22:38:46 +01:00
parent 987890c085
commit 79dcc9a557

View File

@ -74,8 +74,8 @@ Get the prevailing value of a context variable
name: name of variable name: name of variable
params: array of {name:, value:} for each parameter params: array of {name:, value:} for each parameter
*/ */
Widget.prototype.getVariable = function(name,params,defaultValue) { Widget.prototype.getVariable = function(name,actualParams,defaultValue) {
params = params || []; actualParams = actualParams || [];
// Search up the widget tree for the variable name // Search up the widget tree for the variable name
var node = this; var node = this;
while(node && !$tw.utils.hop(node.variables,name)) { while(node && !$tw.utils.hop(node.variables,name)) {
@ -87,34 +87,46 @@ Widget.prototype.getVariable = function(name,params,defaultValue) {
// Get the value // Get the value
var value = node.variables[name].value; var value = node.variables[name].value;
// Substitute any parameters specified in the definition // Substitute any parameters specified in the definition
var defParams = node.variables[name].params; value = this.substituteVariableParameters(value,node.variables[name].params,actualParams);
if(defParams) { value = this.substituteVariableReferences(value);
return value;
};
Widget.prototype.substituteVariableParameters = function(text,formalParams,actualParams) {
if(formalParams) {
var nextAnonParameter = 0, // Next candidate anonymous parameter in macro call var nextAnonParameter = 0, // Next candidate anonymous parameter in macro call
paramInfo, paramValue; paramInfo, paramValue;
// Step through each of the parameters in the macro definition // Step through each of the parameters in the macro definition
for(var p=0; p<defParams.length; p++) { for(var p=0; p<formalParams.length; p++) {
// Check if we've got a macro call parameter with the same name // Check if we've got a macro call parameter with the same name
paramInfo = defParams[p]; paramInfo = formalParams[p];
paramValue = undefined; paramValue = undefined;
for(var m=0; m<params.length; m++) { for(var m=0; m<actualParams.length; m++) {
if(params[m].name === paramInfo.name) { if(actualParams[m].name === paramInfo.name) {
paramValue = params[m].value; paramValue = actualParams[m].value;
} }
} }
// If not, use the next available anonymous macro call parameter // If not, use the next available anonymous macro call parameter
while(nextAnonParameter < params.length && params[nextAnonParameter].name) { while(nextAnonParameter < actualParams.length && actualParams[nextAnonParameter].name) {
nextAnonParameter++; nextAnonParameter++;
} }
if(paramValue === undefined && nextAnonParameter < params.length) { if(paramValue === undefined && nextAnonParameter < actualParams.length) {
paramValue = params[nextAnonParameter++].value; paramValue = actualParams[nextAnonParameter++].value;
} }
// If we've still not got a value, use the default, if any // If we've still not got a value, use the default, if any
paramValue = paramValue || paramInfo["default"] || ""; paramValue = paramValue || paramInfo["default"] || "";
// Replace any instances of this parameter // Replace any instances of this parameter
value = value.replace(new RegExp("\\$" + $tw.utils.escapeRegExp(paramInfo.name) + "\\$","mg"),paramValue); text = text.replace(new RegExp("\\$" + $tw.utils.escapeRegExp(paramInfo.name) + "\\$","mg"),paramValue);
} }
} }
return value; return text;
};
Widget.prototype.substituteVariableReferences = function(text) {
var self = this;
return text.replace(/\$\(([^\)\$]+)\)\$/g,function(match,p1,offset,string) {
return self.getVariable(p1,"");
});
}; };
/* /*