Looks good! I'll stick with it.

Replaced
        if p.h and p != root:

by
        if p.h and not p.h.startswith( "!") and p != root:

though.

Thanks a lot!

On Monday, August 28, 2023 at 9:58:26 PM UTC+2 tbp1...@gmail.com wrote:

> On Monday, August 28, 2023 at 4:56:12 AM UTC-4 p.os...@datec.at wrote:
>
> Yes, that's the point. It simply should work as the RST-plugin does.
>
>
> I wrote a small script that does what I think you want, although only for 
> a single @md file at a time.  The top-level node must have a headline that 
> starts with "*@md *" and a filename or path, like this: @md test.md. This 
> node may optionally be the child of an *@path *node to write the output 
> file somewhere else besides the outline's location.  To use the command, 
> select any node in the markdown's subtree and invoke the command.
>
> Note that the command as currently written will not recognize an 
> *@auto-md* node.  The node's headline has to start with *@md *.  All 
> headlines will be indented according to their nesting level, except that 
> the top node, the one with the file name, will not have a heading (since 
> the *@md * headline wouldn't make much sense, at least to me).
>
> There are no extra features such as the rst3 command has, but it's a 
> start.  Try it out, see what you think or want to change.
>
> @language python
> """Write an @md file tree with headings corresponding to node indentation.
>
> Searches node and parents to find the nearest node headline beginning
> with *@md*, then writes the entire subtree of that node.
>
> Output file is written to the path in effect at the @md node.
> """
>
> from pathlib import Path
> MD_PREFIX = '@md '
> def find_md_root(p):
>     """Return the top position of an @md tree or None.
>     
>     The top headline must begin with "@md ".
>     """
>     for p0 in p.self_and_parents():
>         if p0.h.startswith(MD_PREFIX):
>             break
>     else:
>         p0 = None
>     return p0
>
> def walk_md(root):
>     """Return a string for the markdown file from root position.
>     
>     Headlines are indented relative to their nesting level under
>     the root."""
>     markdown = ''
>     root_indent = root.level()
>     for p in root.self_and_subtree():
>         if p.h and p != root:
>             indent = p.level() - root_indent + 1
>             markdown += '#' * indent
>             markdown += p.h + '\n'
>         markdown += p.b + '\n'
>     return markdown
>
> root = find_md_root(p)
>
> # Get path for node; path does not include the filename
> path = Path(c.getNodePath(root)) if root else None
> if path and path.exists():
>     fname = root.h[len(MD_PREFIX):]
>     path = path.joinpath(Path(fname))
>     md = walk_md(root)
>     try:
>         with open(path, 'w', encoding = 'utf-8') as f:
>             f.write(md)
>         g.es(f'Wrote {path}')
>     except Exception as e:
>         g.es(f'Could not write file at {path}: {e}')
> else:
>    g.es(f'Path {path} does not exist')
>
>

-- 
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/80359485-a570-4940-a870-e2622016b4e8n%40googlegroups.com.

Reply via email to