How to correctly insert new-line characters through a setfield widget

119 views
Skip to first unread message

BurningTreeC

unread,
Dec 13, 2018, 2:29:04 AM12/13/18
to TiddlyWiki
Hi community tiddlers :)

I have a macro that splits text into single characters

I'm using the diff-match-patch widget to get the text with its new-lines and tabs as characters

The macro allows me to replace characters or to get the index of a given search expression or to find matches by prefix and suffix

After the processing I have a new string that contains the new-line and tab characters which I want to replace with new-lines and tabs

I made a json tiddler like

{
    "\u21A9": "\r\n",
    "↩": "\r\n"
}

this lets me replace the characters the diff-match-patch widget uses as replacements for new-line with newlines recognized by the editor


The problem is, that "\r\n" doesn't do it correctly, neither does "\n" alone

I use the action-setfield widget to set the text of a test-tiddler to the new genereated text. It contains the correct text and all the newlines and tabs, but the newlines aren't rendered the same way as in the original tiddler

Two consecutive newlines for example should render a new line, but it doesn't here. It does work if I edit the text and hit the backspace key at the start of such a generated newline. That deletes something like an "invisible" character and afterwards the rendering is correct.


My question  is, if there's a way to insert such a new-line character so that the rendering results right in the end


I hope someone can help,
all the best, Simon

BurningTreeC

unread,
Dec 13, 2018, 2:41:48 AM12/13/18
to TiddlyWiki
To test this, try the following on tiddlywiki.com for example:

put this in some tiddler:

<$button><$action-setfield $tiddler="test" text={{{ [[jsontiddler]getindex[test]] }}}/>Ok</$button>

create the tiddler "jsontiddler" with its type set to "application/json" and put the following inside: 

{
"test": "\r\n\r\nhello\r\n\r\nlala\r\n"
}

save and hit the Ok button. the tiddler "test" gets created with the text from the "test" index, but the new-line characters are actual new-lines

oddly enough, this works correctly. So I'm wondering now what I'm doing wrong in my macro


PMario

unread,
Dec 13, 2018, 5:28:42 AM12/13/18
to TiddlyWiki
Hi

TW uses "\n" and removes "\r" for every OS. It even makes a big difference in file size.

-m

BurningTreeC

unread,
Dec 13, 2018, 5:36:11 AM12/13/18
to TiddlyWiki

Hi

TW uses "\n" and removes "\r" for every OS. It even makes a big difference in file size.

-m

Thanks Mario, I've changed that back. I had experimented with all kinds of combinations, starting with "\n" only

But even so it inserts newline characters, but the resulting tiddler looks (when open for editing) exactly like the original, but the rendered tiddler does not, it doesn't insert the <p></p> when there are consecutive new-lines

Hitting backspace at the start of such a generated newline makes that newline be recognized as such, it seems as if I'm deleting an invisible character


I'll post the macro if you're interested in having a look

BurningTreeC

unread,
Dec 13, 2018, 6:02:42 AM12/13/18
to TiddlyWiki
Here attached is an empty 5.1.18 wiki with the macros and testing tiddlers set up
replace-characters-tests.html

PMario

unread,
Dec 13, 2018, 7:09:25 AM12/13/18
to TiddlyWiki
Hi BTC,

I'm not really sure, what you try to achieve here. ... I do have a guess. ... And I think "you'r doing it wrong".

I'm using the diff-match-patch widget to get the text with its new-lines and tabs as characters

IMO diff-match-patch mechanism can do everything you did with complicated macros (almost) out of the box.

Can you describe, what you want to achieve.

-mario

BurningTreeC

unread,
Dec 13, 2018, 7:19:52 AM12/13/18
to TiddlyWiki
Hi Mario, I'm playing around with splitting strings into single characters, then putting them back together. That's what I'm doing in the first place. I wouldn't need the diff-match-patch here, I just use it to get those special characters visualized

Here I'm trying to replace single characters without changing the overall formatting and without looking into javascript

I haven't looked into the capabilities of the diff-match-patch widget, though - can it do this out of the box?

PMario

unread,
Dec 13, 2018, 7:39:37 AM12/13/18
to TiddlyWiki
On Thursday, December 13, 2018 at 1:19:52 PM UTC+1, BurningTreeC wrote:

I haven't looked into the capabilities of the diff-match-patch widget, though - can it do this out of the box?

I was thinking about the low-level box: https://github.com/google/diff-match-patch/wiki/API

-m

BurningTreeC

unread,
Dec 13, 2018, 7:41:48 AM12/13/18
to TiddlyWiki
To explain what the (complicated, yes that's true) macros do:

they split the input string into single characters prefixed with an index from 1 to text-length, so I have an array:

1h 2e 3l 4l 5o 6%20 7{ 8{ 9$ 10: 11/       and so on

they are numbered so that each list-item is unique

depending what I want to do, I can enlist these numbered characters, remove the prefixed number, knowing at what index I am, and doing stuff with the character, like matching it and the following ones against a prefix or just replacing it or whatever

I can also count the lines if I want, telling at which line something is, I guess I can also figure out the index of the char at that specific line

I want to see how far I can go with wikitext only :P


PMario

unread,
Dec 13, 2018, 7:50:15 AM12/13/18
to TiddlyWiki
On Thursday, December 13, 2018 at 1:41:48 PM UTC+1, BurningTreeC wrote:
...
I want to see how far I can go with wikitext only :P

I see. ... forgive my ignorance: IMO That's completely inefficient. ... Some string manipulations, like find and replace are 1 liners in javascript.

So if you want to have "multi-line" or "tag-like" fields you'd need to dig much deeper into the core and fix it there.

-m

BurningTreeC

unread,
Dec 13, 2018, 7:57:31 AM12/13/18
to TiddlyWiki

On Thursday, December 13, 2018 at 1:41:48 PM UTC+1, BurningTreeC wrote:
...
I want to see how far I can go with wikitext only :P

I see. ... forgive my ignorance: IMO That's completely inefficient. ... Some string manipulations, like find and replace are 1 liners in javascript.

I forgive. This is very inefficient. But we shouldn't ignore the point how far wikitext can go, could help understanding what not to do and what to fix where.


So if you want to have "multi-line" or "tag-like" fields you'd need to dig much deeper into the core and fix it there.

-m

I think I don't want such fields, I just want to get those newline characters correctly into my testtiddler. I believe the problem here is that I'm using diff-match for visualizing that does some processing I don't want

BurningTreeC

unread,
Dec 13, 2018, 8:09:15 AM12/13/18
to TiddlyWiki
That was the problem! :)

Now I've removed diff-match, I used it just to see where those characters are, made other problems by inserting invisible space or whatever somewhere

PMario

unread,
Dec 13, 2018, 8:14:30 AM12/13/18
to TiddlyWiki
On Thursday, December 13, 2018 at 2:09:15 PM UTC+1, BurningTreeC wrote:
...

I think I don't want such fields, I just want to get those newline characters correctly into my testtiddler. I believe the problem here is that I'm using diff-match for visualizing that does some processing I don't want

That was the problem! :)

Glad you found it!

-m
Reply all
Reply to author
Forward
0 new messages