Query about promote-child-bodies script

81 views
Skip to first unread message

lewis

unread,
Apr 18, 2020, 5:41:36 AM4/18/20
to leo-editor
After using a leo script_A to convert a list of lines into separate nodes for each line of p.b.
I have a script_B that reads child nodes, and copies all the body text lines back to the parent. It is based on @button promote-child-bodies, from LeoDocs.

result = [p.b]
b = c.undoer.beforeChangeNodeContents(p)
for child in p.children():
    result.append(f'{child.b}' )    # copy body only
p.b = ''.join(result)
c.undoer.afterChangeNodeContents(p,'promote-child-bodies',b)



Here is a sample of CSV to which I apply the script_A, creating 3 nodes

"Model", "C1", "C2", "C3"
"CSM1", 20, 40, 50
"CSM2", 30, 45, 60


After manually adding a fourth node, containing body text: "CSM3", 40, 55, 70
I run script_B which reads the new node and adds the new body text but it is not on a new line.

"Model", "C1", "C2", "C3"
"CSM1", 20, 40, 50
"CSM2", 30, 45, 60"CSM3", 40, 55, 70

Is it possible that when the nodes are created with script_A, hidden characters are added? I tried using show-invisibles but I only see spaces. Using /n adds new blank lines between all and is not suitable.
Any ideas on how to move it to the next line?

Regards
Lewis

Thomas Passin

unread,
Apr 18, 2020, 9:10:04 AM4/18/20
to leo-editor


On Saturday, April 18, 2020 at 5:41:36 AM UTC-4, lewis wrote:
After using a leo script_A to convert a list of lines into separate nodes for each line of p.b.
I have a script_B that reads child nodes, and copies all the body text lines back to the parent. It is based on @button promote-child-bodies, from LeoDocs.

You need to include the separators you want, in this case a newline:

p.b = '\n'.join(result)

lewis

unread,
Apr 18, 2020, 10:27:53 PM4/18/20
to leo-editor
Using 
p.b = '\n'.join(result)

Here is the output result:

"Model", "C1", "C2", "C3"

"CSM1", 30, 40, 50

"CSM2", 30, 45, 60
"CSM3", 30, 55, 70


Which as expected adds an extra leading newline between the text for each node. Except in the case of the last node which I added manually, by <Ctrl-i> insert-node command.
Note that it is the different behaviour of the manually added nodes which I can't explain.

Regards
Lewis

vitalije

unread,
Apr 19, 2020, 3:07:59 AM4/19/20
to leo-editor
The problem you are facing is that not all of your child positions have '\n' at the end of the body. One solution would be to append child.b.strip() to the result in the loop and at the end to use '\n'.join(result).

Vitalije

lewis

unread,
Apr 19, 2020, 3:53:18 AM4/19/20
to leo-editor
Hi Vitalije,

Before your reply I had been looking at the leo files in Scite and noticed when nodes are created with script to convert lines to nodes, a linefeed is added before the </t>, but the last node does not have a line feed before </t>  (see body text G, H):
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1" >
<leo_header file_format="2"/>
<globals/>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="lewis.20200419120026.1"><vh>CSV NEW</vh></v>
<v t="lewis.20200419172940.1"><vh>CSV NEW</vh>
<v t="lewis.20200419172940.2"><vh>LineNumber 0</vh></v>
<v t="lewis.20200419172940.3"><vh>LineNumber 1</vh></v>
<v t="lewis.20200419172940.4"><vh>LineNumber 2</vh></v>
<v t="lewis.20200419172940.5"><vh>LineNumber 3</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="lewis.20200419120026.1">A, B,
C, D
E, F
G, H</t>
<t tx="lewis.20200419172940.1"></t>
<t tx="lewis.20200419172940.2">A, B,
</t>
<t tx="lewis.20200419172940.3">C, D
</t>
<t tx="lewis.20200419172940.4">E, F
</t>
<t tx="lewis.20200419172940.5">G, H</t>
</tnodes>
</leo_file>

Whereas if I create a new node using <Ctrl-i> insert-node command, the treatment is only to add the </t>
[snip]
<t tx="lewis.20200419173505.4">E, F
</t>
<t tx="lewis.20200419173505.5">G, H</t>
<t tx="lewis.20200419173516.1">I, J</t>
</tnodes>
</leo_file>

It is this difference which causes the promote-child-bodies script to copy the body text back without linefeeds. Would it be more consistent for a linefeed to precede </t> always? Why is the last line treated differently?
I am not an expert at all on linefeed terminology so apologies if this is a poor suggestion.

Regards
Lewis

vitalije

unread,
Apr 19, 2020, 5:11:18 AM4/19/20
to leo-editor


On Sunday, April 19, 2020 at 9:53:18 AM UTC+2, lewis wrote:

It is this difference which causes the promote-child-bodies script to copy the body text back without linefeeds. Would it be more consistent for a linefeed to precede </t> always? Why is the last line treated differently?
I am not an expert at all on linefeed terminology so apologies if this is a poor suggestion.

Regards
Lewis


The node's body can contain whatever you wish. If you wish body to have a linefeed at the end of the body, you can add it yourself. If you don't want it you can delete it using your keyboard or script. I guess it is more natural to create linefeed at the end of body when you type it using keyboard. When you create bodies using script it is easier to forget about this last linefeed. But you certainly can add it and delete it both using keyboard and the script. When combining bodies you should bare this in your mind and decide on your strategy. You can either normalize all bodies to have at least one linefeed at the end and join them without adding an extra linefeed, or you can strip the last linefeed from all bodies that have it and then join them using linefeed as a separator.

Leo's file format represent node bodies correctly. If the body doesn't have linefeed at the end, there will be no linefeed before </t>, and if it has there will be linefeed before closing </t>. That way Leo keeps the original content of your bodies.

HTH Vitalije

Edward K. Ream

unread,
Apr 20, 2020, 11:20:02 AM4/20/20
to leo-editor
On Sat, Apr 18, 2020 at 9:27 PM lewis <lewi...@operamail.com> wrote:
Using 
p.b = '\n'.join(result)

You can strip away all blanks lines as follows:

result = [z.rstrip() for z in result]

Other approaches are possible too.

Edward
Reply all
Reply to author
Forward
0 new messages