Creating a new line character
Hey, I am using Nodebox for a while now and while the documentation is sparse this forum has been a valuable source for a deeper understanding and tipps and tricks.
I think I can contribute on trick that I have not read about in the other posts yet.
Problem: I wanted to import a CSV file from an online source. So I could not rely on the CSV import node, but had to parse the server response into a table on my own.
For that I needed to split the string by line breaks at some point. But none of the string related nodes let's you input a line break anywhere.
So I ended up writing a little custom node that let's you specify a character by ASCII/unicode number.
I also read about John's approach of importing the file, looking for any new-line character present, isolating that character and then using that one going forward.
Solution: Recently I stumbled across a nice way by accident:
The format number node is using a specific syntax (Python, Java, Jython?). While reading up on that I noticed that the syntax also allows you to specify a new line character. For example to format a date and time into two separate lines:
2025-Jan-01
1:30PM
So if you just specify the format of the number to be %n
, the format number node will output a single new line character that you can use to split a long text by lines or to insert a line break into a single string.
P.S.: I wanted to attach an example, but the previous attempt to post this did not go through. So I'm trying again without.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by tolifox on 17 Jan, 2025 11:52 AM
Here is a little example network.
Support Staff 2 Posted by john on 18 Jan, 2025 12:19 AM
Tolifox,
This is WAY COOL! Nice work!
The format_number node is perhaps the least documented of all the undocumented nodes in Nodebox. Here is an exchange I had with Frederik about it in 2015 that includes some useful links:
http://support.nodebox.net/discussions/nodebox-2-3/5397-the-format-...
As Frederik explained, format_number is a crippled form of Java's string.format function. Much of the Java functionality is unavailable, and what bits and pieces remain are shrouded in mystery. Over the years I have found a few useful tricks it can do.
But I hadn't spotted this one! And it's quite handy. Many is the time I tried to get by with textpath's width parameter to add line breaks, but those often did not land quite where I wanted them.
Would you mind if I turned this hack into a simple node and added that node to my library? I think it would be a very handy addition.
I have attached a demo of your hack in node form.
I call it "line_break". It's dirt simple. Choose a character, any character that does not occur in your text, to serve as your line break character. The default is a backslash. Then enter your text using that break character wherever you want a line break. The node returns a single string with that character replaced by %n characters.
You can then treat the output as you would any other string, like feeding it into a textpath node to be rendered in a given font.
If you look inside the extremely simple line_break node you will see that the format_number node has a comment giving you credit for discovering the hack.
If you have any objections to including this node in my library let me know. Otherwise I will include it in version 3.6.
Thanks for a great contribution!
John
3 Posted by tolifox on 18 Jan, 2025 04:39 PM
Hey John,
sure, feel free to include it. I'm happy to have found something that might be useful for others here. Thanks for the credit in the comment. :)
I think I might tidy up my GoogleSheets node a bit and drop it here as well.
Support Staff 4 Posted by john on 21 Jan, 2025 09:41 AM
UPDATE!
I have modified my charpath, kern, and text_svg nodes to accept strings with line breaks and to allow control over vertical leading! Details here:
http://support.nodebox.net/discussions/show-your-work/859-line-brea...