If your notes are not too complicated, here's something you could do.

1. Open a Leo file with at least one of your note subtrees.  Select the top 
node of a note.
2. Copy the node to the clipboard (*Outline/Copy Node*).
3. Open an ordinary text editor and paste the clipboard into it.

If you save this with a .leo extension, it will open as an ordinary Leo 
outline. 

The structure should be pretty self explanatory. The node identifiers (they 
are "gnx"s) are your Leo user name concatenated with a time stamp 
(documented somewhere in Leo's codebase but pretty obvious to figure out) 
and you will need to come up with a way for your program to generate them 
so they have the correct format and are unique time stamps.  That won't be 
hard.  Here is a little example from my own workbook.leo file:

<!-- Created by Leo: https://leo-editor.github.io/leo-editor/leo_toc.html 
-->
<leo_file 
xmlns:leo="http://leo-editor.github.io/leo-editor/namespaces/leo-python-editor/1.1";
 
>
<leo_header file_format="2"/>
<vnodes>
<v t="tom.20230828142729.1"><vh>@md md-test-file.md</vh>
<v t="tom.20230828143844.1"><vh>Node 1</vh>
<v t="tom.20230828143916.1"><vh>Node 1.1</vh></v>
</v>
<v t="tom.20230828143933.1"><vh>Node 2</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="tom.20230828142729.1">This is the head of the MD file.
</t>
<t tx="tom.20230828143844.1">This is node 1.
</t>
<t tx="tom.20230828143916.1">Node 1.1
</t>
<t tx="tom.20230828143933.1">Node 2
</t>
</tnodes>
</leo_file>

You can see that the *vh* elements represent the headlines and the 
corresponding *t* elements represent the body text.  As long as your notes 
do not use any special XML characters you can put the text directly into 
the right places.  Otherwise you will have to perform XML escaping on the 
text.  The file will have to be encoded with utf-8, but that should be the 
default for any text file you write with Python 3+.

The whole thing will be pretty easy to generate with a little Python code.  
I don't think it's necessary to actually generate the file with an XML 
library but that would be easy too, especially if you already know how to 
do work with the library.  The standard Python library 
*xml.etree.ElementTree* should do.
On Thursday, September 7, 2023 at 10:07:08 PM UTC-4 brian wrote:

> I’ve written a program that manages media and creates playlists.  It does 
> things like download RSS feeds, convert video to audio, sync with my cell 
> phone, etc.  
>
> I use Leo to keep notes.  When I find media that I want to add to my Leo 
> notes, I manually create the node and put in my notes in the text body.  I 
> want to expand my software where in addition to creating playlists, it will 
> create a .leo file so that I only have to add my notes.  
>
> I do NOT want to interact with an existing Leo instance.  If I did, I 
> would use leoBridge.  I want to create a .leo file that I can open with 
> Leo.  Like my program outputs playlists now, I want to output a .leo file 
> that I can open with Leo.  
>
> Looking at the API and classes, it would seems it would be possible to 
> create a .leo file.  It seems the code base has the core and the GUI in 
> different modules.  I like using the API or python classes most since I can 
> use my IDE and debugger.  Another option would be to spin up another 
> instance of Leo and use the bridge.  Another option would be to have my 
> program output the code that I could paste into a leo node to execute.  
>
> I have not considered outputting a format that leo can read directly.  It 
> would still be easier to code and debug if I used the Leo API or Leo code 
> base.  
>
> On Thursday, September 7, 2023 at 4:57:18 PM UTC-4 tbp1...@gmail.com 
> wrote:
>
>> Could you say more about what you want to accomplish?  Maybe there is 
>> another way to go about it.  Do you really need to open Leo 
>> programmatically from another program?  For example, you could write some 
>> (non-Leo) file with your program, launch Leo (from your program) with that 
>> file on the command line, and Leo would open or import that file.
>>
>> Well, it *used* to but just now when I tested it, you only get a node 
>> with the file name (if it's not a .leo file) and no content.  I don't know 
>> if that is now the intended behavior or not but it's a change.  I'm going 
>> to file an issue on that.
>>
>> Otherwise, to communicate with Leo requires the Leo bridge or some kind 
>> of server like leoserver, but you'd have to write a client for that.  But 
>> perhaps there is another way to do what you want to end up with.
>>
>> On Thursday, September 7, 2023 at 4:16:56 PM UTC-4 brian wrote:
>>
>>> How do I create a Leo outline from within a Python program? I have a 
>>> Python program where I want to export into a Leo outline format file. 
>>>
>>>
>>> All the snippets of code I’ve found assumes the code is running within 
>>> Leo. I want to run outside LEO 
>>>
>>>
>>> I tried this:
>>>
>>> from leo.core import leoGlobals as leo_g
>>>
>>> from leo.core import leoApp
>>>
>>> leo_g.app = leoApp.LeoApp()
>>>
>>> leo_c = leo_g.app.newCommander(‘test.leo’) 
>>>
>>> But I got the exception NoneType object has no attribute 
>>> ‘globalSettingsDict’ 
>>>
>>>
>>> I also tried:
>>>
>>> from leo.core.loeGlobals import createScratchCommander 
>>>
>>> c = createScratchCommander(‘test.leo’)
>>>
>>> I got the same error. 
>>>
>>>
>>> I found the “mod_read_dir_outline.py” plugin but it seems to be written 
>>> to work within Leo. 
>>>
>>>
>>> I found a post about “web-to-outline script” (
>>> https://groups.google.com/g/leo-editor/c/CkS8MLRStsA/m/7xbIowG9BAAJ) 
>>> but I could not find this script. 
>>>
>>>
>>> I also found LeoBridge to interface with an existing instance of Leo but 
>>> I just want to create an leo outline in another program and then manually 
>>> open the file with Leo.  I don't want to risk corrupting an existing 
>>> leo file.  
>>>
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/4cb8fb6b-365b-4fe2-9b76-797dfd09bac3n%40googlegroups.com.

Reply via email to