Retain exact number of newlines through $wikify?

Is it possible to retain the exact arrangements of newlines in a text that is wikified and then shown in a codeblock (or copied to clipboard)?

If tiddler Text contains exactly:

{{A}}

{{B}}
{{C}}

I expected that the following code:

<$wikify name="text" text={{Text}}>
  <$codeblock code=<<text>>/>
</$wikify>

would produce the following, shown in a codeblock widget:

Alpha

Bravo
Charlie

Instead it produces

AlphaBravoCharlie

or, if output="formattedtext" is added to the wikify widget, it produces



Alpha

Beta

Gamma

Is it possible to retain the exact spacing/newlines, that is: double newline remains double newline, single newline remains single newline? The leading/trailing newlines are unwelcome, but easy to deal with if consistent, so they’re not the heart of the problem.

I haven’t found any satisfying solution so far. Two ugly workarounds I have found are:

  • Transclude a tiddler containing a single newline in places where I want double newline, so in this example, assuming the tiddler - contains a single newline:
{{A}}
{{-}}
{{B}}
{{C}}
  • add any whitespace character, e.g. single space, between the double newlines (this is error prone and the space remains in the final output ).
  • I experimented with manipulating the text before it is wikified, that is using a filtered transclusion as $wikify text parameter, and search-replace the double newlines. But I haven’t found anything that would work.

Any help would be appreciated!


Context:

I’m creating a set of document templates, which should be easy to copy from TW. The templates consist partially of plain text, and partially of reusable transcluded parts (so the wikification is necessary).
Right now for testing purposes I’m only displaying the templates in a codeblock, to see if the spacing is preserved right.

I have found a much better workaround:

<$wikify name="text" text={{{ [{!!text}search-replace:g:regexp[\n\n],[
¶
]] }}} output="text">
	<$codeblock code={{{ [<text>search-replace:g:regexp[¶],[]] }}} />
</$wikify>

Since single newlines are retained but double are squashed to single, we replace \n\n with \n¶\n before wikifying, and then removing the before displaying in codeblock or copying. This will work as long as the text itself is not supposed to contain , any other wild character of choice can be used.

I have a feeling and hope such a hack should not be necessary, so I’m still interested in an answer.

The parsing process eats any whitespace that is part of a syntactic construction. So, for example, all the newlines following a paragraph are lost. You can see this with the parse tree preview for your example; the parse tree does not directly express any of the whitespace:

Thanks for clarifying! This means that what I’m trying to achieve will not be possible without a workaround of some kind. The one I’ve described above is sufficient though, as long as I only need to discern between single and double newlines.

Try this

<pre><code>{{A}}

{{B}}
{{C}}</code></pre>

It works for me. Be aware, that if there are any newlines in A B C they will also show up.

1 Like

Thanks, it works for me too! I have actually added the pre and code tags as pre/suffixes to the wikified text, so that the tiddler that holds the document template can be kept clean as I intended.

\define prefix() <pre><code>
\define suffix() </code></pre>
<$wikify name="text" text={{{ [{Text}addprefix<prefix>addsuffix<suffix>] }}} >
  <$codeblock code=<<text>> />
</$wikify>

This is intended in my use case.