Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 4.5 KiB · executableRawPermalink
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0
3# Copyright (c) 2026 UtopiaIA
4"""Exports the site's wide "How it works" diagram for the README, in
5English, as standalone SVGs, dark and light.
6
7usage: diagram.py <site/index.html> <site/site.css> <out dir>
8
9The site's tall diagram, for narrow screens, stays on the site: for a
10reader who has chosen a theme, GitHub rewrites the colour-scheme media
11queries of a README's <picture>, and a source that also tests the screen's
12width then shows on wide screens too.
13
14The site's diagram tells the scene of crates/cli/tests/three_agents.rs, where
15c lands as a fast-forward, a is rebased with 1 path merged, and b conflicts.
16The README tells the scene of docs/assets/demo-agents.gif and of its quick
17start, where a lands as a fast-forward, b is rebased onto it without
18merging any path, and c conflicts. So the letters move round, a to b, b to
19c and c to a, each with its colour, and the rebase is only "rebased". Every
20other label is the site's.
21
22The colours are site.css's tokens for each theme, written into a <style>,
23and the ground is left transparent for GitHub's own page. No scripts and no
24external fonts: GitHub shows these files through <img>.
25"""
26import os
27import re
28import sys
29
30SITE, CSS, OUT = sys.argv[1:4]
31ROUND = {"a": "b", "b": "c", "c": "a"}
32WIDTH, HEIGHT = 1000, 408
33TITLE = ("How braid works: workspaces commit in parallel over one shared store, "
34 "land on main in turn, and git sync sends main to any git remote")
35
36
37def tokens(css):
38 """site.css's custom properties: the dark ones of :root, and the light
39 ones that its prefers-color-scheme block puts over them."""
40 dark_block = re.search(r"^:root \{(.*?)^\}", css, re.S | re.M).group(1)
41 light_block = re.search(r"@media \(prefers-color-scheme: light\) \{\s*:root \{(.*?)\}", css, re.S).group(1)
42
43 def props(block):
44 block = re.sub(r"/\*.*?\*/", "", block, flags=re.S)
45 return dict(re.findall(r"--([\w-]+):\s*([^;]+);", block))
46
47 dark = props(dark_block)
48 return {"dark": dark, "light": {**dark, **props(light_block)}}
49
50
51def rules(css, values, used):
52 """The diagram's rules of site.css, `.dgm .x { ... }`, for the classes
53 the SVG uses, with every var() resolved."""
54 out = []
55 for cls, body in re.findall(r"^\.dgm \.([\w-]+) \{([^}]*)\}", css, re.M):
56 # The loop back and its arrow belong to the agent that conflicts:
57 # b's teal on the site, c's indigo here.
58 if cls in ("loop", "ar-b"):
59 body = body.replace("var(--g-teal)", "var(--g-indigo)")
60 cls = "ar-c" if cls == "ar-b" else cls
61 if cls in used:
62 body = re.sub(r"var\(--([\w-]+)\)", lambda m: values[m[1]].strip(), body.strip())
63 out.append(f".{cls} {{ {body} }}")
64 return "\n".join(out)
65
66
67def english(svg):
68 svg = re.sub(r'<tspan data-l="es" lang="es"[^>]*>[^<]*</tspan>', "", svg)
69 return svg.replace(' data-l="en" lang="en"', "")
70
71
72def relabel(svg):
73 """The README's scene: the letters move round, colours and all."""
74 svg, n = re.subn(r"\b(acc|t|st|ar|dw-m|dt-m)-([abc])\b", lambda m: f"{m[1]}-{ROUND[m[2]]}", svg)
75 assert n >= 12, n
76 svg, n = re.subn(r"(>workspace(?: |</tspan><tspan [^>]*>))([abc])<",
77 lambda m: f"{m[1]}{ROUND[m[2]]}<", svg)
78 assert n == 3, n
79 svg, n = re.subn(r">rebased · 1 path merged<", ">rebased<", svg)
80 assert n == 1, n
81 return svg
82
83
84def main():
85 html = open(SITE, encoding="utf-8").read()
86 css = open(CSS, encoding="utf-8").read()
87 svg = re.search(r'<svg class="dgm dgm-wide".*?</svg>', html, re.S).group(0)
88 svg = relabel(english(svg))
89 svg, n = re.subn(r"^<svg [^>]*>", "", svg)
90 assert n == 1
91 used = set(re.findall(r'class="([^"]+)"', svg))
92 used = {c for group in used for c in group.split()}
93 for theme, values in tokens(css).items():
94 out = (
95 "<!-- SPDX-License-Identifier: Apache-2.0\n Copyright (c) 2026 UtopiaIA\n"
96 " Made by scripts/demos/diagram.py from the site's diagram: edit that, not this. -->\n"
97 f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {WIDTH} {HEIGHT}" '
98 f'width="{WIDTH}" height="{HEIGHT}" role="img" aria-labelledby="dgm-title">\n'
99 f'<title id="dgm-title">{TITLE}</title>\n'
100 f"<style>\n{rules(css, values, used)}\n</style>\n"
101 f"{svg}\n"
102 )
103 name = os.path.join(OUT, f"how-it-works-{theme}.svg")
104 with open(name, "w", encoding="utf-8") as f:
105 f.write(out)
106 print(name, len(out.encode()), "bytes")
107
108
109main()