String manipulations on textarea input

125 views
Skip to first unread message

Antonis Lamnatos

unread,
May 10, 2021, 5:25:48 AM5/10/21
to TiddlyWiki
I'm moving my personal journal written in markdown files into Tiddlywiki. I'm looking for a way to perform string/text manipulation on the input of an <$edit-text>.

Currently, each entry starts with a ##header which was the date the entry was written. A blank line follows and then one or more paragraphs of the main entry body:

    ## YYYYMMDD

    Lorem ipsum dolor sit amet, consectetur adipiscing elit...

I've created a Journal Entry tiddler page which would have an <$edit-text tag=textarea> widget. I'll manually paste in the textarea each journal entry and by pressing a button it will be split into the following elements/fields, which would then be added as a new tiddler with the following fields:

    Title: YYYYMMDD
    Text: Lorem ipsum (etc)
    Year: YYYY
    Month: MM

I'm looking for a way way to perform all the little subtring() manipulations and isolating the header line and the main body. I've already figured how to create a new tiddler from a series of existing fields. 

PMario

unread,
May 10, 2021, 5:33:35 AM5/10/21
to TiddlyWiki
Hi,
Field names have to be lowercase only.
-m

Antonis Lamnatos

unread,
May 10, 2021, 7:27:19 AM5/10/21
to TiddlyWiki
Ah yes, thanks for reminding me. Although it was purely for showing how I want to split the date per field. I haven't yet reached the point where I'm trying to reference actual fields.

I'm trying to see if some or even most of what I've described can be achieved with <$list filter>-ing through the text area input. Maybe combining splits, regexes and splitregexes. 

The other approach would be to code a JavaScript macro but that gets quite more complicated and is not as easy to play with until I get it right.

TW Tones

unread,
May 10, 2021, 8:20:21 PM5/10/21
to TiddlyWiki
Quick tips

  • Text areas have lines delimited by \n
  • if you have the text in a filter you can use splitregexp[\n]] to split it into each line, then split again on split[:]] to get the first[] item or the fieldname:value
    • text must be handled differently as its multiline.
    • you can then join[] back as needed.
  • Note the fieldname:values resembles a datatiddler format, and may present an opportunity.
Tones

Antonis Lamnatos

unread,
May 12, 2021, 6:41:23 AM5/12/21
to TiddlyWiki
I've made some progress with this, here's where I'm currently stuck.

I'm pasting the text (##header and body) in an <$edit-text> widget. This is linked to a different, dedicated Tiddler called NewEntryData. Then I'm bringing back only the month bit from the header by using the following. The button text appears as it should (Set to: "05" for example), so this tells me that the testmonth variable is properly filled.

Unfortunatelly though, the same variable in the <$action-setfield> part is always set either to the literal value of the filter right above (i.e. <$list filter... etc ), or is set to the literal text <<testmonth>>. It depends on what combination of variable reference I use (I've tried everything descibed in Tobias' Variables vs. Parameters reference list).

Any idea how should I reference the variable in the widget's parameters for it to get the actual content instead of some literal text?

<$vars testmonth='<$list filter="[{NewEntryData!!text}removeprefix[##]trim[]splitregexp[\n]trim[]first[]splitregexp[]rest[4]first[2]join[]]"/>'>
<$button>
<$action-setfield input-month=<<testmonth>> />
Set to: "<<testmonth>>"
</$button>
</$vars>

Saq Imtiaz

unread,
May 12, 2021, 6:57:21 AM5/12/21
to TiddlyWiki
Try this:

<$vars testmonth={{{[{NewEntryData!!text}removeprefix[##]trim[]splitregexp[\n]trim[]first[]splitregexp[]rest[4]first[2]join[]]}}}>
<$button>
<$action-setfield input-month=<<testmonth>> />
Set to: "<<testmonth>>"
</$button>
</$vars>

Eric Shulman

unread,
May 12, 2021, 7:10:06 AM5/12/21
to TiddlyWiki
On Wednesday, May 12, 2021 at 3:41:23 AM UTC-7 lamn...@gmail.com wrote:
I've made some progress with this, here's where I'm currently stuck.

I'm pasting the text (##header and body) in an <$edit-text> widget. This is linked to a different, dedicated Tiddler called NewEntryData. Then I'm bringing back only the month bit from the header by using the following. The button text appears as it should (Set to: "05" for example), so this tells me that the testmonth variable is properly filled.

The testmonth variable contains $list widget source code.  When you display this variable in the button label by using <<testmonth>> you are, in effect, using the variable as a macro, which causes the value of the variable to be inserted into the output AND also evaluated, which processes the $list widget filter, producing the "05" that you see displayed.  However, when you use the variable as a parameter in the $action-setfield widget, the value is NOT evaluated and is simply passed along to the $action-setfield, thus storing the unprocessed $list widget filter syntax into the field.

Any idea how should I reference the variable in the widget's parameters for it to get the actual content instead of some literal text?

Instead of using $vars to set the value of "testmonth", using $wikify, like this:

<$wikify name=testmonth text='<$list filter="[{NewEntryData!!text}removeprefix[##]trim[]splitregexp[\n]trim[]first[]splitregexp[]rest[4]first[2]join[]]"/>'>
<$button>
<$action-setfield input-month=<<testmonth>> />
Set to: "<<testmonth>>"
</$button>
</$wikify>

This will cause the $list widget filter to be evaluated regardless of whether <<testmonth>> is used as an $action-setfield parameter or directly rendered and displayed as button text.

Note that Saq's solution, using "filtered transclusion" syntax (tripled curly braces) will also cause the filter to be evaluated and assigned as the value of <<testmonth>>, producing the desired result as well.

enjoy,
-e
Reply all
Reply to author
Forward
0 new messages