mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 03:57:21 +00:00
CurrentTiddler variable consistency in subfilters and prefixes (#5691)
* Make currentTiddler variable consistent in subfilters and filter run prefixes * Updated filterun prefix and subfilter operators to use ..currentTiddler instead of outerCurrentTiddler
This commit is contained in:
parent
bf25c4d34a
commit
3fc7895af2
@ -17,10 +17,21 @@ exports.filter = function(operationSubFunction,options) {
|
||||
return function(results,source,widget) {
|
||||
if(results.length > 0) {
|
||||
var resultsToRemove = [];
|
||||
results.each(function(result) {
|
||||
var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([result]),widget);
|
||||
results.each(function(title) {
|
||||
var filtered = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
||||
getVariable: function(name) {
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
case "..currentTiddler":
|
||||
return widget.getVariable("currentTiddler");
|
||||
default:
|
||||
return widget.getVariable(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
if(filtered.length === 0) {
|
||||
resultsToRemove.push(result);
|
||||
resultsToRemove.push(title);
|
||||
}
|
||||
});
|
||||
results.remove(resultsToRemove);
|
||||
|
@ -19,23 +19,25 @@ exports.reduce = function(operationSubFunction,options) {
|
||||
var index = 0;
|
||||
results.each(function(title) {
|
||||
var list = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
||||
getVariable: function(name) {
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
case "accumulator":
|
||||
return "" + accumulator;
|
||||
case "index":
|
||||
return "" + index;
|
||||
case "revIndex":
|
||||
return "" + (results.length - 1 - index);
|
||||
case "length":
|
||||
return "" + results.length;
|
||||
default:
|
||||
return widget.getVariable(name);
|
||||
}
|
||||
getVariable: function(name) {
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
case "..currentTiddler":
|
||||
return widget.getVariable("currentTiddler");
|
||||
case "accumulator":
|
||||
return "" + accumulator;
|
||||
case "index":
|
||||
return "" + index;
|
||||
case "revIndex":
|
||||
return "" + (results.length - 1 - index);
|
||||
case "length":
|
||||
return "" + results.length;
|
||||
default:
|
||||
return widget.getVariable(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
if(list.length > 0) {
|
||||
accumulator = "" + list[0];
|
||||
}
|
||||
|
@ -26,15 +26,17 @@ exports.sort = function(operationSubFunction,options) {
|
||||
compareFn;
|
||||
results.each(function(title) {
|
||||
var key = operationSubFunction(options.wiki.makeTiddlerIterator([title]),{
|
||||
getVariable: function(name) {
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
default:
|
||||
return widget.getVariable(name);
|
||||
}
|
||||
getVariable: function(name) {
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
case "..currentTiddler":
|
||||
return widget.getVariable("currentTiddler");
|
||||
default:
|
||||
return widget.getVariable(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
sortKeys.push(key[0] || "");
|
||||
});
|
||||
results.clear();
|
||||
|
@ -20,7 +20,18 @@ exports.filter = function(source,operator,options) {
|
||||
results = [],
|
||||
target = operator.prefix !== "!";
|
||||
source(function(tiddler,title) {
|
||||
var list = filterFn.call(options.wiki,options.wiki.makeTiddlerIterator([title]),options.widget);
|
||||
var list = filterFn.call(options.wiki,options.wiki.makeTiddlerIterator([title]),{
|
||||
getVariable: function(name) {
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
case "..currentTiddler":
|
||||
return options.widget.getVariable("currentTiddler");
|
||||
default:
|
||||
return options.widget.getVariable(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
if((list.length > 0) === target) {
|
||||
results.push(title);
|
||||
}
|
||||
|
@ -31,6 +31,8 @@ exports.reduce = function(source,operator,options) {
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
case "..currentTiddler":
|
||||
return options.widget.getVariable("currentTiddler");
|
||||
case "accumulator":
|
||||
return "" + accumulator;
|
||||
case "index":
|
||||
|
@ -27,10 +27,13 @@ exports.sortsub = function(source,operator,options) {
|
||||
iterator(options.wiki.getTiddler(title),title);
|
||||
},{
|
||||
getVariable: function(name) {
|
||||
if(name === "currentTiddler") {
|
||||
return title;
|
||||
} else {
|
||||
return options.widget.getVariable(name);
|
||||
switch(name) {
|
||||
case "currentTiddler":
|
||||
return "" + title;
|
||||
case "..currentTiddler":
|
||||
return options.widget.getVariable("currentTiddler");
|
||||
default:
|
||||
return options.widget.getVariable(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -1,6 +1,6 @@
|
||||
caption: filter
|
||||
created: 20200929174420821
|
||||
modified: 20201027185144953
|
||||
modified: 20210522162551921
|
||||
op-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
||||
op-neg-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
||||
op-neg-output: those input titles that <<.em "do not">> pass the filter <<.place S>>
|
||||
@ -28,6 +28,8 @@ Simple filter operations can be concatenated together directly (eg `[tag[HelloTh
|
||||
</$vars>
|
||||
```
|
||||
|
||||
Note that within the subfilter, the "currentTiddler" variable is set to the title of the tiddler being processed. The value of currentTiddler outside the subfilter is available in the variable "..currentTiddler". <<.from-version "5.1.24">>
|
||||
|
||||
<<.tip "Compare with the similar [[subfilter|subfilter Operator]] operator which runs a subfilter and directly returns the results">>
|
||||
|
||||
<<.tip "Compare with the analagous named filter run prefix `:filter`">>
|
||||
|
@ -1,6 +1,6 @@
|
||||
caption: reduce
|
||||
created: 20201004154131193
|
||||
modified: 20201208185109549
|
||||
modified: 20210522162536854
|
||||
op-input: a [[selection of titles|Title Selection]] passed as input to the filter
|
||||
op-output: the final result of running the subfilter <<.place S>>
|
||||
op-parameter: a [[filter expression|Filter Expression]]. Optional second parameter for initial value for accumulator
|
||||
@ -18,6 +18,7 @@ The following variables are available within the subfilter:
|
||||
|
||||
* ''accumulator'' - the result of the previous subfilter run
|
||||
* ''currentTiddler'' - the input title
|
||||
* ''..currentTiddler'' - the value of the variable `currentTiddler` outside the subfilter. <<.from-version "5.1.24">>
|
||||
* ''index'' - the numeric index of the current list item (with zero being the first item in the list)
|
||||
* ''revIndex'' - the reverse numeric index of the current list item (with zero being the last item in the list)
|
||||
* ''length'' - the total length of the input list
|
||||
|
@ -1,6 +1,6 @@
|
||||
caption: sortsub
|
||||
created: 20200424160155182
|
||||
modified: 20210428152533501
|
||||
modified: 20210522162521222
|
||||
op-input: a [[selection of titles|Title Selection]]
|
||||
op-neg-output: the input, sorted into reverse order by the result of evaluating subfilter <<.param S>>
|
||||
op-output: the input, sorted into ascending order by the result of evaluating subfilter <<.param S>>
|
||||
@ -15,7 +15,9 @@ type: text/vnd.tiddlywiki
|
||||
|
||||
Each item in the list of input titles is passed to the subfilter in turn. The subfilter transforms the input titles into the form needed for sorting. For example, the subfilter `[length[]]` transforms each input title in the number representing its length, and thus sorts the input titles according to their length.
|
||||
|
||||
Note that within the subfilter, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits subfilters like `[{!!value}divide{!!cost}]` to be used for computation.
|
||||
Note that within the subfilter, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits subfilters like `[{!!value}divide{!!cost}]` to be used for computation.
|
||||
|
||||
The value of currentTiddler outside the subfilter is available in the variable "..currentTiddler". <<.from-version "5.1.24">>
|
||||
|
||||
The suffix <<.place T>> determines how the items are compared and can be:
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
created: 20150124182421000
|
||||
modified: 20210428084144231
|
||||
modified: 20210522162642994
|
||||
tags: [[Filter Syntax]]
|
||||
title: Filter Expression
|
||||
type: text/vnd.tiddlywiki
|
||||
@ -33,6 +33,8 @@ If a run has:
|
||||
|
||||
<<.tip "Compare named filter run prefix `:reduce` with [[reduce Operator]] which is used to used to flatten a list of items down to a single item by repeatedly applying a subfilter.">>
|
||||
|
||||
<<.tip """Within the filter runs prefixed with `:reduce`, `:sort` and `:filter`, the "currentTiddler" variable is set to the title of the tiddler being processed. The value of currentTiddler outside the subfilter is available in the variable "..currentTiddler".<<.from-version "5.1.24">>""" >>
|
||||
|
||||
In technical / logical terms:
|
||||
|
||||
|!Run |!Equivalent named prefix |!Interpretation |!Output |
|
||||
|
@ -1,5 +1,5 @@
|
||||
created: 20210428083929749
|
||||
modified: 20210428140713422
|
||||
modified: 20210522162628946
|
||||
tags: [[Filter Syntax]] [[Filter Run Prefix]]
|
||||
title: Sort Filter Run Prefix
|
||||
type: text/vnd.tiddlywiki
|
||||
@ -13,7 +13,7 @@ type: text/vnd.tiddlywiki
|
||||
|
||||
Each input title from previous runs is passed to this run in turn. The filter run transforms the input titles into the form needed for sorting. For example, the filter run `[length[]]` transforms each input title in to the number representing its length, and thus sorts the input titles according to their length.
|
||||
|
||||
Note that within the filter run, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits filter runs like `:sort:number[{!!value}divide{!!cost}]` to be used for computation.
|
||||
Note that within the filter run, the "currentTiddler" variable is set to the title of the tiddler being processed. This permits filter runs like `:sort:number[{!!value}divide{!!cost}]` to be used for computation. The value of currentTiddler outside the run is available in the variable "..currentTiddler".
|
||||
|
||||
The `:sort` filter run prefix uses an extended syntax that allows for multiple suffixes, some of which are required:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user