Why would this code strip line feeds?

Why would this code strip line feeds?

Postby kunkel321 » Mon Mar 17, 2014 3:50 pm

Volker had previously helped me viewtopic.php?f=1&t=453&start=0 with a script that deletes a node, after pasting its text to the note field of its parent...
Code: Select all
def task = node.plainText
def datetime = new Date()
def parent = node.parent
parent.note += """
====================
Task: ${task}
Completed: ${format(datetime)}
===================
"""
node.delete()

It works well, but in my previous setup, if would occasionally strip out the line feeds (line breaks?) in the entire note field that is was pasting into (thus making the note into one long line of text).
Caveat: I'm not 100% sure that it was this script, and not some other process that was happening in my map.... I was never able to intentionally reproduce the effect, but it only ever happened to nodes that had things logged into them...

Does anyone have ideas as to why this would happen to my node.notes?
Thanks in advance for any ideas.
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Why would this code strip line feeds?

Postby boercher » Thu Mar 20, 2014 12:10 am

The problem probably is that notes have HTML content. The method you are using converts a plain text into HTML by caring for line breaks and something more. The method seems to perfom not so well on concatinating the old and the added HTML string. If you'd use node.noteText instead of node.note you gain full control over the HTML as this is what the properties contains.

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Why would this code strip line feeds?

Postby kunkel321 » Thu Mar 20, 2014 4:57 pm

Hi Volker,
Again, thanks for the feedback.
I think I might not be doing your suggestions correctly...
I tried changing the first line
def task = node.plainText
into
def task = node.noteText
But it gets pasted into the note as
=============
Task: null
Completed: 3/20/12
=============


Also, I'm just testing the, before mentioned, "stripping" problem. It never occurs the first time a child gets logged into the notes. However if a second child gets logged in then the effect happens. What happens is that the newly logged item retains it's line-breaks, but everything that was already in the note, gets smooshed together...

Is it possible that the "+=" in Line 4
parent.note += """
is the culprit?

Is it possible that this is a bug?
================

The effect appears to be reproducible.. If anyone else cares to try, here's the script:
Code: Select all
def task = node.plainText
def datetime = new Date()
def parent = node.parent
parent.note += """
=======
Task: ${task}
Logged: ${format(datetime)}
"""
node.delete()


And here are some shots of the effect happening...
Image
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Why would this code strip line feeds?

Postby boercher » Fri Mar 21, 2014 12:24 am

Use of node.note unfortunately converts to plain text. This should work:
Code: Select all
parent.noteText += """
<p>
  =======<br>
  Task: ${task}<br>
  Logged: ${format(datetime)}<br>
"""


Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Why would this code strip line feeds?

Postby kunkel321 » Fri Mar 21, 2014 4:38 pm

Not much luck there either unfortunately..
Putting the ".noteText" in that location actually breaks the script.
Code: Select all
def task = node.plainText
def datetime = new Date()
def parent = node.parent
parent.noteText += """
<p>
  =======<br>
  Task: ${task}<br>
  Logged: ${format(datetime)}<br>
"""
node.delete()


Not sure why, but if I "Execute on all recursively" it seems to run without an error dialog (though it fails to log the item as expected).
If I "Execute on one selected" or "Execute on all selected" then I get this error
Image

I'll experiment more later.... I can also post a screencast, if that helps.
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Why would this code strip line feeds?

Postby boercher » Mon Mar 24, 2014 12:16 am

You should make sure that there are no cut & paste artifacts that lead to these errors. It appears to me that your scripts contains sometimes some more characters ("=") and sometimes some less (""") than posted here.

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Why would this code strip line feeds?

Postby kunkel321 » Mon Mar 24, 2014 4:33 pm

It actually does occur to me that I have some extra utilities running in the background... MarcoExpressPro is a Windows automation app. I do have it set to "prefix" mode and the prefix is an equals sign... (i.e. if I type an "=" then MacExp is allerted to see if a macro trigger will follow). Also, I have ClipboardHelp+Spell running, and that is a clipboard monitor...

Alas, I deactivated both of these this morning, prior to starting FreePlane, but my current Groovy problems persist...
IDEA: Maybe I'll try to "capture" the parent.node.text and manually put it with the logged information, then paste the whole lot in. Maybe that will preserve my line breaks...

Anyway, my apologies for being a pest. :oops:
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Why would this code strip line feeds?

Postby boercher » Wed Mar 26, 2014 11:40 pm

See this post for the resolution: ''node.note'' tries to evaluate note texts starting with an equal sign a a formula. Use ''node.noteText'' if you don't want/need that.

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Why would this code strip line feeds?

Postby kunkel321 » Thu Mar 27, 2014 3:53 pm

Hi Volker,
Thanks for the reply.
You had previously (above, in this thread) suggested node.noteText (though it actually appears as "parent.noteText" where "parent" is a variable).
It seems to break the script though...
The item does not get pasted into the parent notes.
Oddly, my above post shows some error messages, but today the script is running without any error messages.

I was experimenting with trying to preserve the content of the original note
Code: Select all
def task = node.plainText
def datetime = new Date()
def student = node.parent
def casenote = parent.note.to.plain
student.note = casenote + """
zzzzzzzzzz
Task: ${task}
Completed: ${format(datetime)}
"""
node.delete()


The script actually works, but it does not solve the above-mentioned problem of the note text getting smooshed.
Perhaps some other folks can try the script and see if they get the same effect. Remember that it runs correctly the first time, but when you run it a second time on the same parent, everything in the note field, above the newly logged/appended item is stripped of line breaks. (as in the above image).

I can submit a Mantis report this this if it helps. Also, this is not a super-high-priority script for me. I'll admit that it mostly just bugs me that it doesn't work as expected...
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Why would this code strip line feeds?

Postby boercher » Thu Mar 27, 2014 5:07 pm

Sorry, I wasn't following the thread carefully. The following script should do what you want:
Code: Select all
def task = node.plainText
def datetime = new Date()
def student = node.parent
def oldNote = parent.noteText
def plainOldNote = (oldNote) ? htmlUtils.removeHtmlTagsFromString(oldNote) : ""
student.note = plainOldNote + """
zzzzzzzzzz
Task: ${task}
Completed: ${format(datetime)}
"""
node.delete()

I think it should be easier to append plain text to a note. The following methods came to my mind
htmlUtils.toHtml(plain)
htmlUtils.joinHtml(html, html)
and then
parent.noteText = htmlUtils.joinHtml(parent.noteText, htmlUtils.toHtml("new Text"))

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Next

Return to Open Discussion

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron