ontoref-outreach/site/scripts/build/gen-diagram.nu
2026-07-17 01:01:30 +01:00

138 lines
6.1 KiB
Text

#!/usr/bin/env nu
# Project the ontology (core.ncl) into a standalone node-link GRAPH diagram SVG
# and save it as an image file in the site. This is the web-usable form of
# `onre diagram`: the 55 nodes laid out on a circle (grouped by level), with the
# 144 edges drawn as chords across it — a real graph, not a list. Served at
# /images/ontoref-diagram.svg, referenced by the About graph block.
#
# Rendered as an <img> (isolated from page CSS), so colours are baked literal
# (the ontoref dark palette) rather than CSS vars.
#
# Usage (from outreach/site):
# nu scripts/build/gen-diagram.nu --root ../.. --out site/public/images/ontoref-diagram.svg
const TAU = 6.283185307179586
const DEG = 57.29577951308232
# Level order around the ring + per-level colour and dot radius.
const LEVELS = [
[level color dot];
["Axiom" "#E8A838" 6.0]
["Tension" "#C0CCD8" 6.0]
["Practice" "#7fc8e0" 4.5]
]
def svg-esc [s: string] {
$s | str replace --all '&' '&amp;' | str replace --all '<' '&lt;' | str replace --all '>' '&gt;'
}
def clip [s: string, n: int] {
if (($s | str length) <= $n) { $s } else { (($s | str substring 0..($n - 1)) | str trim) + "…" }
}
def r1 [x] { $x | into float | math round --precision 1 }
def main [
--root: string = "../.."
--out: string = "site/public/images/ontoref-diagram.svg"
# Gate mode: reproduce from core.ncl and refuse if what is served differs. Exists because the
# `diagram` recipe was outside `sync-full` — the generator ran by hand, once, and nothing
# compared its output against the ontology afterwards. That is `generator-outside-the-chain`,
# the anti-pattern this justfile already names. Same shape as gen-architecture.nu --check.
--check
] {
let ip = $"($root)/code/ontology"
let core = $"($root)/.ontoref/ontology/core.ncl"
if not ($core | path exists) { error make { msg: $"core.ncl not found: ($core)" } }
let graph = (nickel export --format json --import-path $ip $core | from json)
let nodes = ($graph | get nodes)
let edges = ($graph.edges? | default [])
# Order nodes by level (contiguous arcs: axioms, then tensions, then practices).
let ordered = ($LEVELS | get level | each {|lvl|
$nodes | where {|n| ($n.level? | default "") == $lvl }
} | flatten)
let count = ($ordered | length)
let n_edges = ($edges | length)
let W = 920.0
let cx = ($W / 2)
let cy = (($W / 2) + 14) # nudge down, leave the title band clear
let R = 322.0 # node ring radius (labels radiate outside it)
# Position every node on the ring; keep id → {x,y,ang} for edge chords.
let placed = ($ordered | enumerate | each {|e|
let ang = ($TAU * $e.index / $count)
let lvl = ($e.item.level? | default "")
let spec = ($LEVELS | where level == $lvl | first)
{
id: ($e.item.id? | default ""),
name: ($e.item.name? | default ($e.item.id? | default "")),
level: $lvl,
color: $spec.color,
dot: $spec.dot,
ang: $ang,
x: ($cx + $R * ($ang | math cos)),
y: ($cy + $R * ($ang | math sin)),
}
})
let pmap = ($placed | reduce --fold {} {|it, acc| $acc | insert $it.id $it })
# Edge chords — only between two placed nodes (skip edges to ADR/project ids).
let edge_svg = ($edges | each {|ed|
let a = (try { $pmap | get ($ed.from? | default "") } catch { null })
let b = (try { $pmap | get ($ed.to? | default "") } catch { null })
if ($a == null) or ($b == null) { null } else {
$"<line x1=\"(r1 $a.x)\" y1=\"(r1 $a.y)\" x2=\"(r1 $b.x)\" y2=\"(r1 $b.y)\" stroke=\"#46607c\" stroke-width=\"0.9\" opacity=\"0.30\"/>"
}
} | compact | str join "\n ")
# Nodes — dot + radial label (rotated to read outward, flipped on the left half).
let node_svg = ($placed | each {|p|
let dot = $"<circle cx=\"(r1 $p.x)\" cy=\"(r1 $p.y)\" r=\"($p.dot)\" fill=\"($p.color)\" stroke=\"#0f1319\" stroke-width=\"1.4\"/>"
let deg = ($p.ang * $DEG)
let left = (($p.ang | math cos) < 0)
let lx = ($cx + ($R + 10) * ($p.ang | math cos))
let ly = ($cy + ($R + 10) * ($p.ang | math sin))
let rot = (if $left { $deg + 180 } else { $deg })
let anchor = (if $left { "end" } else { "start" })
let txt = (clip $p.name 26)
let label = $"<text x=\"(r1 $lx)\" y=\"(r1 $ly)\" fill=\"#aeb9c8\" font-size=\"9\" font-family=\"Inter, sans-serif\" text-anchor=\"($anchor)\" transform=\"rotate\((r1 $rot) (r1 $lx) (r1 $ly)\)\" dominant-baseline=\"middle\">(svg-esc $txt)</text>"
$"($dot)\n ($label)"
} | str join "\n ")
# Legend (level → colour + count).
let legend = ($LEVELS | get level | enumerate | each {|e|
let lvl = $e.item
let spec = ($LEVELS | where level == $lvl | first)
let c = ($placed | where level == $lvl | length)
let ly = (64 + $e.index * 20)
$"<circle cx=\"24\" cy=\"(r1 $ly)\" r=\"5\" fill=\"($spec.color)\"/><text x=\"36\" y=\"(r1 ($ly + 4))\" fill=\"#aeb9c8\" font-size=\"12\" font-family=\"Inter, sans-serif\">($lvl)s · ($c)</text>"
} | str join "\n ")
let H = ($W + 4)
let title = $"<text x=\"24\" y=\"34\" fill=\"#E8A838\" font-size=\"17\" font-weight=\"700\" font-family=\"ui-monospace, monospace\">ontoref</text><text x=\"108\" y=\"34\" fill=\"#8090A4\" font-size=\"13\" font-family=\"Inter, sans-serif\">ontology graph · ($count) nodes · ($n_edges) edges</text>"
let svg = $"<svg viewBox=\"0 0 ($W) ($H)\" width=\"($W)\" height=\"($H)\" xmlns=\"http://www.w3.org/2000/svg\" role=\"img\" aria-label=\"ontoref ontology graph — ($count) nodes, ($n_edges) edges\">
<rect x=\"0.5\" y=\"0.5\" width=\"(r1 ($W - 1))\" height=\"(r1 ($H - 1))\" rx=\"12\" fill=\"#0f1319\" stroke=\"#1e2a38\"/>
($title)
($legend)
($edge_svg)
($node_svg)
</svg>"
if $check {
if (not ($out | path exists)) or ((open --raw $out) != $svg) {
print $"(ansi red)DRIFT: ($out) does not reproduce from core.ncl \(run: just diagram\)(ansi reset)"
exit 1
}
print $"(ansi green)gen-diagram: up to date(ansi reset) · ($count) nodes · ($n_edges) edges"
return
}
let parent = ($out | path dirname)
if ($parent | is-not-empty) { mkdir $parent }
$svg | save -f $out
print $"gen-diagram: ($count) nodes · ($n_edges) edges → ($out)"
}