Remove last character of a tiddler's text within a macro

189 views
Skip to first unread message

MagoArcade

unread,
Aug 18, 2019, 2:49:19 PM8/18/19
to TiddlyWiki
Let's say "$:/plugins/felixhayashi/tiddlymap/graph/views/map" contains the text "{line 1} Hello World}" (note the curly bracket)


\define ViewPath()
$
:/plugins/felixhayashi/tiddlymap/graph/views/map
\end


\define ViewTiddlerAppended()
{{$(ViewPath)$}}. Nice to see you.
\end

How do I get <<ViewTiddlerAppended>> to read:

"{line 1} Hello World. Nice to see you."

(i.e. remove the last character of the text in the tiddler)

Mark S.

unread,
Aug 18, 2019, 5:29:13 PM8/18/19
to TiddlyWiki
This variant uses the new splitregexp operator to eliminate the last character:

\define ViewTiddlerAppended()
<$set name=shortpath filter="[{$(viewpath)$}splitregexp[^(.*?).$]]" select=1 >
<<shortpath>>. Nice to see you.
</$set>
\end

<$vars viewpath="$:/
plugins/felixhayashi/tiddlymap/graph/views/map" >

<<ViewTiddlerAppended>>

</$vars>


Mohammad

unread,
Aug 19, 2019, 2:09:45 AM8/19/19
to TiddlyWiki
To Josiah,

This is an example of using regexp in Tiddlywiki worth to be included in your regexp tutorial!

@Mark
Why not just using <viewpath> instead of {$(viewpath)$}

--Mohammad

Mark S.

unread,
Aug 19, 2019, 9:53:28 AM8/19/19
to TiddlyWiki
Statements like

{{<<variable>>}}

don't work, so I assume that the filter equivalent, {<variable>} also doesn't work.

When I test this latter form, it doesn't work.

The $(...)$ substitution approach almost always does what I originally intended, so I tend to stick to it, perhaps more than I need to.

Thanks!

MagoArcade

unread,
Aug 19, 2019, 2:41:30 PM8/19/19
to tiddl...@googlegroups.com
Thanks Mark - this looks really good. However, what I'm wanting to do it to truncate the !!text inside the tiddler identified by ``viewpath`` I can't seem to get this to work with that. For illustration:

\define ViewTiddlerAppended()
<$vars ViewText={{$(ViewPath)$!!text}}>
VIEW TEXT
: : <<ViewText>>
<$set name=shortpath filter="[{{$(ViewPath)$!!text}}splitregexp[^(.*?).$]]" select=1 >
<br>OUTPUT: [<<shortpath>>]
</$set>
</
$vars>
\end

Gives me:

VIEW TEXT: : { "8c44558c-cd16-4064-87b6-d4b40fd167e8": { "x": -351, "y": -55 }, "565d2cae-a39e-4bec-b16b-543619cf2bd7": { "x": -119, "y": -72 }, "13f94e2c-189b-439e-aa55-0c7a88d45f97": { "x": -132, "y": 136 }, "e20a7328-d70c-477d-8103-1d99ffbf031b": { "x": -127, "y": -191 }, "516a25e4-c141-4469-a790-229b90da4bfa": { "x": -126, "y": 11 }, "cd326bff-d50c-406a-a5ca-a87699ae7bbe": { "x": -461, "y": -201 }, "7a8b1138-dbb9-49c8-91d6-12b9f21929f2": { "x": -559, "y": -108 }, "6a96291a-44bc-4d77-9261-8cc7b2964e87": { "x": -281, "y": -255 }, "3cadefc8-4186-4524-968a-22083b54c0bc": { "x": 18, "y": -146 }, "41bb28a9-b37f-4628-9730-0a48234a7490": { "x": -582, "y": 13 }, "6a5db04b-27e0-4e0d-81a3-1a3c1b44e928": { "x": -503, "y": 144 }, "7cc70f20-3457-4641-a2ab-d6f0512ab092": { "x": -333, "y": 176 } }
OUTPUT
: []

 Any suggestions?


Mohammad

unread,
Aug 19, 2019, 6:13:58 PM8/19/19
to TiddlyWiki
Mark, I mean just use <variable>, See the below revised code


\define ViewTiddlerAppended()
<$set name=shortpath filter="[<viewpath>splitregexp[^(.*?).$]]" select=1 >

<<shortpath>>. Nice to see you.
</$set>
\end

<$vars viewpath="$:/
plugins/felixhayashi/tiddlymap/graph/views/map" >

<<ViewTiddlerAppended>>

</$vars>

Eric Shulman

unread,
Aug 19, 2019, 6:23:29 PM8/19/19
to TiddlyWiki


On Monday, August 19, 2019 at 11:41:30 AM UTC-7, MagoArcade wrote:
Thanks Mark - this looks really good. However, what I'm wanting to do it to truncate the !!text inside the tiddler identified by ``viewpath`` I can't seem to get this to work with that. For illustration:

<$set name=shortpath filter="[{{$(ViewPath)$!!text}}splitregexp[^(.*?).$]]" select=1 >

TextReferences within filters use SINGLE curly-braces... thusly:
<$set name=shortpath filter="[{$(ViewPath)$!!text}splitregexp[^(.*?).$]]" select=1 >

note: similarly, Variables within filters use SINGLE angle-brackets (e.g., [<somevar>...])

enjoy,
-e
Eric Shulman
TiddlyTools: "Small Tools for Big Ideas!" (tm) http://tiddlytools.github.io (TWClassic)
InsideTiddlyWiki: The Missing Manuals

Mark S.

unread,
Aug 19, 2019, 6:48:31 PM8/19/19
to TiddlyWiki
This code:

\define ViewTiddlerAppended()
<$vars viewtext={{$(viewpath)$}}>
''VIEW TEXT:''  <$text text=<<viewtext>>/>
<$set name=shortpath filter="[{$(viewpath)$}splitregexp[^(.*?).$]]" select=1 >

<br>''OUTPUT:'' [<<shortpath>>]
</
$set>
</$vars>
\end

<$vars viewpath="$:/
plugins/felixhayashi/tiddlymap/graph/views/map" >

<<ViewTiddlerAppended>>

</$vars>

Produces this result:


VIEW TEXT:  { "8c44558c-cd16-4064-87b6-d4b40fd167e8": {
 
"x": -351, "y": -55 }, "565d2cae-a39e-4bec-b16b-543619cf2bd7": { "x":
-119, "y": -72 }, "13f94e2c-189b-439e-aa55-0c7a88d45f97": { "x": -132,
"y": 136 }, "e20a7328-d70c-477d-8103-1d99ffbf031b": { "x": -127, "y":
-191 }, "516a25e4-c141-4469-a790-229b90da4bfa": { "x": -126, "y": 11 },
"cd326bff-d50c-406a-a5ca-a87699ae7bbe": { "x": -461, "y": -201 },
"7a8b1138-dbb9-49c8-91d6-12b9f21929f2": { "x": -559, "y": -108 },
"6a96291a-44bc-4d77-9261-8cc7b2964e87": { "x": -281, "y": -255 },
"3cadefc8-4186-4524-968a-22083b54c0bc": { "x": 18, "y": -146 },
"41bb28a9-b37f-4628-9730-0a48234a7490": { "x": -582, "y": 13 },
"6a5db04b-27e0-4e0d-81a3-1a3c1b44e928": { "x": -503, "y": 144 },
"7cc70f20-3457-4641-a2ab-d6f0512ab092": { "x": -333, "y": 176 } }


OUTPUT: [{

"8c44558c-cd16-4064-87b6-d4b40fd167e8": { "x": -351, "y": -55 },
"565d2cae-a39e-4bec-b16b-543619cf2bd7": { "x": -119, "y": -72 },
"13f94e2c-189b-439e-aa55-0c7a88d45f97": { "x": -132, "y": 136 },
"e20a7328-d70c-477d-8103-1d99ffbf031b": { "x": -127, "y": -191 },
"516a25e4-c141-4469-a790-229b90da4bfa": { "x": -126, "y": 11 },
"cd326bff-d50c-406a-a5ca-a87699ae7bbe": { "x": -461, "y": -201 },
"7a8b1138-dbb9-49c8-91d6-12b9f21929f2": { "x": -559, "y": -108 },
"6a96291a-44bc-4d77-9261-8cc7b2964e87": { "x": -281, "y": -255 },
"3cadefc8-4186-4524-968a-22083b54c0bc": { "x": 18, "y": -146 },
"41bb28a9-b37f-4628-9730-0a48234a7490": { "x": -582, "y": 13 },
"6a5db04b-27e0-4e0d-81a3-1a3c1b44e928": { "x": -503, "y": 144 },

"7cc70f20-3457-4641-a2ab-d6f0512ab092": { "x": -333, "y": 176 } }]




Note that the last } is trimmed off. If it doesn't work for you, check if there is a space on the end of your tiddler contents. This is literally taking off
the last character, even if that character is a space.

-- Mark

On Monday, August 19, 2019 at 11:41:30 AM UTC-7, MagoArcade wrote:

Mark S.

unread,
Aug 19, 2019, 6:52:32 PM8/19/19
to TiddlyWiki
Hi Mohammad,

Well, that won't do what MA wants. Your code would try to split the tiddler name, $:/plugins/felixhayashi/tiddlymap/graph/views/map, but MA
wants the tiddler contents truncated by one.

Mohammad

unread,
Aug 20, 2019, 1:29:59 AM8/20/19
to TiddlyWiki
@Mark,
 Thanks for clarification.

--Mohammad

@TiddlyTweeter

unread,
Aug 20, 2019, 5:52:36 AM8/20/19
to TiddlyWiki
Ciao Mark S.

"splitregexp" seems to be behaving here like "return match, throw the rest away"

Yes?

Equivalent to ...

match: ^(.*?).$
replace
: $1

The lazy match, *?, insists that the last character of the line is thrown away. Which is what was wanted?

TT

MagoArcade

unread,
Aug 20, 2019, 2:54:04 PM8/20/19
to TiddlyWiki
Thanks again for the reply Mark. However, I just can't figure why the code's working your end but not mine. It looks like you've tested this code. But when I run mine, I get a different result. Mine:

\define ViewTiddlerAppended()
<$vars ViewText={{$(viewpath)$}}>
VIEW TEXT
: <<ViewText>>

<$set name=shortpath filter="[{$(viewpath)$}splitregexp[^(.*?).$]]" select=1 >
<br>OUTPUT: [<<shortpath>>]
</$set>
</
$vars>
\end

<$vars viewpath="$:/plugins/felixhayashi/tiddlymap/graph/views/MyView/map">

View Tiddler Appended: <<ViewTiddlerAppended>>

</$vars>

Output:

View Tiddler Appended: VIEW TEXT: { "8c44558c-cd16-4064-87b6-d4b40fd167e8": { "x": -351, "y": -55 }, "565d2cae-a39e-4bec-b16b-543619cf2bd7": { "x": -119, "y": -72 }, "13f94e2c-189b-439e-aa55-0c7a88d45f97": { "x": -132, "y": 136 }, "e20a7328-d70c-477d-8103-1d99ffbf031b": { "x": -127, "y": -191 }, "516a25e4-c141-4469-a790-229b90da4bfa": { "x": -126, "y": 11 }, "cd326bff-d50c-406a-a5ca-a87699ae7bbe": { "x": -461, "y": -201 }, "7a8b1138-dbb9-49c8-91d6-12b9f21929f2": { "x": -559, "y": -108 }, "6a96291a-44bc-4d77-9261-8cc7b2964e87": { "x": -271, "y": -250 }, "3cadefc8-4186-4524-968a-22083b54c0bc": { "x": 18, "y": -146 }, "41bb28a9-b37f-4628-9730-0a48234a7490": { "x": -582, "y": 13 }, "6a5db04b-27e0-4e0d-81a3-1a3c1b44e928": { "x": -503, "y": 144 }, "7cc70f20-3457-4641-a2ab-d6f0512ab092": { "x": -333, "y": 176 } }
OUTPUT
: []

Any ideas? I'm using the most recent version of tw

Mark S.

unread,
Aug 20, 2019, 3:12:09 PM8/20/19
to TiddlyWiki
I'm guessing that your data is a little different. Maybe you could use the tiddler export to export

$:/plugins/felixhayashi/tiddlymap/graph/views/map

as a JSON file. Then attach and upload it here.

MagoArcade

unread,
Aug 20, 2019, 5:06:54 PM8/20/19
to TiddlyWiki
Thanks. Attached
$__plugins_felixhayashi_tiddlymap_graph_views_MyView_map.json

Mark S.

unread,
Aug 20, 2019, 7:08:06 PM8/20/19
to TiddlyWiki
It's the linefeeds in the original text. What I did here was to add regular expressions to take the lines apart, join them back together, and then do the original split.

Good luck!

\define ViewTiddlerAppended()
<$vars ViewText={{$(viewpath)$}}>
<p/>VIEW TEXT: <<ViewText>>

<$set name=shortpath filter="[{$(viewpath)$}splitregexp[\n]join[ ]splitregexp[^(.*?).\s*$]]" select=1 >
<p/>OUTPUT: <<shortpath>>
</$set>
</
$vars>
\end

^(.*?).$

<$vars viewpath="$:/plugins/felixhayashi/tiddlymap/graph/views/MyView/map 1">


View Tiddler Appended: <<ViewTiddlerAppended>>

</$vars>



On Tuesday, August 20, 2019 at 2:06:54 PM UTC-7, MagoArcade wrote:
Thanks. Attached

MagoArcade

unread,
Aug 25, 2019, 5:22:24 PM8/25/19
to TiddlyWiki
Wow - thanks Mark - that did it. That's some spot and some regex/tw coding right there!

TonyM

unread,
Aug 26, 2019, 12:04:32 AM8/26/19
to TiddlyWiki
I would think transforming a field to a variable is a good approach

<$set name=fieldname value={{!!fieldname}} emptyValue="DefaultValue">
You can than use <
<fieldname>>
or in filters
<fieldname>
or in macros $(fieldname)$ or <<__fieldname__>> or if used as a parameter $fieldname$
</$set>

Notice how it is often fine using the fieldname as the variable name. It leaves less to keep track of.

\define macroname(filter)
<$set name=working value="$filter$" emptyValue={{!!filter}}>
<$set name=filter value=<<working>> emptyValue="defaultfilter">
So here we use <<filter>> etc... and the macro will use the "filter" in the macro call otherwise use the value in a field called filter otherwise the default filter.
</$set></$set>

The beauty of this is your macre can use "filter" however it was found, you could override it with a field by the same name and always have a default.

Or take this further.
\define macroname(filter)
<$set name=working1 value="$filter$" emptyValue=<<__filter__>> >
<$set name=working2 value=<<working1>> emptyValue={{!!filter}}>
<$set name=filter value=<<working2>> emptyValue="defaultfilter">
So here we use <<filter>> etc... and the macro will use the "filter" in the macro call otherwise use and existing value in the filter variable, then the value in a field called filter otherwise the default filter.
</$set></$set>
Regards
Tony
Reply all
Reply to author
Forward
0 new messages