Problem with extra spaces showing up around macro generated text

All you smart people,

I have a few macros that I use to generate a new newspaper article transcription for my railroad TW (see attached file). One of the macros is a form with edit-text widgets to enter the information (NewNewspaper). Another macro is used to generate a title for the new tiddler (newtidtitle). Another macro is used to convert a date from the format 1888-07-06 to July 6, 1888 (date-str).

The macros generally work great, but when I use the date-str macro by itself in the tiddler text, there is always an extra space following the converted date. The date-str macro is also used in the process of generating the title for the new tiddler. The problem with that is there are 6 extra spaces in front of the date and 8 extra spaces after (see screenshot).

I’ve tried a number of different things to eliminate the extra spaces, but to no avail. The question I have for you smart folks is where do these extra spaces originate, why are there so many in the title and not in tiddler text and how do I eliminate them? Any help would be greatly appreciated. Perhaps there is a much more efficient way of accomplishing what I’m after.

Thanks in advance! Cheers!

new article.json (3.3 KB)

Try adding \whitespace trim at the beginning of the date-str macro, like this:

\define date-str(...params...)
\whitespace trim
... rest of macro code here ...
\end

Alternatively, if you are using <date-str ...> within some filter syntax (i.e., to generate the title for a new tiddler), then you can write: <date-str ...>trim[] to remove the leading/trailing whitespace from the macro output.

The reason there is a difference in the number of spaces when used “by itself in the tiddler text” vs. “generating the title for the new tiddler”, is that when <<date-str>> is displayed as a stand-alone macro, the browser’s HTML rendering engine automagically reduces sequences of whitespace to single spaces, while when used to construct a new title, the macro output isn’t being “rendered”, so all the whitespace is retained.

-e

4 Likes

Thanks Eric,

Putting the \whitespace trim within the macro definition was all that was needed. I didn’t realize you could do that. Thanks so much!

Consider also the trim operator trim[ ] (Space in brackets), when writing macros where stray spaces are an issue. I am not sure it is a bug, more the consequence of space often being used as a delimiter.

Thanks for the tip @TW_Tones. I need to have a cheat sheet for filter operators right in front of me at all times.

I

A quick solution is a direct link to Filter Operators, the key piece of information is the header to each set of operators eg; String Operators

Alternatively to the static version Filter Operators

You can also use these with an iframe.

Great idea! Thanks very much.