#!/usr/bin/env python3
"""Render the wiki's accessible SVG diagrams using only Python's standard library."""
import html
import json
import re
import textwrap
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RAW = "https://raw.githubusercontent.com/wiki/ModelCloud/GPTQModel/assets/diagrams"
COLORS = [
("#1d4ed8", "#eff6ff", "#bfdbfe"),
("#0f766e", "#f0fdfa", "#99f6e4"),
("#7e22ce", "#faf5ff", "#e9d5ff"),
("#9a3412", "#fff7ed", "#fed7aa"),
]
def wrapped(text, width):
return textwrap.wrap(text, width=width, break_long_words=True, break_on_hyphens=False)
def text(x, y, lines, size=18, color="#334155", weight="400", leading=25):
content = "".join(
f'{html.escape(line)}' for i, line in enumerate(lines)
)
return f'{content}'
def render(diagram):
title = wrapped(diagram["title"], 53)
subtitle = wrapped(diagram["subtitle"], 90)
header = 58 + len(title) * 36 + len(subtitle) * 26 + 28
note = wrapped(diagram["note"], 92)
lane_heights = []
for lane in diagram["lanes"]:
count = len(lane["steps"])
assert 2 <= count <= 4
width = (920 - (count - 1) * 32) / count
max_title = max(len(wrapped(step["title"], int((width - 34) / 13))) for step in lane["steps"])
max_detail = max(len(wrapped(step["detail"], int((width - 34) / 11))) for step in lane["steps"])
lane_heights.append(34 + max_title * 26 + max_detail * 25 + 60)
height = header + sum(lane_heights) + len(note) * 25 + 68
description = (
diagram["subtitle"]
+ " "
+ " ".join(
lane["label"] + ": " + " Then ".join(step["title"] + ". " + step["detail"] for step in lane["steps"])
for lane in diagram["lanes"]
)
+ " "
+ diagram["note"]
)
parts = [
'',
f'\n")
return "\n".join(parts)
def markdown(diagram):
slug = diagram["page"]
asset = slug.lower() + ".svg"
lines = [
"",
"## Workflow at a glance",
"",
f"![{diagram['title']}. {diagram['subtitle']}]({RAW}/{asset})",
"",
f"**Reading the diagram:** {diagram['note']}",
"",
"",
"Text version of the workflow
",
"",
]
for lane in diagram["lanes"]:
steps = " → ".join(step["title"] for step in lane["steps"])
lines.append(f"- **{lane['label']}:** {steps}.")
lines += ["", " ", "", f"[Open full-size SVG]({RAW}/{asset})", ""]
return "\n".join(lines) + "\n"
def main():
diagrams = json.loads((ROOT / ".tools/diagrams.json").read_text())
assert len({d["page"] for d in diagrams}) == len(diagrams)
for diagram in diagrams:
assert re.fullmatch(r"[A-Za-z0-9-]+", diagram["page"])
svg = ROOT / "assets/diagrams" / (diagram["page"].lower() + ".svg")
svg.write_text(render(diagram))
page = ROOT / (diagram["page"] + ".md")
content = page.read_text()
block = markdown(diagram)
if "" in content:
content = re.sub(
r".*?\n?", lambda _: block, content, flags=re.S
)
else:
# Keep the opening explanation before the visual; insert before the next section.
matches = list(re.finditer(r"^## ", content, re.M))
index = 1 if matches and content[matches[0].start() :].startswith("## Overview\n") else 0
offset = matches[index].start() if len(matches) > index else len(content)
content = content[:offset].rstrip() + "\n\n" + block + "\n" + content[offset:]
page.write_text(content)
print(f"Rendered and embedded {len(diagrams)} workflow diagrams.")
if __name__ == "__main__":
main()