Repository navigation
Static tree pictures: renderTreeSvg and glpython-tree - #54
Merged
Merged
Conversation
A list that reads as a tree, drawn as an SVG file in Node without a browser: the runtime's tree rule, treeLayout's positions and TreeView's look on a light card; marks for subtrees; byte-identical output; a <title> with the list. Parses Python's literal syntax, writes leaves with Python's repr. Built as its own entry (glpython-editor/tree-svg, no React or Monaco), with the command glpython-tree; the peer dependencies are optional, so a repo using only this doesn't get React. Checks: test:svg (snapshots, positions, CPython's repr, errors, the command). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
markRoot (--mark-root) highlights only the root's box, not its edges or children, with the same look as a marked subtree's boxes; it combines with mark and marks a single value's leaf. The description names it first: «– markiert: Wurzel» / «– marked: root». Snapshots for it alone, with a subtree, and on a single value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
P43: a list that reads as a tree, drawn as a static SVG file in Node, without a browser, for a compile step (slides, solutions, quiz options, print).
What it does
renderTreeSvg(text, options) → string(src/treeSvg.ts):True,False,None, lists and tuples. Leaves are written with Python's repr (2.50→2.5,"it's",1e+16). A single value is a tree of one leaf.listtreerule (_glpython_tree_of): a first element that is a string is the label, without quotes. Unlike the visualizer, long texts aren't shortened.layoutTreefromtreeLayout.ts, so the positions are the onesTreeViewuses.TreeView's, with the light theme's colours. Solid node boxes, dashed leaf boxes, string and number colours, and the same edges, spacing and radius.monospace, at 16 px by default. Widths are in characters (0.6 em each), and each text is fitted to that width withtextLength, so it doesn't overflow its box in a different monospace font.mark: each marked subtree has its boxes and the edges between them in the accent colour, with the boxes filled--glp-current. Marking the whole tree marks all of it. A mark that occurs twice or not at all is an error.markRoot(--mark-root): the root node's box alone, not its edges or children, in the same look as a marked subtree's boxes. It combines withmark, and on a single value it marks that leaf. The description names it first: «– markiert: Wurzel» / «– marked: root», e.g. «– markiert: Wurzel; ['*', 3, 4]».role="img"and a<title>with the list, plus– marked: …(lang: 'en') or– markiert: …(lang: 'de').TreeSvgErrornaming the position with a caret, or explaining why a mark is ambiguous or missing.glpython-tree(bin/glpython-tree.mjs):glpython-tree "<list>" [--mark "<subtree>"]... [--lang en|de] [--font-size <px>] [-o file.svg];-o, and reads the list from stdin without an argument;dist/tree-svg.js, exported asglpython-editor/tree-svg, with no React or Monaco; it shares the layout chunk withdist/index.js.peerDependenciesMetamakes them optional, so a repo installing the package only for the pictures doesn't get React or Monaco installed. I checked this with agit+fileinstall in an empty project: onlyglpython-editorwas installed, and both the import andnpx glpython-treework.Checks
npm run test:svg(new, in CI after the build):markRootalone, with a subtree mark and on a single value, against snapshots inscripts/tree-svg/, byte for byte and the same twice.treeLayoutplaces its node.role="img", the<title>, and escaped XML.Also checked:
TreeViewin the embed's REPL at the same font size (15 px), side by side; they look the same.test:protocolandtest:layoutpass.How to use it from another repo
The command is then available as
npx glpython-tree …. Installing runsprepare, which buildsdist/, as for the IDE. That takes a moment and needs no TypeScript setup on the course side.Going live
Nothing changes for the embed or the IDE: the workspace bundle is unchanged apart from sharing the layout chunk. A tag (e.g.
v0.1.28) on the merge commit makes it installable.Follow-ups (not done)
textLengthfits the text to its box.1.0doesn't find1, unlike Python's==.🤖 Generated with Claude Code