1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-19 16:24:51 +00:00

Experimental support for custom filter operators

Just as we can define custom widgets we can also define custom parameterised filter operators
This commit is contained in:
jeremy@jermolene.com 2022-05-06 15:01:17 +01:00
parent a9938a6c67
commit e01dfa1507
5 changed files with 123 additions and 2 deletions

View File

@ -244,13 +244,15 @@ exports.compileFilter = function(filterString) {
var operands = [], var operands = [],
operatorFunction; operatorFunction;
if(!operator.operator) { if(!operator.operator) {
// Use the "title" operator if no operator is specified
operatorFunction = filterOperators.title; operatorFunction = filterOperators.title;
} else if(!filterOperators[operator.operator]) { } else if(!filterOperators[operator.operator]) {
operatorFunction = filterOperators.field; // Unknown operators treated as "unknown" - at run time we can distinguish between a custom operator and falling back to the default "field" operator
operatorFunction = filterOperators.unknown;
} else { } else {
// Use the operator function
operatorFunction = filterOperators[operator.operator]; operatorFunction = filterOperators[operator.operator];
} }
$tw.utils.each(operator.operands,function(operand) { $tw.utils.each(operator.operands,function(operand) {
if(operand.indirect) { if(operand.indirect) {
operand.value = self.getTextReference(operand.text,"",currTiddlerTitle); operand.value = self.getTextReference(operand.text,"",currTiddlerTitle);

View File

@ -0,0 +1,59 @@
/*\
title: $:/core/modules/filters/unknown.js
type: application/javascript
module-type: filteroperator
Filter operator for handling unknown filter operators
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var fieldFilterOperatorFn = require("$:/core/modules/filters/field.js").field;
/*
Export our filter function
*/
exports.unknown = function(source,operator,options) {
var customDefinitionTitle = "[" + operator.operator + "[]]",
customDefinition = options.widget && options.widget.getVariableInfo && options.widget.getVariableInfo(customDefinitionTitle);
if(customDefinition && customDefinition.srcVariable) {
var variables = Object.create(null);
$tw.utils.each(customDefinition.srcVariable.variableParams,function(param,index) {
var value = operator.operands[index];
if(value === undefined) {
value = param["default"] || "";
}
variables[param.name] = value;
});
var getVariable = function(name,opts) {
if(name in variables) {
return variables[name];
} else {
return options.widget.getVariable(name,opts);
};
};
var getVariableInfo = function(name,opts) {
return options.widget.getVariableInfo(name,opts);
}
var list = options.wiki.filterTiddlers(customDefinition.srcVariable.value,{getVariable: getVariable,getVariableInfo: getVariableInfo},source);
if(operator.prefix === "!") {
var results = [];
source(function(tiddler,title) {
if(list.indexOf(title) === -1) {
results.push(title);
}
});
return results;
} else {
return list;
}
} else {
return fieldFilterOperatorFn(source,operator,options);
}
};
})();

View File

@ -0,0 +1,24 @@
title: CustomOperators/NestedParameterised
description: Nested parameterised custom operator usage
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
\function [dividebysomething[]](first:ignored,factor:0.5)
[divide[2]multiply<factor>]
\end
\function [multiplebysomething[]](first:ignored,factor:2)
[multiply[2]dividebysomething[],<factor>]
\end
<$text text={{{ [[123]multiplebysomething[]] }}}/>
-
<$text text={{{ [[123]multiplebysomething[x],[4]] }}}/>
+
title: ExpectedResult
<p>246-492</p>

View File

@ -0,0 +1,20 @@
title: CustomOperators/Parameterised
description: Parameterised custom operator usage
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
\function [multiplybysomething[]](first:ignored,factor:2)
[multiply[2]multiply<factor>]
\end
<$text text={{{ [[123]multiplybysomething[]] }}}/>
-
<$text text={{{ [[123]multiplybysomething[x],[4]] }}}/>
+
title: ExpectedResult
<p>492-984</p>

View File

@ -0,0 +1,16 @@
title: CustomOperators/Simple
description: Simple custom operator usage
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\function [multiplybytwo[]]()
[multiply[2]]
\end
<$text text={{{ [[123]multiplybytwo[]] }}}/>
+
title: ExpectedResult
<p>246</p>