Predictygraph DSL — specification v0.2 (draft)¶
Predictygraph is a small, line-oriented text format for diagrams (flowcharts, architecture graphs, trees, grids). Design goals:
- Readable diffs — one statement per line, no XML, no coordinates unless you ask.
- Layout is explicit and local — every container (graph or group) chooses its own layout ("balancing style"). A layered flow can contain a grid, which can contain a radial cluster. Changing one group never reshuffles the others.
- Styling is plain CSS — a small, documented subset resolved by the tool itself. Output is static SVG with presentation attributes; no browser, no JS.
- Language agnostic — the grammar below is the contract. Python is only the reference implementation.
File extension: .pdg. Encoding: UTF-8.
1. Lexical rules¶
- Comments:
#followed by a space (or end of line), at line start or after whitespace.#fffand#idare therefore never comments. Not applied insidecss { }. - Strings:
"..."with\",\\,\nescapes.\nin labels makes a line break. - Identifiers:
[A-Za-z_][A-Za-z0-9_-]*(case-sensitive). - Classes:
.name— attach CSS classes. - Attributes:
key=valuewhere value is an identifier, number, string, orx,y. - Statements end at newline.
{opens a block,}closes it (own line or end of line).
2. Statements¶
graph [attrs] # optional header, must be first statement
css "file.css" # external stylesheet (relative to the .pdg file)
css { ...raw css... } # inline stylesheet (braces must balance)
default node|edge|group [attrs] # defaults for everything after this line in scope
ID ["Label"] [.class...] [attrs] # node
A ARROW B [ARROW C...] ["label"] [.class...] [attrs] # edge chain
A, B ARROW C, D # fan-out/fan-in (cartesian product)
group ID ["Label"] [.class...] [attrs] { # container with its own layout
...statements...
}
Nodes referenced in an edge but never declared are created implicitly in the current scope with their ID as label. A node belongs to the scope where it is first declared or referenced — so declare nodes inside their group first and connect them from anywhere.
2.1 Variables and reusable blocks¶
let NAME = VALUE # value variable, VALUE = identifier, number, string or x,y
def NAME [PARAM[=DEFAULT]...] { # reusable block
...statements...
}
use NAME ID ["Label"] [.class...] [PARAM=VALUE...] [attrs] # place a copy of the block
$NAMEis replaced by the variable's value anywhere a value can stand: labels, attribute values, class names. Inside strings write"${NAME}". An unknown$NAMEis an error. Variables follow scope: one set in a group is visible only in that group.defstatements produce nothing.useplaces one copy of the block as agroupwith the givenID, label, classes and attrs, so it gets its own layout like any group.- Inside a block,
$IDand$LABELrefer to theuseline's ID and label, and each parameter is a variable. Parameters without a default must be given onuse. - IDs inside a block are local. A copy's node
cpubecomesID-cpu, so two copies never clash. Connect to it from outside with that full name (node1-cpu -> switch). defmay appear anywhere before its firstuse; a block mayuseother blocks but not itself.
let accent = "#3939D8"
def server gpus=2 {
cpu "CPU"
gpu "GPU x${gpus}" fill=$accent
cpu -> gpu
}
group rack layout=row {
use server node1 "Node 1"
use server node2 "Node 2" gpus=8
}
scheduler -> node1-cpu, node2-cpu
3. Edges¶
Arrow token = optional <, a body of -, = or . characters, optional >
(a body without any head needs 2+ chars).
- Body
-solid,=thick (width x2.5),.dashed (first char decides). >at the end: arrowhead at the target;<at the start: arrowhead at the source.
Examples: a -> b, a <-> b, a -- b (no heads), a ==> b, a <.. b.
Per-edge attributes override arrow defaults:
width=3— stroke width in pxarrow=none|end|start|both— overrides what the token sayshead=triangle|open|diamond|dot|bar— arrowhead shapehead-size=N— arrowhead length in px (default grows with width: 6 + 2.2 x width, max 18). Heads are drawn as geometry, not SVG markers: the tip sits exactly on the target outline, the stroke stops at the head's base, and the head is aligned with the flow axis.route=straight|ortho|curve|bend— routing for this edgecurve: full S-shaped bezier.bend: straight in the middle, rounded only at the ends. The edge leaves and enters nodes along the flow axis, runsstub=px, then bends with cornerradius=(default 12) into a straight diagonal.ortho: right angles;radius=rounds the corners (default 6).radius=N,stub=N— bend tuning (also graph-wide and as CSSradius:/stub:onedge)diagonal=N|none|always— when a bent edge may use one slanted segment instead of a right-angle jog. Everybend/orthoedge is: stub along the flow axis, one jog sideways, stub into the target. The jog is diagonal only if the sideways offset is at most N px and the slant fits between the two stubs atdiagonal-angle; otherwise it is a perpendicular run (Z shape). Aligned ports are always one straight line. Default:40forroute=bend,noneforroute=ortho.alwayssteepens the angle as needed so every jog is diagonal.diagonal-angle=45— slant angle in degrees from the flow axis (10–80; 45 = octilinear)fan=spread— edges leaving (or entering) one side of a node use separate ports spread along that side, turn right after their stub and slant atdiagonal-angle, so a fan-out reads as parallel branches instead of merging into one ortho bus. A spread branch that already ends within 24px of level with its target is snapped straight (one per source): the column/row gap between sources and the target's rank shift to line it up, so no hand-tunedgap=/paddingis needed.- Both keys work on edges, groups (nearest group wins), the
graphline and in CSS. - Lanes: when the sideways runs of different edges would land on the same line and
overlap, they are spread into parallel lanes (9 px apart, ordered so they don't cross).
Edges sharing a source or target and the same look (body, dash, colour, classes) are
a deliberate bus and stay merged; a dashed
..>path into the same node is a distinct trace and gets its own lane. exit=top|bottom|left|right,enter=...— pin the side an edge leaves/enters (port at the side's middle). Mixed vertical/horizontal sides draw an L with one rounded corner. The same side at both ends (exit=left enter=left) draws a C that goes around every box between.
Automatic routing rules (layered layouts):
- Edges between the same two nodes are offset in parallel; A <-> B draws one double-headed edge.
- Ortho edges sharing a target (fan-in) or source (fan-out) turn at one common level, forming a bus.
- Rectangular nodes that overlap on the cross axis are joined by a single straight line (no jog).
- weight=N — how strongly a layered layout keeps the edge short/straight (default 1)
- constraint=false — the edge is drawn but ignored by layout
Graph-wide defaults: graph edge-width=1.5 arrow=end head=triangle route=curve or
default edge width=2.
4. Nodes¶
Attributes:
shape=box|square|round|pill|circle|ellipse|diamond|hexagon|cylinder|note|none|image(square= box forced to equal width and height)
Edge endpoints may be node IDs or group IDs. a -> pool attaches to the group's outer box.
- image=path.svg|path.png — makes the node an image leaf. Default shape=image
(no box, label below). With an explicit shape the image sits inside the box above the label.
- img-width=48 img-height=48 — image size (defaults: intrinsic, max 96px).
- width=W height=H — minimum size.
- at=x,y — position, only honoured by layout=fixed.
- rank=N (layered) / row=R col=C (grid) — pin a slot.
- align-x=ID — after layout, centre this node or group horizontally on another item,
even one in a different panel (line a box up with the one it calls below). It only
moves inside its parent's content box and never past a sibling, so nothing overlaps.
- sub="second line" — secondary text under the label, on nodes and groups. Styled with
the sub selector (default: grey, 0.85em, never bold); node.x sub { ... } targets one
class. \n makes several lines.
4.1 Annotations¶
- Steps:
a -> b step=3draws a numbered circle for the edge: just left of the edge label when there is one; otherwise on the line a third of the way along, or, when the edge is too short for that (three circle widths), beside its middle, clear of any box. Column gaps with a numbered edge widen to fit the circle.graph steps=autonumbers every edge without its ownstep=in file order. Styled withstep(fillis the circle,colorthe number). - Legend:
legend "Key"names the box;legend .green "HAMi component"adds an entry whose swatch is styled like a node with those classes. Drawn under the diagram; top level only, at most 64 entries. Styled withlegend. - Caption:
graph caption="Figure 1: ..."draws a line under the diagram (and the legend). Styled withcaption(default: grey, italic, centred). - Accent bar: CSS
accent-bar: 4px #16a34adraws a strip inside the box edge and widens the box by its width;accent-side: left|right.accent-bar: noneremoves it. - Header band: CSS
title-fill: #dbeafeon a group fills a strip behind its title and subtitle. Ignored withlabel-position: bottom. - Vertical panel label: CSS
label-position: lefton a group turns its title 90° and puts it in a column at the panel's left edge. Padding moves the content, not the label, so a leftpaddingmakes room for an edge to pass between them. - Side brackets:
bracket a, b "note"draws a curly bracket right of the diagram from the top of the highest listed item to the bottom of the lowest, with the note beside it. Overlapping brackets stack outward, shorter spans nearest the diagram. Top level only; styled withbracket(stroke,stroke-width, text keys).
Not yet supported: highlight boxes that don't change the layout, and badges on node corners.
5. Layouts (balancing styles)¶
Every container takes layout= and its own options. Groups are laid out first
(inside-out), then treated as a single box by their parent. Edges between members
of different groups are projected onto the group boxes for the parent's layout, then
routed between real nodes.
layout=layered(default) — rank-based flow (Sugiyama-style).dir=TB|BT|LR|RLbalance=center|start|end|median—centercentres each rank,medianpulls nodes towards their neighbours (dot-like),start/endjustify ranks.rank-gap=60 node-gap=30order=source|auto—sourcekeeps file order inside each rank (no crossing minimisation, the most stable);autominimises crossings.layout=grid—cols=N(orrows=N),align=center|start|end,gap=24.layout=row/layout=column— single line,align=,gap=.align=stretchmakes every child as wide (column) or tall (row) as the largest — equal-width tiers for stack diagrams. A stretched group keeps its content left;justify=centercentres it.layout=radial—center=ID(default first child),radius=auto|N.layout=fixed— children useat=x,y(top-left, relative to the container).
Default for a group is layout=row when it has no internal edges, else layered
(set explicitly for stable results).
6. Styling (CSS subset)¶
Selectors: type (node, edge, group, graph, label), .class, #id,
compounds (node.db, edge.hot), group descendant (#cluster node). Specificity
and source order follow CSS. Order of precedence:
built-in defaults < stylesheets < default statements < inline attributes.
Stylesheets are layered, each overriding the one before:
default.css, shipped with predictygraph (font, sizes, neutral colours)- the theme (
graph theme=…or--theme), if any - css in the
.pdgfile (css { … }blocks andcss "file.css") --cssfiles given on the command line
Themes only set colours and shape unless their TOML sets [shape] font, so the
default font survives a theme change.
Supported properties:
- Box:
fill,stroke,stroke-width,stroke-dasharray,border-radius,padding,min-width,min-height,opacity,shadow(none|soft) - Text:
color,font-family,font-size,font-weight,font-style,text-align - Edge:
stroke,stroke-width,stroke-dasharray,arrow,head,route - Group: everything a node has plus
label-position: top|bottom|above(above: the title sits outside, over the outline) - Custom properties
--name: valueandvar(--name)are supported, so slidr themes can share palettes.
6.1 Render targets and fonts¶
Labels are measured to size their boxes, but drawn later by whatever shows the SVG. If the viewer uses a different font from the one measured, labels overflow their boxes (wider font) or leave gaps (narrower font). A target names where the SVG will be shown, so the measurement matches:
host(default): fonts installed on the machine running predictygraph; a bundled copy if the font is not installed.linux,macos,windows: the first font in thefont-familylist that the platform ships; generic families (sans-serif, …) resolve the way that platform does.web: every font in the list is measured and boxes fit the widest, so labels fit whichever font a browser picks.
Set it with graph target=web or --target web (the option wins).
The default font is Roboto, Arial, "Helvetica Neue", sans-serif. Roboto is not
bundled: pages showing the SVG should load it as a web font. Without it, viewers fall
back to Arial/Helvetica. Platform-specific fonts go in @target blocks, which apply
only when the active target is listed:
graph { font-family: Roboto, Arial, "Helvetica Neue", sans-serif; }
@target linux { graph { font-family: "DejaVu Sans", sans-serif; } }
@target windows { graph { font-family: Arial, sans-serif; } }
predictygraph bundles width-only copies of DejaVu Sans, Liberation Sans/Serif/Mono and Carlito. Liberation and Carlito have the same character widths as Arial/Helvetica, Times New Roman, Courier New and Calibri, so those are measured exactly. Any other font with no bundled or installed copy is measured with DejaVu Sans, with a warning.
An SVG made for one target can overflow on another. For example, a diagram made with
--target windows (Arial) and opened on Linux (DejaVu Sans, 10–15% wider) has labels that
run past their boxes. Use web for files that are shared or published.
7. Animation¶
Animation is optional and never changes layout. The first frame (t=0) is always the complete diagram, so static renderers (rsvg, cairosvg, WeasyPrint, PDF) stay correct.
Edge flows — a moving dotted pattern along the edge:
flow=forward— dots travel source → targetflow=reverse— target → sourceflow=both— two interleaved streams, one each wayflow=auto— follow the arrowheads (->forward,<-reverse,<->both)flow-speed=1.2s— time for one dash period (default 1.2s;msalso accepted)flow=draw— the edge traces itself in from the source, holds, and redraws;flow-speedis the full cycle (default 3s). The t=0 frame shows the whole edge.flow-dot=ring— hollow dots (background-filled rings) instead of solid dots
grid=40 on the graph line (or graph { grid: 40; grid-color: #223; }) draws a faint
background grid with that pitch. It is decoration only and never affects layout.
corner-marks: 6px on a node draws short L ticks just outside its four corners (stroke colour).
Without an explicit dash a flow draws round dots over a faint guide line; with a dashed
body (..>, stroke-dasharray=) that dash pattern is what moves.
Animated borders on nodes and groups:
border=march— dashed outline that crawls around the shape ("marching ants")border=pulse— outline breathes in width and opacityborder=glow— a halo ring expands and fades behind the shapeborder-speed=1.6s— cycle time
All four keys work as attributes, in default node|edge|group, on the graph line
(flow-speed only) and as CSS properties, e.g. edge.hot { flow: forward; flow-speed: .6s; }.
SVG output uses SMIL <animate> (plays in browsers); the same tracks drive APNG frames.
Output modes (CLI):
predictygraph x.pdg— animated SVGpredictygraph x.pdg --static/--at 0.4— SVG frozen at t=0 / t=0.4spredictygraph x.pdg -o x.png— animated PNG (APNG), one seamless loop (LCM of all cycle times, capped at 12s);--fps 20 --scale 2 --seconds Ntune it. Needspredictygraph[apng].
8. Pipeline and output contract¶
Same shape as slidr: one intermediate representation, many output handlers.
.pdg text ─parse→ AST (model.py) ─style→ ─layout ⇄ route→ Scene IR (ir.py) ─→ render/svg.py
(iterated) └→ render/apng.py
- AST (
model.Document): groups, nodes, edges with attributes and resolved CSS. - Layout (
layout.py): per-group placement, inside-out. - Routing (
route.py): ports, lanes, bus levels, diagonal policy. The router reports rank gaps too narrow for the lanes passing through them; the layout widens exactly those gaps and runs again (so horizontal runs keep ≥12 px clear of boxes above and below). - Scene IR (
scene.py→ir.Scene): absolute geometry, paint, text lines and animation tracks. Group titles are placed here: a title slides along its band to a span no edge crosses; if none exists the group is widened and the pipeline reruns. Edge labels sit on the longest straight run of their edge. - Writers (
render/): consume only the Scene IR.svg(animated or frozen) andapng. New targets (PNG, PDF, ODP shapes, slidr-native) add a writer, nothing else.
The SVG writer emits a standalone SVG 1.1 file. Every element carries
class="pdg-node <classes>" and id; styling is written as presentation attributes,
and title clearance uses plain geometry (no masks), so the file looks the same in rsvg,
cairosvg, Inkscape, WeasyPrint, PowerPoint or a browser. Text width is measured with
the actual font file (no browser).
9. Example¶
graph dir=LR balance=median route=ortho edge-width=1.5
css "theme.css"
user "Data scientist" image=icons/user.svg
group k8s "Kubernetes cluster" layout=layered dir=TB {
api "kube-apiserver"
sched "HAMi scheduler" .hami
api -> sched
}
group gpus "GPU nodes" layout=grid cols=2 {
g0 "A100" ; g1 "A100" ; g2 "H100" ; g3 "H100"
}
user -> api "submit pod"
sched ==> gpus "bind" .hot
(; may separate statements on one line.)