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

Support macro parameters in filter run prefixes (#6164)

* Support macro params in filter run prefixes and support substitution for variables set by filter run prefixes

* feat: add support macro parameters and variable substitution for all filter run prefixes

* fix: rename options argument to opts for disambiguation

* feat: add support for macro params to cascade filterrun prefix
This commit is contained in:
Saq Imtiaz 2021-11-23 14:51:42 +01:00 committed by GitHub
parent 7fcc84156e
commit 76cdc17f3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 86 additions and 62 deletions

View File

@ -26,17 +26,19 @@ exports.cascade = function(operationSubFunction,options) {
filterFnList[index] = options.wiki.compileFilter(filter); filterFnList[index] = options.wiki.compileFilter(filter);
} }
var output = filterFnList[index](options.wiki.makeTiddlerIterator([title]),{ var output = filterFnList[index](options.wiki.makeTiddlerIterator([title]),{
getVariable: function(name) { getVariable: function(name,opts) {
switch(name) { opts = opts || {};
case "currentTiddler": opts.variables = {
return "" + title; "currentTiddler": "" + title,
case "..currentTiddler": "..currentTiddler": widget.getVariable("currentTiddler")
return widget.getVariable("currentTiddler"); };
default: if(name in opts.variables) {
return widget.getVariable(name); return opts.variables[name];
} } else {
return widget.getVariable(name,opts);
} }
}); }
});
if(output.length !== 0) { if(output.length !== 0) {
result = output[0]; result = output[0];
return false; return false;

View File

@ -16,23 +16,30 @@ Export our filter function
exports.filter = function(operationSubFunction,options) { exports.filter = function(operationSubFunction,options) {
return function(results,source,widget) { return function(results,source,widget) {
if(results.length > 0) { if(results.length > 0) {
var resultsToRemove = []; var resultsToRemove = [],
index = 0;
results.each(function(title) { results.each(function(title) {
var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{ var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
getVariable: function(name) { getVariable: function(name,opts) {
switch(name) { opts = opts || {};
case "currentTiddler": opts.variables = {
return "" + title; "currentTiddler": "" + title,
case "..currentTiddler": "..currentTiddler": widget.getVariable("currentTiddler"),
return widget.getVariable("currentTiddler"); "index": "" + index,
default: "revIndex": "" + (results.length - 1 - index),
return widget.getVariable(name); "length": "" + results.length
};
if(name in opts.variables) {
return opts.variables[name];
} else {
return widget.getVariable(name,opts);
} }
} }
}); });
if(filtered.length === 0) { if(filtered.length === 0) {
resultsToRemove.push(title); resultsToRemove.push(title);
} }
++index;
}); });
results.remove(resultsToRemove); results.remove(resultsToRemove);
} }

View File

@ -20,20 +20,19 @@ exports.map = function(operationSubFunction,options) {
results.clear(); results.clear();
$tw.utils.each(inputTitles,function(title) { $tw.utils.each(inputTitles,function(title) {
var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{ var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
getVariable: function(name) { getVariable: function(name,opts) {
switch(name) { opts = opts || {};
case "currentTiddler": opts.variables = {
return "" + title; "currentTiddler": "" + title,
case "..currentTiddler": "..currentTiddler": widget.getVariable("currentTiddler"),
return widget.getVariable("currentTiddler"); "index": "" + index,
case "index": "revIndex": "" + (inputTitles.length - 1 - index),
return "" + index; "length": "" + inputTitles.length
case "revIndex": };
return "" + (inputTitles.length - 1 - index); if(name in opts.variables) {
case "length": return opts.variables[name];
return "" + inputTitles.length; } else {
default: return widget.getVariable(name,opts);
return widget.getVariable(name);
} }
} }
}); });

View File

@ -15,26 +15,24 @@ Export our filter prefix function
exports.reduce = function(operationSubFunction,options) { exports.reduce = function(operationSubFunction,options) {
return function(results,source,widget) { return function(results,source,widget) {
if(results.length > 0) { if(results.length > 0) {
var accumulator = ""; var accumulator = "",
var index = 0; index = 0;
results.each(function(title) { results.each(function(title) {
var list = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{ var list = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
getVariable: function(name) { getVariable: function(name,opts) {
switch(name) { opts = opts || {};
case "currentTiddler": opts.variables = {
return "" + title; "currentTiddler": "" + title,
case "..currentTiddler": "..currentTiddler": widget.getVariable("currentTiddler"),
return widget.getVariable("currentTiddler"); "index": "" + index,
case "accumulator": "revIndex": "" + (results.length - 1 - index),
return "" + accumulator; "length": "" + results.length,
case "index": "accumulator": "" + accumulator
return "" + index; };
case "revIndex": if(name in opts.variables) {
return "" + (results.length - 1 - index); return opts.variables[name];
case "length": } else {
return "" + results.length; return widget.getVariable(name,opts);
default:
return widget.getVariable(name);
} }
} }
}); });

View File

@ -26,14 +26,16 @@ exports.sort = function(operationSubFunction,options) {
compareFn; compareFn;
results.each(function(title) { results.each(function(title) {
var key = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{ var key = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
getVariable: function(name) { getVariable: function(name,opts) {
switch(name) { opts = opts || {};
case "currentTiddler": opts.variables = {
return "" + title; "currentTiddler": "" + title,
case "..currentTiddler": "..currentTiddler": widget.getVariable("currentTiddler")
return widget.getVariable("currentTiddler"); };
default: if(name in opts.variables) {
return widget.getVariable(name); return opts.variables[name];
} else {
return widget.getVariable(name,opts);
} }
} }
}); });

View File

@ -122,7 +122,7 @@ Widget.prototype.getVariableInfo = function(name,options) {
}); });
// Only substitute variable references if this variable was defined with the \define pragma // Only substitute variable references if this variable was defined with the \define pragma
if(variable.isMacroDefinition) { if(variable.isMacroDefinition) {
value = this.substituteVariableReferences(value); value = this.substituteVariableReferences(value,options);
} }
return { return {
text: value, text: value,
@ -175,10 +175,10 @@ Widget.prototype.resolveVariableParameters = function(formalParams,actualParams)
return results; return results;
}; };
Widget.prototype.substituteVariableReferences = function(text) { Widget.prototype.substituteVariableReferences = function(text,options) {
var self = this; var self = this;
return (text || "").replace(/\$\(([^\)\$]+)\)\$/g,function(match,p1,offset,string) { return (text || "").replace(/\$\(([^\)\$]+)\)\$/g,function(match,p1,offset,string) {
return self.getVariable(p1,{defaultValue: ""}); return options.variables && options.variables[p1] || (self.getVariable(p1,{defaultValue: ""}));
}); });
}; };

View File

@ -419,6 +419,22 @@ describe("'reduce' and 'intersection' filter prefix tests", function() {
// Prepend the position in the list using the index and length variables // Prepend the position in the list using the index and length variables
expect(wiki.filterTiddlers("[tag[shopping]] :map[get[title]addprefix[-]addprefix<length>addprefix[of]addprefix<index>]").join(",")).toBe("0of4-Brownies,1of4-Chick Peas,2of4-Milk,3of4-Rice Pudding"); expect(wiki.filterTiddlers("[tag[shopping]] :map[get[title]addprefix[-]addprefix<length>addprefix[of]addprefix<index>]").join(",")).toBe("0of4-Brownies,1of4-Chick Peas,2of4-Milk,3of4-Rice Pudding");
}); });
it("should handle macro parameters for filter run prefixes",function() {
var widget = require("$:/core/modules/widgets/widget.js");
var rootWidget = new widget.widget({ type:"widget", children:[ {type:"widget", children:[]} ] },
{ wiki:wiki, document:$tw.document});
rootWidget.makeChildWidgets();
var anchorWidget = rootWidget.children[0];
rootWidget.setVariable("greet","Hello $name$",[{name:"name"}],true);
rootWidget.setVariable("echo","$text$",[{name:"text"}],true);
// :map prefix
expect(wiki.filterTiddlers("1 :map[subfilter<greet Tom>join[ ]]",anchorWidget).join(",")).toBe("Hello Tom");
expect(wiki.filterTiddlers('[tag[shopping]] :map[<echo "$(index)$ $(currentTiddler)$">]',anchorWidget).join(",")).toBe("0 Brownies,1 Chick Peas,2 Milk,3 Rice Pudding");
// :reduce prefix
expect(wiki.filterTiddlers("1 :reduce[subfilter<greet Tom>join[ ]]",anchorWidget).join(",")).toBe("Hello Tom");
expect(wiki.filterTiddlers('[tag[shopping]] :reduce[<echo "$(accumulator)$ $(index)$ $(currentTiddler)$,">]',anchorWidget).join(",")).toBe(" 0 Brownies, 1 Chick Peas, 2 Milk, 3 Rice Pudding,");
});
}); });
})(); })();