how to get all the content of the body?

48 views
Skip to first unread message

HaveF HaveF

unread,
Jun 28, 2023, 1:29:23 AM6/28/23
to leo-editor
Hi, Leo user,

Who knows how to get all the contents of the body?
What I want is to replace << xxx >> and @others in the body.

I have created a simple function, but it has problems handling child nodes or children of child nodes. I suspect there is a ready-made function, but I can't find it anyway, can anyone tell me about it?

Thanks!

Best,
HaveF

Thomas Passin

unread,
Jun 28, 2023, 8:26:10 AM6/28/23
to leo-editor
I think you want the method g.getScript().  Look at its docstring in LeoPyRef.leo to see how to use it.

BTW, here is how I found it.  I remembered that Leo scripts get run by writing a file called ScriptFile.py. So I searched for that using the Nav tab.  The first thing that came up was in executeScriptHelper, so I looked at that hit. Its parent is the command execute-script.  That seemed promising.  Looking in  execute-script , I noticed a line script = g.getScript(c, p or c.p, useSelectedText=useSelectedText).  So I searched for getScript, and that seems to be what you want.

This is typically how I find relevant code in Leo's code base.  Others probably have their own ways, and mine is probably not the most "efficient" way to go about it.  The Nav tab makes it feasible and convenient. It's a brilliant feature.  The plugin's docstring says it was created by Ville M. Vainio, and I applaud him for it.

HaveF HaveF

unread,
Jun 28, 2023, 9:09:40 AM6/28/23
to leo-e...@googlegroups.com
On Wed, Jun 28, 2023 at 8:26 PM Thomas Passin <tbp1...@gmail.com> wrote:
I think you want the method g.getScript().  Look at its docstring in LeoPyRef.leo to see how to use it.
Super useful function! It works! You save my day, Thomas!
I finally don't have to use my crappy, buggy recursive expansion functions that I wrote myself...

 

BTW, here is how I found it.  I remembered that Leo scripts get run by writing a file called ScriptFile.py. So I searched for that using the Nav tab.  The first thing that came up was in executeScriptHelper, so I looked at that hit. Its parent is the command execute-script.  That seemed promising.  Looking in  execute-script , I noticed a line script = g.getScript(c, p or c.p, useSelectedText=useSelectedText).  So I searched for getScript, and that seems to be what you want.

This is typically how I find relevant code in Leo's code base.  Others probably have their own ways, and mine is probably not the most "efficient" way to go about it.  The Nav tab makes it feasible and convenient. It's a brilliant feature.  The plugin's docstring says it was created by Ville M. Vainio, and I applaud him for it.
Thanks for your detailed way.
Maybe I need to take the time to get a general idea of what all these functions are doing...


--
--
Sincerely,

HaveF

Edward K. Ream

unread,
Jun 28, 2023, 10:14:50 AM6/28/23
to leo-e...@googlegroups.com
On Wed, Jun 28, 2023 at 8:09 AM HaveF HaveF <iamap...@gmail.com> wrote:

> Super useful function! It works! You save my day, Thomas!

Thanks, Thomas, for your help.

Edward

Thomas Passin

unread,
Jun 30, 2023, 10:17:35 PM6/30/23
to leo-editor
I tested the script with the latest revision of the PR3215 branch that has all the UNL/GNX changes.  It still works, Glory Hallelujah!

Thomas Passin

unread,
Jun 30, 2023, 10:21:33 PM6/30/23
to leo-editor
On Friday, June 30, 2023 at 10:17:35 PM UTC-4 Thomas Passin wrote:
I tested the script with the latest revision of the PR3215 branch that has all the UNL/GNX changes.  It still works, Glory Hallelujah!

On Wednesday, June 28, 2023 at 10:14:50 AM UTC-4 Edward K. Ream wrote:
On Wed, Jun 28, 2023 at 8:09 AM HaveF HaveF <iamap...@gmail.com> wrote:

> Super useful function! It works! You save my day, Thomas!

Hmm, I must have posted that script in a different thread.  Here's the one I'm talking about:

UNL = 'unl://C:/Tom/git/leo-editor/leo/core/LeoPyRef.leo#Code-->Core classes-->@file leoGlobals.py-->g.Urls & UNLs-->g.handleUnl'

c0, p0 = c, c.p
c2 = g.handleUnl(UNL, c)
content = c2.p.b  # <---- here is the content of the UNL's body

if c2 is not c0:
    g.app.selectLeoWindow(c0)

c0.selectPosition(p0)
print(content[:100])  # To verify we got the right text


Edward K. Ream

unread,
Jul 1, 2023, 4:51:16 AM7/1/23
to leo-e...@googlegroups.com
On Fri, Jun 30, 2023 at 9:17 PM Thomas Passin <tbp1...@gmail.com> wrote:
I tested the script with the latest revision of the PR3215 branch that has all the UNL/GNX changes.  It still works, Glory Hallelujah!

:-) Keep on testing!

Edward
Reply all
Reply to author
Forward
0 new messages