How to keep white spaces and line breaks when processing text

140 views
Skip to first unread message

Mohammad

unread,
Feb 23, 2019, 3:06:44 AM2/23/19
to TiddlyWiki
Consider myTid tiddler has the below content:

This is a test

* one
* two




and then, the below script does some small processing like adding some prefixes.


<$vars x={{myTid!!text}} >
<$list filter="[<x>addprefix[xx]]">

</$list>

And the result:


xxThis is a test * one * two


Is there any way to keep whitespace and linebreaks?
This may seems useless but it is used when you want to process the text filed of a tiddler has paragraphs.

--Mohammad



PMario

unread,
Feb 23, 2019, 6:28:03 AM2/23/19
to TiddlyWiki
Hi,

If you want to process the text field use the transclude widget in block mode.

-m

Mohammad

unread,
Feb 23, 2019, 12:59:27 PM2/23/19
to TiddlyWiki
Hello Mario,

 I did not understand how transclude widget can be used in the above example.
To clarify my question, assume I get a multi paragraph tiddler and I want to
change all mario word in the text to MARIO.

So, I need to keep the whitespace and linebreaks when I feed a tiddler to the macro and write back it

--Mohammad

PMario

unread,
Feb 23, 2019, 1:54:37 PM2/23/19
to TiddlyWiki
Hi,

I know your filter is more complicated, but technically I wanted to say:

<$list filter="myTid">
<$transclude mode=block/>
</$list>

-m

PMario

unread,
Feb 23, 2019, 2:00:28 PM2/23/19
to TiddlyWiki
Hi,

>To clarify my question, assume I get a multi paragraph tiddler and I want to
>change all mario word in the text to MARIO.

That's something different and imo hard to do with wikitext functions. ... We don't have propper find and replace functions at the moment.

The diff-match-patch core functions may be a starting point to build something, that you want. But imo it would need some new widgets.

-m

Mohammad

unread,
Feb 23, 2019, 2:10:37 PM2/23/19
to TiddlyWiki
Hi Mario,
 I got your point. Thank you. So, that is why my code breaks in doing that.

See below macro

\define snr(source, searchValue, replaceValue)
\whitespace trim
<$vars
 src  
=<<__source__>>
 sval
=<<__searchValue__>>
 rval
=<<__replaceValue__>>
>
<$list filter="[<src>search<sval>]" emptyMessage="""<$text text=<<src>> />"""  >
<$list variable="p1" filter="[<src>splitbefore<sval>]">
<$list variable="p2" filter="[<src>removeprefix<p1>]">
<$list variable="p3" filter="[<p1>removesuffix<sval>addsuffix<rval>]">
<$text text=<<p3>>/>
</
$list>
<$macrocall $name="snr"
source
=<<p2>>
searchValue
=<<sval>>
replaceValue
=<<rval>>
/>
</
$list>
</$list>
</
$list>
</$vars>
\end

This simple code can do find and replace and it can simple change the mario to MARIO when you work with one paragraph but as soon as you feed it a whole tiddler contains several paragraph, it fails. If I can keep the whitespace and linebreaks, it will work.

--Mohammad

BurningTreeC

unread,
Feb 23, 2019, 3:14:24 PM2/23/19
to TiddlyWiki
Hi Mohammad, how is this:

<$wikify name="hellothere" text="""<$view tiddler="HelloThere" mode="block" format="text"/>""">

<<hellothere>>

</$wikify> 

BurningTreeC

unread,
Feb 23, 2019, 3:30:09 PM2/23/19
to TiddlyWiki
Some errors to fix:

\define snr(source:"", searchValue:"", replaceValue:"")

\whitespace trim
<$vars
 src  
=<<__source__>>
 sval
=<<__searchValue__>>
 rval
=<<__replaceValue__>>
>
<$list filter="[<src>search<sval>]" emptyMessage="""<$text text=<<src>> />"""  >
<$list variable="p1" filter="[<src>splitbefore<sval>]">
<$list variable="p2" filter="[<src>removeprefix<p1>]">
<$list variable="p3" filter="[<p1>removesuffix<sval>addsuffix<rval>]">
<$text text=<<p3>>/>
<$macrocall $name="snr"

source
=<<p2>>
searchValue
=<<sval>>
replaceValue
=<<rval>>
/>
</$list>
</$list>
</$list>
</
$list>
</$vars>
\end 

... you get endless loops if you close the list before the macrocall!

you should also prevent from empty attributes with source:"" etc. ...

using this, try the following on tiddlywiki.com:


<$wikify name="hellothere" text="""<$view tiddler="HelloThere" mode="block" format="text"/>""">

<$wikify name="result" text="""<$macrocall $name="snr" source=<<hellothere>> searchValue="TiddlyWiki" replaceValue="ikiWylddiT"/>""">

<<result>>

</$wikify>

</$wikify> 

BurningTreeC

unread,
Feb 23, 2019, 3:39:10 PM2/23/19
to TiddlyWiki
the p3 filter needs an emptyMessage and it fully works on tiddlywiki.com

<$list variable="p3" filter="[<p1>removesuffix<sval>addsuffix<rval>]" emptyMessage="""<$text text=<<src>>/>"""> 

BurningTreeC

unread,
Feb 23, 2019, 3:55:08 PM2/23/19
to TiddlyWiki
... with help by the diff-text widget you see what has changed:

<$wikify name="hellothere" text="""<$view tiddler="HelloThere" mode="block" format="text"/>""">
<$wikify name="result" text="""<$macrocall $name="snr" source=<<hellothere>> searchValue="TiddlyWiki" replaceValue='ikiWylddiT'/>""" mode="block">

<$diff-text source={{HelloThere}} dest=<<result>>>
{{$:/language/Diffs/CountMessage}}
</$diff-text>

<<result>>

</$wikify>
</$wikify> 

TonyM

unread,
Feb 23, 2019, 8:36:02 PM2/23/19
to TiddlyWiki
Mohammad,

As is commonly the case, especially in TiddlyWiki, it is important to return to why we are asking to do something. It is actually very common for people to think search and replaces is the answer when another solution may be more valid. In part because a manual search and replace is easier for one or two cases, automated replacements re a better solution for multiple cases, so automating search and replace is not often overly useful.

This does not replace the feature you are looking for, it just potentially diminishes the value, or number of occasions it may be needed.

<$set name=first-name value="Mohammad">
<$set name=last-name value="TheMan">
<$set name=birthday value="26/12">
<$set name=salution value="Sir">

<$transclude tiddler="letter-template"/>


</$set></$set></$set>

letter-template
Dear <<salutation>> <<first-name>>  <<last-name>>,

We are aware your birthday is on the <<birthday>> and would like to give you the following gift, <<giftname>>
...

The key to making such productivity gains with TiddlyWiki is to always be thinking of how you can capture and represent data in the first place in a way that future retrofit or customisation is easier.

I applaud your building the tools for such retrofit, but also think it is important to avoid such a need in the first place. In this example if we foresaw the need to search and replace something more than once, we can consider if we should construct our template instead. If it is only once, manual change is cheap and practical.

By the way, if you had not thought of the need to change those values when you first wrote the tiddler, perhaps when you identify you need to search and replaces rather than change the literal value, change it to a variable instead.

So Search for "Mohammad" and replace with <<first-name>> 
or perhaps "Mohammad" and replace with {{!!first-name}} 
would be the optimum approach.

So perhaps you can ensure any search and replace solution is tolerant of Variables and text references, perhaps even substitution variables $param$ $(variable)$

Regards
Tony

Mohammad

unread,
Feb 24, 2019, 12:52:35 AM2/24/19
to TiddlyWiki
Hello BTC,
 Your solution is really amazing and you did the magic. I was working for a week with no success.

Yes, I did some test on tiddlywiki.com and it works like a charm. I am trying to use it in Commander for
heavy batch operation, and I will back to you with results.

Thanks a million!

Best
Mohammad

Mohammad

unread,
Feb 24, 2019, 12:53:08 AM2/24/19
to TiddlyWiki
Yep, that is correct.

Mohammad

Mohammad

unread,
Feb 24, 2019, 12:55:16 AM2/24/19
to TiddlyWiki
This is also great! but I will need to customize it.
Yes, this way I can check the change before applying the replace!

--Mohammad

Mohammad

unread,
Feb 24, 2019, 1:27:30 AM2/24/19
to TiddlyWiki
Tony!
 Thank you for proposal.
Using Tiddlywiki template and variable substitution makes it very flexible.
I will add this solution to TW-Scripts. 

--Mohammad
Reply all
Reply to author
Forward
0 new messages