As promised, here is a version of the script that will work if you select
either the *@jupytext* node or any of its immediate children. Since the
tree will only be one level deep, clicking on any node in the tree will do.
"""Convert an @jupytext node tree for rendering with VR3.
Select any node of the @jupytext tree before running this script.
"""
MD_MARKER = '# %% [markdown]'
CODE_MARKER = '# %%'
PREFIX = '<< prefix >>'
AT_MD = '@language md'
AT_CODE = '@language python'
MD_LITERAL_FENCE = "```"
def process_first_line(line:str) -> tuple(str, bool):
"""Convert the first line of a cell for VR3."""
is_code = False
if line.startswith(MD_MARKER):
line = AT_MD
elif line.startswith(CODE_MARKER):
line = AT_CODE
is_code = True
if line.startswith('# '):
line = line[2:]
return (line, is_code)
found_head = False
count = 0
for p0 in c.p.self_and_parents():
count += 1
if count > 2: # Only look one step up the tree
break
jupy_file = p0.h.split('@jupytext')
if len(jupy_file) > 1:
found_head = True
JUPYTER_POSITION = p0
break
if not found_head:
g.es('Cannot find an @jupytext node')
else:
cells = [AT_MD]
for p in JUPYTER_POSITION.subtree():
is_code = False
headline = p.h
is_prefix = headline == PREFIX
if is_prefix:
cells.append(MD_LITERAL_FENCE)
lines = p.b.split('\n')
firstline = lines[0]
firstline, is_code = process_first_line(firstline)
cells.append(firstline)
for line in lines[1:]:
i = 0 if is_code else 2
cells.append(line[i:])
if is_prefix:
cells.append(MD_LITERAL_FENCE)
body = '\n'.join(cells)
vr3_jupytext_node = JUPYTER_POSITION.insertAfter()
vr3_jupytext_node.h = 'Rendering for ' + jupy_file[1]
vr3_jupytext_node.b = body
c.selectPosition(vr3_jupytext_node)
c.redraw()
On Wednesday, October 30, 2024 at 7:40:42 PM UTC-4 Thomas Passin wrote:
> Now that we have a more stable import format for @jupytext nodes, I redid
> my script to produce a version that renders the file with VR3. For this
> version, you need to select the head node of the @jupytext tree. I will
> make the script able to walk up the file tree to find the @jupytext top
> node but I wanted to get the script out there for testing as soon as
> possible. Note that this script will not modify the @jupytext file in any
> way.
>
> To use, copy the script to a node somewhere in the outline in which you
> have an @jupytext external file. Select the command's node and use the
> "script-button" button to create a button for the command. Then highlight
> the @jupytext node and press the button.
>
> The command will create a new node below your @jupytext tree and will
> select it. VR3 may even open automatically and render it. If you activate
> the command and you haven't selected an @juptext node nothing will happen
> except a message about it.
>
> Please give this script a good workout so we can fix up any edge cases.
>
> """Convert an @jupytext node tree for rendering with VR3.
>
> The selected node must be the head of the tree.
> """
>
> MD_MARKER = '# %% [markdown]'
> CODE_MARKER = '# %%'
> PREFIX = '<< prefix >>'
>
> AT_MD = '@language md'
> AT_CODE = '@language python'
> MD_LITERAL_FENCE = "```"
>
> JUPYTER_POSITION = c.p
>
> def process_first_line(line:str) -> tuple(str, bool):
> """Convert the first line of a cell for VR3."""
> is_code = False
> if line.startswith(MD_MARKER):
> line = AT_MD
> elif line.startswith(CODE_MARKER):
> line = AT_CODE
> is_code = True
> if line.startswith('# '):
> line = firstline[2:]
> return (line, is_code)
>
>
> jupy_file = JUPYTER_POSITION.h.split('@jupytext')
> if len(jupy_file) == 1:
> g.es('Not an @jupytext node')
> else:
> cells = [AT_MD]
> for p in JUPYTER_POSITION.subtree():
> is_code = False
> headline = p.h
> is_prefix = headline == PREFIX
> if is_prefix:
> cells.append(MD_LITERAL_FENCE)
> lines = p.b.split('\n')
> firstline = lines[0]
> firstline, is_code = process_first_line(firstline)
> cells.append(firstline)
> for line in lines[1:]:
> i = 0 if is_code else 2
> cells.append(line[i:])
> if is_prefix:
> cells.append(MD_LITERAL_FENCE)
>
> body = '\n'.join(cells)
>
> vr3_jupytext_node = c.p.insertAfter()
> vr3_jupytext_node.h = 'Rendering for ' + jupy_file[1]
> vr3_jupytext_node.b = body
>
> c.selectPosition(vr3_jupytext_node)
> c.redraw()
>
--
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/leo-editor/e02972de-2efd-41b4-87d1-77c812be49b5n%40googlegroups.com.