Adds Text substitution support in widget attributes and new operator (#7526)

* feat: new text substitution support, first pass

* fix: use the widget method instead of evaluating a filter

* revert to earlier implementation that emulates macro syntax

* fix: capitalize comments

* feat: add support for triple backticks for substituted attributes

* docs: added docs for substitute operator

* chore: more docs tweaks

* docs: substituted attributes, refactored docs for widget attributes

* docs: fixed typo

* docs: more examples for substituted attributes

* docs: updated prior documentation on concatenating text and variables

* docs: documentation corrections

* Update editions/tw5.com/tiddlers/filters/examples/substitute Operator (Examples).tid

Co-authored-by: btheado <brian.theado@gmail.com>

---------

Co-authored-by: btheado <brian.theado@gmail.com>
This commit is contained in:
Saq Imtiaz 2023-06-24 15:57:15 +02:00 committed by GitHub
parent e5566543c9
commit 3825e2579f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 361 additions and 66 deletions

View File

@ -0,0 +1,36 @@
/*\
title: $:/core/modules/filters/substitute.js
type: application/javascript
module-type: filteroperator
Filter operator for substituting variables and embedded filter expressions with their corresponding values
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.substitute = function(source,operator,options) {
var results = [],
operands = [];
$tw.utils.each(operator.operands,function(operand,index){
operands.push({
name: (index + 1).toString(),
value: operand
});
});
source(function(tiddler,title) {
if(title) {
results.push(options.wiki.getSubstitutedText(title,options.widget,{substitutions:operands}));
}
});
return results;
};
})();

View File

@ -305,10 +305,11 @@ exports.parseAttribute = function(source,pos) {
start: pos
};
// Define our regexps
var reAttributeName = /([^\/\s>"'=]+)/g,
reUnquotedAttribute = /([^\/\s<>"'=]+)/g,
var reAttributeName = /([^\/\s>"'`=]+)/g,
reUnquotedAttribute = /([^\/\s<>"'`=]+)/g,
reFilteredValue = /\{\{\{([\S\s]+?)\}\}\}/g,
reIndirectValue = /\{\{([^\}]+)\}\}/g;
reIndirectValue = /\{\{([^\}]+)\}\}/g,
reSubstitutedValue = /(?:```([\s\S]*?)```|`([^`]|[\S\s]*?)`)/g;
// Skip whitespace
pos = $tw.utils.skipWhiteSpace(source,pos);
// Get the attribute name
@ -361,8 +362,15 @@ exports.parseAttribute = function(source,pos) {
node.type = "macro";
node.value = macroInvocation;
} else {
node.type = "string";
node.value = "true";
var substitutedValue = $tw.utils.parseTokenRegExp(source,pos,reSubstitutedValue);
if(substitutedValue) {
pos = substitutedValue.end;
node.type = "substituted";
node.rawValue = substitutedValue.match[1] || substitutedValue.match[2];
} else {
node.type = "string";
node.value = "true";
}
}
}
}

View File

@ -380,6 +380,8 @@ Widget.prototype.computeAttribute = function(attribute) {
} else if(attribute.type === "macro") {
var variableInfo = this.getVariableInfo(attribute.value.name,{params: attribute.value.params});
value = variableInfo.text;
} else if(attribute.type === "substituted") {
value = this.wiki.getSubstitutedText(attribute.rawValue,this) || "";
} else { // String attribute
value = attribute.value;
}

View File

@ -1063,6 +1063,34 @@ exports.getTextReferenceParserInfo = function(title,field,index,options) {
return parserInfo;
}
/*
Parse a block of text of a specified MIME type
text: text on which to perform substitutions
widget
options: see below
Options include:
substitutions: an optional array of substitutions
*/
exports.getSubstitutedText = function(text,widget,options) {
options = options || {};
text = text || "";
var self = this,
substitutions = options.substitutions || [],
output;
// Evaluate embedded filters and substitute with first result
output = text.replace(/\$\{([\S\s]+?)\}\$/g, function(match,filter) {
return self.filterTiddlers(filter,widget)[0] || "";
});
// Process any substitutions provided in options
$tw.utils.each(substitutions,function(substitute) {
output = $tw.utils.replaceString(output,new RegExp("\\$" + $tw.utils.escapeRegExp(substitute.name) + "\\$","mg"),substitute.value);
});
// Substitute any variable references with their values
return output.replace(/\$\((\w+)\)\$/g, function(match,varname) {
return widget.getVariable(varname,{defaultValue: ""})
});
};
/*
Make a widget tree for a parse tree
parser: parser object

View File

@ -0,0 +1,40 @@
title: Filters/substitute
description: Test substitute operator
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: substitute filter data 1
tags: Hello There [[Welcome to TiddlyWiki]] GettingStarted
TiddlyWiki
+
title: substitute filter data 2
The output of the filter `[[substitute filter data 1]tags[]]` is ${[[substitute filter data 1]tags[]]}$.
+
title: substitute filter data 3
Welcome to $(projectname)$ $1$ $2$ $3$. Tiddlers starting with `substitute`: ${[prefix[substitute]format:titlelist[]join[ ]]}$.
+
title: Output
\whitespace trim
<$let projectname="TiddlyWiki">
(<$text text={{{ [[]substitute[]] }}}/>)
(<$text text={{{ [[Hello There, welcome to $TiddlyWiki$]substitute[]] }}}/>)
(<$text text={{{ [[Welcome to $(projectname)$]substitute[]] }}}/>)
(<$text text={{{ [[Welcome to $(projectname)$ $1$]substitute[today]] }}}/>)
(<$text text={{{ [[This is not a valid embedded filter ${ hello )$]substitute[]] }}}/>)
(<$text text={{{ [{substitute filter data 2}substitute[]] }}}/>)
(<$text text={{{ [{substitute filter data 3}substitute[every],[day]] }}}/>)
</$let>
+
title: ExpectedResult
<p>()
(Hello There, welcome to $TiddlyWiki$)
(Welcome to TiddlyWiki)
(Welcome to TiddlyWiki today)
(This is not a valid embedded filter ${ hello )$)
(The output of the filter `[[substitute filter data 1]tags[]]` is Hello.)
(Welcome to TiddlyWiki every day $3$. Tiddlers starting with `substitute`: [[substitute filter data 1]] [[substitute filter data 2]] [[substitute filter data 3]].)</p>

View File

@ -0,0 +1,19 @@
title: Widgets/SubstitutedAttributes
description: Attributes specified as string that should have substitution performed.
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
<$let project="TiddlyWiki" disabled="true">
<div class=`$(project)$
${ [[Hello]addsuffix[There]] }$` attrib=`myvalue` otherattrib=`$(1)$` blankattrib=`` quoted="here" disabled=```$(disabled)$```>
</div>
</$let>
+
title: ExpectedResult
<p><div attrib="myvalue" blankattrib="" class="TiddlyWiki
HelloThere" disabled="true" otherattrib="" quoted="here"></div></p>

View File

@ -161,6 +161,16 @@ describe("HTML tag new parser tests", function() {
expect($tw.utils.parseAttribute(" attrib1>",0)).toEqual(
{ type : 'string', value : 'true', start : 0, name : 'attrib1', end : 8 }
);
expect($tw.utils.parseAttribute("p=`blah` ",1)).toEqual(null);
expect($tw.utils.parseAttribute("p=`blah` ",0)).toEqual(
{ start: 0, name: 'p', type: 'substituted', rawValue: 'blah', end: 8 }
);
expect($tw.utils.parseAttribute("p=```blah``` ",0)).toEqual(
{ start: 0, name: 'p', type: 'substituted', rawValue: 'blah', end: 12 }
);
expect($tw.utils.parseAttribute("p=`Hello \"There\"`",0)).toEqual(
{ start: 0, name: 'p', type: 'substituted', rawValue: 'Hello "There"', end: 17 }
);
});
it("should parse HTML tags", function() {

View File

@ -0,0 +1,37 @@
created: 20230614225302905
modified: 20230614233448662
tags: [[Operator Examples]] [[substitute Operator]]
title: substitute Operator (Examples)
type: text/vnd.tiddlywiki
\define time() morning
\define field() modified
\procedure sentence() This tiddler was last $(field)$ on ${[{!!modified}format:date[DDth MMM YYYY]]}$
\define name() Bugs Bunny
\define address() Rabbit Hole Hill
!Substitute <<.op substitute[]>> operator parameters
<<.operator-example 1 "[[Hi, I'm $1$ and I live in $2$]substitute[Bugs Bunny],[Rabbit Hole Hill]]">>
!Substitute variables
This example uses the following variables:
* name: <$codeblock code=<<name>>/>
* address: <$codeblock code=<<address>>/>
<<.inline-operator-example "[[Hi, I'm $(name)$ and I live in $(address)$]substitute[]]">>
!Substitute variables and operator parameters
This example uses the following variable:
* time: <$codeblock code=<<time>>/>
<<.inline-operator-example "[[Something in the $(time)$ at $2$ about $1$ ]substitute[Maths],[the Library]]">>
!Substitute a filter expression and a variable
This example uses the following variables:
* field: <$codeblock code=<<field>>/>
* sentence: <$codeblock code=<<sentence>>/>
<<.inline-operator-example "[<sentence>substitute[]]">>

View File

@ -0,0 +1,27 @@
caption: substitute
created: 20230614223551834
modified: 20230615173049692
op-input: a [[selection of titles|Title Selection]]
op-output: the input titles with placeholders for filter expressions, parameter and variables replaced with their corresponding values
op-parameter: the <<.op substitute>> operator optionally accepts a variable number of parameters, see below for details
op-purpose: returns each item in the list, replacing within each title placeholders for filters, parameters and variables with their corresponding values
tags: [[Filter Operators]] [[String Operators]]
title: substitute Operator
type: text/vnd.tiddlywiki
<<.from-version "5.3.0">>
The <<.op substitute>> operator replaces any placeholders in the input titles in the following order:
# filter expressions
# parameters to the <<.op substitute>> operator
# variables
|placeholder syntax|description|h
|`$n$`|Text substitution of a parameter provided to the operator, where n is the position of the parameter starting with 1 for the first parameter. Unmatched placeholders pass through unchanged.|
|`$(varname)$`|Text substitution of a variable. Undefined variables are replaced with an empty string.|
|`${ filter expression }$`|Text substitution with the first result of evaluating the filter expression. |
<<.tip """Placeholders that contain square bracket characters are not valid filter syntax when used directly in a filter expression. However they can be provided as input to the <$macrocall $name=".op" _="substitute"/> operator as text references or variables""">>
<<.operator-examples "substitute">>

View File

@ -1,9 +1,10 @@
created: 20160424150551727
modified: 20211230153027382
modified: 20230615114519672
tags: Learning
title: Concatenating text and variables using macro substitution
type: text/vnd.tiddlywiki
<<.from-version "5.3.0">> It is recommended to use [[substituted attributes|Substituted Attribute Values]] or the [[substitute filter operator|substitute Operator]] to concatenate text and variables.
It's a frequent use case in ~TiddlyWiki that you will want to put the results of variables together with various bits of strings of text. This process in some programming languages is often referred to as "concatenating" text.

View File

@ -0,0 +1,16 @@
created: 20230615045132842
modified: 20230615045231048
tags: WikiText [[Widget Attributes]]
title: Filtered Attribute Values
type: text/vnd.tiddlywiki
Filtered attribute values are indicated with triple curly braces around a [[Filter Expression]]. The value will be the first item in the resulting list, or the empty string if the list is empty.
<<.from-version "5.2.2">> To improve readability, newlines can be included anywhere that whitespace is allowed within filtered attributes.
This example shows how to add a prefix to a value:
```
<$text text={{{ [<currentTiddler>addprefix[$:/myprefix/]] }}} />
```
<<.warning "The value of the attribute will be the exact text from the first item in the resulting list. Any wiki syntax in that text will be left as-is.">>

View File

@ -1,6 +1,6 @@
caption: HTML
created: 20131205160816081
modified: 20230115100934146
modified: 20230615060119987
tags: WikiText
title: HTML in WikiText
type: text/vnd.tiddlywiki
@ -58,12 +58,13 @@ If you do not close any other tag then it will behave as if the missing closing
! Attributes
In an extension of conventional HTML syntax, attributes of elements/widgets can be specified in several different ways:
In an extension of conventional HTML syntax, attributes of elements and widgets can be specified in [[several different ways|Widget Attributes]]:
* a literal string
* a transclusion of a TextReference
* a transclusion of a [[macro/variable|Macros]]
* as the result of a [[Filter Expression]]
* [[a literal string|Literal Attribute Values]]
* [[a transclusion of a textReference|Transcluded Attribute Values]]
* [[a transclusion of a macro/variable|Transcluded Attribute Values]]
* [[as the result of a filter expression|Filtered Attribute Values]]
* <<.from-version "5.3.0">> [[as the result of performing filter and variable substitutions on the given string|Substituted Attribute Values]]
!! Style Attributes
@ -85,64 +86,10 @@ The advantage of this syntax is that it simplifies assigning computed values to
<div style.color={{!!color}}>Hello</div>
```
!! Literal Attribute Values
Literal attribute values can use several different styles of quoting:
* Single quotes (eg `attr='value'`)
* Double quotes (eg `attr="value"`)
* Tripe double quotes (eg `attr="""value"""`)
* No quoting is necessary for values that do not contain spaces (eg `attr=value`)
Literal attribute values can include line breaks. For example:
```
<div data-address="Mouse House,
Mouse Lane,
Rodentville,
Ratland."/>
```
By using triple-double quotes you can specify attribute values that contain single double quotes. For example:
```
<div data-address="""Mouse House,
"Mouse" Lane,
Rodentville,
Ratland."""/>
```
!! Transcluded Attribute Values
Transcluded attribute values are indicated with double curly braces around a TextReference. For example:
```
attr={{tiddler}}
attr={{!!field}}
attr={{tiddler!!field}}
```
<<.warning "The value of the attribute value will be the exact text retrieved from the TextReference. Any wiki syntax in that text will be left as-is.">>
!! Variable Attribute Values
Variable attribute values are indicated with double angle brackets around a [[macro invocation|Macro Calls]]. For example:
```
<div title=<<MyMacro "Brian">>>
...
</div>
```
<<.warning "The text from the definition of the macro will be retrieved and text substitution will be performed (i.e. <<.param $param$>> and <<.param &#36;(...)&#36;>> syntax). The value of the attribute value will be the resulting text. Any wiki syntax in that text (including further macro calls and variable references) will be left as-is.">>
!! Filtered Attribute Values
Filtered attribute values are indicated with triple curly braces around a [[Filter Expression]]. The value will be the first item in the resulting list, or the empty string if the list is empty.
<<.from-version "5.2.2">> To improve readability, newlines can be included anywhere that whitespace is allowed within filtered attributes.
This example shows how to add a prefix to a value:
```
<$text text={{{ [<currentTiddler>addprefix[$:/myprefix/]] }}} />
```
<<.warning "The value of the attribute will be the exact text from the first item in the resulting list. Any wiki syntax in that text will be left as-is.">>

View File

@ -0,0 +1,30 @@
created: 20230615045409162
modified: 20230615045432768
tags: [[Widget Attributes]] WikiText
title: Literal Attribute Values
type: text/vnd.tiddlywiki
Literal attribute values can use several different styles of quoting:
* Single quotes (eg `attr='value'`)
* Double quotes (eg `attr="value"`)
* Tripe double quotes (eg `attr="""value"""`)
* No quoting is necessary for values that do not contain spaces (eg `attr=value`)
Literal attribute values can include line breaks. For example:
```
<div data-address="Mouse House,
Mouse Lane,
Rodentville,
Ratland."/>
```
By using triple-double quotes you can specify attribute values that contain single double quotes. For example:
```
<div data-address="""Mouse House,
"Mouse" Lane,
Rodentville,
Ratland."""/>
```

View File

@ -0,0 +1,45 @@
base-url: http://tiddlywiki.com/
created: 20230615050814821
modified: 20230615173033918
tags: [[Widget Attributes]] WikiText
title: Substituted Attribute Values
type: text/vnd.tiddlywiki
<<.from-version "5.3.0">>
Substituted attribute values can use two different styles of quoting:
* Single backticks <$codeblock code="attr=`value`"/>
* Triple backticks <$codeblock code="attr=```value```"/>
The value of the attribute will be the text denoted by the backticks with any of the placeholders for filter expressions and variables substituted with their corresponding values. Filter expression placeholders are substituted before variable placeholders, allowing for further variable substitution in their returned value.
<<.warning "Any other wiki syntax in that text will be left as-is.">>
|placeholder syntax|description|h
|`$(varname)$`|Text substitution of a variable. Undefined variables are replaced with an empty string. |
|`${ filter expression }$`|Text substitution with the first result of evaluating the filter expression. |
! Examples
!! Substituting a variable value into a string
<$macrocall $name=wikitext-example-without-html src='<$text text=`Hello there this is the tiddler "$(currentTiddler)$"`/>'/>
!! Substituting a variable value and the result of evaluating a filter expression into a string
<$macrocall $name=wikitext-example-without-html src='<$text text=`This tiddler is titled "$(currentTiddler)$" and was last modified on ${[{!!modified}format:date[DDth MMM YYYY]]}$`/>'/>
!! Concatenating strings and variables to create a URL
<$macrocall $name=wikitext-example-without-html src='
<$let hash={{{ [<currentTiddler>encodeuricomponent[]] }}}>
<a href=`http://tiddlywiki.com/#$(hash)$`>this tiddler on tiddlywiki.com</a>
</$let>'/>
!! Concatenating variables and a text reference to create a URL
<$macrocall $name=wikitext-example-without-html src='
<$let hash={{{ [<currentTiddler>encodeuricomponent[]] }}}>
<a href=`${ [{!!base-url}] }$#$(hash)$`>this tiddler on tiddlywiki.com</a>
</$let>'/>

View File

@ -0,0 +1,14 @@
created: 20230615045327830
modified: 20230615045353826
tags: [[Widget Attributes]] WikiText
title: Transcluded Attribute Values
type: text/vnd.tiddlywiki
Transcluded attribute values are indicated with double curly braces around a TextReference. For example:
```
attr={{tiddler}}
attr={{!!field}}
attr={{tiddler!!field}}
```
<<.warning "The value of the attribute value will be the exact text retrieved from the TextReference. Any wiki syntax in that text will be left as-is.">>

View File

@ -0,0 +1,14 @@
created: 20230615045239825
modified: 20230615045312961
tags: [[Widget Attributes]] WikiText
title: Variable Attribute Values
type: text/vnd.tiddlywiki
Variable attribute values are indicated with double angle brackets around a [[macro invocation|Macro Calls]]. For example:
```
<div title=<<MyMacro "Brian">>>
...
</div>
```
<<.warning "The text from the definition of the macro will be retrieved and text substitution will be performed (i.e. <<.param $param$>> and <<.param &#36;(...)&#36;>> syntax). The value of the attribute value will be the resulting text. Any wiki syntax in that text (including further macro calls and variable references) will be left as-is.">>

View File

@ -0,0 +1,21 @@
created: 20230615045526689
modified: 20230615060059476
tags: WikiText
title: Widget Attributes
type: text/vnd.tiddlywiki
Attributes of HTML elements and widgets can be specified in several different ways:
* [[a literal string|Literal Attribute Values]]
* [[a transclusion of a textReference|Transcluded Attribute Values]]
* [[a transclusion of a macro/variable|Transcluded Attribute Values]]
* [[as the result of a filter expression|Filtered Attribute Values]]
* <<.from-version "5.3.0">> [[as the result of performing filter and variable substitutions on the given string|Substituted Attribute Values]]
|attribute type|syntax|h
|literal |single, double or triple quotes or no quotes for values without spaces |
|transcluded |double curly braces around a text reference |
|variable |double angle brackets around a macro or variable invocation |
|filtered |triple curly braces around a filter expression|
|substituted|single or triple backticks around the text to be processed for substitutions|