diff --git a/core/modules/filters/lookup.js b/core/modules/filters/lookup.js index 2174e8c47..4af4ac681 100644 --- a/core/modules/filters/lookup.js +++ b/core/modules/filters/lookup.js @@ -5,9 +5,11 @@ module-type: filteroperator Filter operator that looks up values via a title prefix -[lookup:[]] +[lookup::[],[]] -Prepends the prefix to the selected items and returns the specified field value +Prepends the prefix to the selected items and returns the specified +field or index value. If the 2nd suffix does not exist, it defaults to field. +If the second operand is missing it defaults to "text" for fields, and "0" for indexes \*/ (function(){ @@ -20,10 +22,35 @@ Prepends the prefix to the selected items and returns the specified field value Export our filter function */ exports.lookup = function(source,operator,options) { - var results = []; - source(function(tiddler,title) { - results.push(options.wiki.getTiddlerText(operator.operand + title) || operator.suffix || ''); - }); + var results = [], + suffixes = operator.suffixes || [], + defaultSuffix = suffixes[0] ? (suffixes[0][0] || "") : "", + indexSuffix = (suffixes[1] && suffixes[1][0] === "index") ? true : false, + target; + if(operator.operands.length == 2) { + target = operator.operands[1] + } else { + target = indexSuffix ? "0": "text"; + } + if(indexSuffix) { + source(function(tiddler,title) { + var targetTitle = operator.operands[0] + title; + var data = options.wiki.extractTiddlerDataItem(targetTitle,target,defaultSuffix); + results.push(data); + }); + } else { + source(function(tiddler,title) { + var targetTitle = operator.operands[0] + title; + var targetTiddler = options.wiki.getTiddler(targetTitle); + if(targetTiddler) { + var value = targetTiddler.getFieldString(target); + if(value == "" && defaultSuffix !== "") { + value = defaultSuffix; + } + results.push(value); + } + }); + } return results; }; diff --git a/editions/tw5.com/tiddlers/filters/examples/lookup Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/lookup Operator (Examples).tid index 052e1d2bd..e923e8d5c 100644 --- a/editions/tw5.com/tiddlers/filters/examples/lookup Operator (Examples).tid +++ b/editions/tw5.com/tiddlers/filters/examples/lookup Operator (Examples).tid @@ -1,7 +1,10 @@ created: 20170907144257037 -modified: 20170907144559822 +modified: 20201224034837935 title: lookup Operator (Examples) type: text/vnd.tiddlywiki <<.operator-example 1 "[all[shadows+tiddlers]tag[$:/tags/PageControls]lookup[$:/config/PageControlButtons/Visibility/]]" "Retrieve the visibility status of each page control button">> <<.operator-example 2 "[all[shadows+tiddlers]tag[$:/tags/PageControls]lookup:show[$:/config/PageControlButtons/Visibility/]]" "Retrieve the visibility status of each page control button, this time with a default value">> +<<.operator-example 3 "[all[tiddlers]has[plugin-type]removeprefix[$:/plugins/tiddlywiki/]lookup:missing-description:field[$:/plugins/tiddlywiki/],[description]]" "Retrieve the description of all plugin-tiddlers that are in the `$:/plugins/tiddlywiki/` namespace.">> +<<.operator-example 4 "OriginalTiddlerPaths +[lookup:missing-index:index[$:/config/],[HelloThere]]" "Lookup the original tiddler path on disk for the [[Hello There]] tiddler.">> +<<.operator-example 5 "OriginalTiddlerPaths +[lookup:missing-index:index[$:/config/],[MissingTiddler]]" "Lookup the original tiddler path on disk for the [[MissingTiddler]] tiddler.">> diff --git a/editions/tw5.com/tiddlers/filters/lookup Operator.tid b/editions/tw5.com/tiddlers/filters/lookup Operator.tid index 9cbd6d5b5..4262564a4 100644 --- a/editions/tw5.com/tiddlers/filters/lookup Operator.tid +++ b/editions/tw5.com/tiddlers/filters/lookup Operator.tid @@ -1,24 +1,40 @@ caption: lookup created: 20170907103639431 -modified: 20170907144703051 +modified: 20210116081305739 op-input: a [[selection of titles|Title Selection]] -op-output: the lookup values corresponding to each input title -op-parameter: prefix applied to input titles to yield title of lookup tiddler from which value is retrieved -op-parameter-name: P -op-purpose: applies a prefix to each input title to yield the title of a tiddler from which the final value is retrieved -op-suffix: the default value to be used for missing lookups -op-suffix-name: D +op-output: the lookup values corresponding to each lookup tiddler +op-parameter: prefix applied to input titles to yield title of lookup tiddler from which value is retrieved. Now accepts 1 or 2 parameters, see below for details +op-parameter-name: P, T +op-purpose: applies a prefix to each input title to yield the title of a tiddler from which the final value is retrieved. With a single parameter, the default field is "text" and the default index is "0". If a second parameter is provided, that becomes the target field or index. +op-suffix: the default value to be used for missing lookups. This operator can now accept a second suffix, see below for details +op-suffix-name: D, I tags: [[Filter Operators]] title: lookup Operator type: text/vnd.tiddlywiki <<.from-version "5.1.15">> -The action of this operator is as follows: +The action of this operator is as follows with 1 parameter: * Apply the specified prefix to each input tiddler title, yielding a new list of tiddler titles -* Transclude the value of each of those tiddlers -** Substitute the default value for missing or empty tiddlers +* Transclude the value of the `text` field each of those tiddlers +** Substitute the default value for missing or empty values +* Return the list of values + +<<.from-version "5.1.24">> + +The action of this operator is as follows with 2 parameters: + +If there are two parameters provided, use the second parameter as the target field or index. + +<<.note """If there is only one parameter given, the filter checks for a second suffix equal to "index". If this suffix is found, the default target index is "0". +In all other cases, the default target field is "text".""">> + +Then: + +* Apply the specified prefix to each input tiddler title, yielding a new list of tiddler titles +* Transclude the value of the target field or index +** Substitute the default value for missing or empty values * Return the list of values <<.operator-examples "lookup">>