https://tiddlywiki.com/prerelease/#%24%3A%2Fplugins%2Ftiddlywiki%2Fdynannotate
Check out the examples tab in the plugin tiddler.
I am working on a macro to highlight target strings within the text field of a tiddler.The use case is to make it easier to for me see where the targetStrings are (if any).
My question is how to highlight the targetString in the macro (ie. treatingthe macro as a string) as opposed to (apparently) executing the macroand highlighting the targetString in the output.
\define formatTid.highlightText(inputText targetString)
<$set name="outputText" filter="[[$inputText$]] +[splitregexp[\b$targetString$\b]join[@@color:blue;$targetString$@@]]" select="0">
| ''outputText''|<$text text=<<outputText>>/>|
\end
<$macrocall $name="formatTid.highlightText" inputText={{highlightTest.sentence!!text}} targetString="goFish"/
>
outputText | \define @@color:blue;goFish@@() !!! This example has three instances of @@color:blue;goFish@@. \end <<@@color:blue;goFish@@>> |
\define formatTid.highlightText(inputText targetString)
<$set name="outputText" filter="[[$inputText$]] +[splitregexp[\b$targetString$\b]join[@@color:blue;$targetString$@@]]" select="0">
| ''outputText''|@@white-space:pre;<<showOutput>>@@|
\end
\define showOutput()
\rules only styleinline
$(outputText)$
\end
<$macrocall $name="formatTid.highlightText" inputText={{highlightTest.sentence!!text}} targetString="goFish"/>
outputText |
\define goFish() !!! This example has three instances of goFish. \end <<goFish>> |
This time I tried a macro with a filter that used goFish in a filter. See below.Nothing was listed in the outputText.Tomorrow I'll look at the literal output text.
\define formatTid.highlightText(source targetString)
<$set name="outputText" filter="[{$source$}splitregexp[$targetString$]join[@@color:blue;$targetString$@@]]" select="0">
| ''outputText''|@@white-space:pre-wrap;font-family:monospace;<<showOutput>>@@|
\end
\define showOutput()
\rules only styleinline
$(outputText)$
\end
<$select tiddler="$:/temp/highlight/source">
<$list filter="[all[]sort[]]"><option value=<<currentTiddler>>><$text text=<<currentTiddler>>/></option></$list>
</$select>
<$edit-text tiddler="$:/temp/highlight/target" tag="input" default="" placeholder="enter text to highlight" /><br>
<$macrocall $name="formatTid.highlightText" source={{$:/temp/highlight/source}} targetString={{$:/temp/highlight/target}}/>
Enter code here...\define formatTid.highlightText(source:"MainMenu" targetString:"Handler" highlightColor:"blue")
<$set name="outputText" filter="[{$source$}splitregexp[$targetString$]join[@@color:$highlightColor$;$targetString$@@]]" select="0">
<!-- Future Ideas
* display number of matches
* use CSS for highlighting not just color
-->
|''targetString is highlighted in @@color:$highlightColor$; $highlightColor$@@''|
|@@white-space:pre-wrap;font-family:monospace;<<showOutput>>@@|
\end
\define showOutput()
\rules only styleinline
$(outputText)$
\end
<!-- 1. Type string into entryBuffer
2. press GO to:
* copy string from entryBuffer to targetString (avoids responding to each keystroke)
* initialize source to ""
3. list tiddlers that contain targetString (if any)
4. select tiddler to be highlighted
-->
''Enter targetString and press GO'' <$edit-text tiddler="$:/temp/highlight/entryBuffer" tag="input" default="" placeholder="enter text to highlight" />
<$button><$action-setfield $tiddler="$:/temp/highlight/targetString" $value={{$:/temp/highlight/entryBuffer}}/><$action-setfield $tiddler="$:/temp/highlight/source" $value=""/>GO</$button>
<$vars targetString={{$:/temp/highlight/targetString}}><br>
''Select Source Tid'' (if any) <$select tiddler="$:/temp/highlight/source" default="blurg">
<$list filter="[!is[system]search:text<targetString>has[code-library]sort[]]"><option value=<<currentTiddler>>><$text text=<<currentTiddler>>/></option></$list>
</$select><br>
<$reveal text={{$:/temp/highlight/source}} type="nomatch" default="">
<$macrocall $name="formatTid.highlightText" source={{$:/temp/highlight/source}} targetString=<<targetString>>/>
</$reveal>
</$vars>
<!-- Future Ideas
* display number of matches
* use CSS for highlighting not just color
-->
\define formatTid.highlightText(source:"MainMenu" targetString:"Handler" style:"color:blue;font-weight:bold;")
<style> .highlight { $style$ } </style>
<$vars outputText={{{ [{$source$}splitregexp[$targetString$]join[@@.highlight $targetString$@@]] }}}>
<$text text={{{ [{$source$}splitregexp[$targetString$]count[]subtract[1]] }}} /> matches:
<div style="border:1px solid;white-space:pre-wrap;font-family:monospace;"><<showOutput>></div>
\end
\define formatTid.highlightText(source:"MainMenu" targetString:"Handler" highlightColor:"blue" highlightStyle:"background-color:khaki;color:#656565;font-size:8.5pt;font-weight:bold;border-radius: 2px;border-style:solid;border-color:gray;border-width:1px;")
<$set name="outputText" filter="[{$source$}splitregexp[$targetString$]join[@@$highlightStyle$$targetString$@@]]" select="0">
|''For source'': @@color:navy;font-size:8pt;font-weight:bold;$source$@@; ''targetString'': @@color:navy;font-size:8pt;font-weight:bold;$targetString$@@; ''found'' @@color:navy;font-size:8pt;font-weight:bold;<$text text={{{ [{$source$}splitregexp[$targetString$]count[]subtract[1]] }}} /> match(es).@@|
<div style="border:1px solid;white-space:pre-wrap;font-family:monospace;">
<<showOutput>>
<
/div>
\end
\define showOutput()
\rules only styleinline
$(outputText)$
\end
\define formatTid.highlightText.ui()
<!-- User interface to:
* enter target string
* select source
* display formatTid.highlightText results in table
-->
---
<!-- Type string into entryBuffer and press GO to
* copy string from entryBuffer to targetString (avoids responding to each keystroke)
* initialize source to (avoid retaining prev choice if no matching tiddlers were found)
-->
''Enter targetString and press GO'' <$edit-text tiddler="$:/
temp/highlight/entryBuffer" tag="input" default="" placeholder="enter text to highlight" />
<$button><$action-setfield $tiddler="$:/temp/highlight/targetString" $value={{$:/temp/highlight/entryBuffer}}/><$action-setfield $tiddler="$:/temp/highlight/source" $value=""/>GO</$button>
<$vars targetString={{$:/temp/highlight/targetString}}><br>
<!-- Select source tiddler from tids that contain
the search string (if any)
-->
''Select Source (tiddler)'' @@color:navy;font-variant:super;font-weight:bold;(<$count filter="[!is[system]search:text<targetString>has[code-library]]"/> found)@@ <$select tiddler="$:/temp/highlight/source" default="blurg">
<$list filter="[!is[system]search:text<targetString>has[code-library]sort[]]"><option value=<<currentTiddler>>><$text text=<<currentTiddler>>/></option></$list>
</$select><br>
<!-- Highlight targetString's in the selected source tid.
-->
<$reveal text={{$:/temp/highlight/source}} type="
nomatch" default="">
<$macrocall $name="formatTid.highlightText" source={{$:/temp/highlight/source}} targetString=<<targetString>>/>
</$reveal>
</$vars>
---
\end
<<formatTid.highlightText.ui>>