Unexpected spacing in macro - bug?

46 views
Skip to first unread message

Stefan Curtis

unread,
May 31, 2021, 12:13:19 PM5/31/21
to TiddlyWiki

Hi,

I've been writing a macro that involves a couple of nested <$set ...> widgets and have spent several hours pulling my hair out, because there were leading and trailing spaces surrounding the parsed macro value that I wasn't putting there, and that were breaking what I wanted to do...

I've finally tracked it down to the macro having each of the <$set ...> tags and their respective closing tags on separate lines (simple example below). If I put everything on a single line, the rogue spaces disappear!

This is odd behaviour to me, as I've always been taught (and found) that multi-line HTML/XML is easier to read, and therefore easier to maintain. Is this expected behaviour or a bug?

Thanks,

Stefan


\define multi-line-example()
xx<$set name="var1" value="A">
yy<$set name="var2" value="B">
zz<<var1>><<var2>>zz
</$set>yy
</$set>xx
\end

\define single-line-example()
xx<$set name="var1" value="A">yy<$set name="var2" value="B">zz<<var1>><<var2>>zz</$set>yy</$set>xx
\end

+++ Tiddler +++

aa<<multi-line-example>>aa

aa<<single-line-example>>aa

+++ Output +++

aaxx yy zzABzz yy xxaa

aaxxyyzzABzzyyxxaa



TW Tones

unread,
May 31, 2021, 5:03:20 PM5/31/21
to TiddlyWiki
Some quick tips
  • In filters you can use the trim operator to remove spaces that creep in
  • If defining macros (especially buttons) use \whitespace trim to eliminate unwanted white space
  • As soon as there is a line break in code that aims to concatenate values you can introduce additional spaces. put them on a single line as in my example below
I imagine your messy macro was due to debugging?

This works!
\define multi-line-example()
xx<$set name="var1" value="A">yy<$set name="var2" value="B">zz<<var1>><<var2>>zz</$set>yy</$set>xx
\end

Regards
Tones

Eric Shulman

unread,
May 31, 2021, 5:25:13 PM5/31/21
to TiddlyWiki
On Monday, May 31, 2021 at 9:13:19 AM UTC-7 stefan...@gmail.com wrote:
I've finally tracked it down to the macro having each of the <$set ...> tags and their respective closing tags on separate lines (simple example below). If I put everything on a single line, the rogue spaces disappear!
This is odd behaviour to me, as I've always been taught (and found) that multi-line HTML/XML is easier to read, and therefore easier to maintain. Is this expected behaviour or a bug?

Even in HTML, whitespace matters.  If you enter text like this:
Some words on the first line
followed by some words on a second line

The line break is NOT ignored. Rather, it is treated as a single whitespace character and is reduced to a single space.

Thus, the HTML content is rendered as:
Some words on the first line followed by some words on a second line

In TiddlyWiki, you can eliminate this unwanted whitespace by using the "\whitespace trim" syntax.

If you place this syntax at the start of the tiddler, it will affect all content in that tiddler
If you place this syntax as the first line of a macro definition, it will only affect the content of that macro

Thus, for your purposes, you could write:
\define multi-line-example()
\whitespace trim 
xx<$set name="var1" value="A">
yy<$set name="var2" value="B">
zz<<var1>><<var2>>zz
</$set>yy
</$set>xx
\end

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