From 98e62992c2f83011ad3ba6bbad349322c170807f Mon Sep 17 00:00:00 2001 From: Jeremie Grodziski Date: Mon, 23 Nov 2026 10:43:15 +0000 Subject: [PATCH] docs(website): draw the traits diagram MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The trait vocabulary of METAMODEL.md §3 as one SVG, in the style of the Specy metamodel diagrams: the entity in the middle, the seven families around it, one box per trait with the attributes it contributes, a dashed box for a marker, red dots on the TNamed + TStructural + TInvocable composition. - website/scripts/traits-diagram.mjs generates website/assets/diagrams/traits-metamodel.svg (committed). Labels carry textLength measured from static/fonts/*.ttf, so the file still fits its boxes as an on GitHub, where no webfont loads. - The metamodel-diagram shortcode inlines it on the traits page: every box and family frame links to its section, with hover and focus states in the SVG's own stylesheet. assets/css/custom.css styles the card. - traits-diagram.test.mjs pins the drawing to schemas/ (the trait names and the entity record's keys), to its generator, and to the headings it links to. The website package's test script runs it, as does the website workflow. - METAMODEL.md §3 shows the same file as its image. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PKCVh1MUps9E7nFWjgQgpW --- .github/workflows/website.yml | 4 + METAMODEL.md | 8 + website/README.md | 36 ++ website/assets/css/custom.css | 46 ++ website/assets/diagrams/traits-metamodel.svg | 185 ++++++++ .../docs/reference/metamodel/traits.md | 4 + .../_shortcodes/metamodel-diagram.html | 14 + website/package.json | 2 +- website/scripts/traits-diagram.mjs | 421 ++++++++++++++++++ website/scripts/traits-diagram.test.mjs | 62 +++ 10 files changed, 781 insertions(+), 1 deletion(-) create mode 100644 website/assets/css/custom.css create mode 100644 website/assets/diagrams/traits-metamodel.svg create mode 100644 website/layouts/_shortcodes/metamodel-diagram.html create mode 100644 website/scripts/traits-diagram.mjs create mode 100644 website/scripts/traits-diagram.test.mjs diff --git a/.github/workflows/website.yml b/.github/workflows/website.yml index fb86c59..1027161 100644 --- a/.github/workflows/website.yml +++ b/.github/workflows/website.yml @@ -39,6 +39,10 @@ jobs: node-version: 22 cache: pnpm - run: pnpm install --frozen-lockfile + # the generated diagrams are committed: one that is stale against its + # generator, or links to a heading a page no longer has, fails here + # (ci.yml runs the same test when schemas/ — the vocabulary — changes) + - run: pnpm --filter @codegraph/website test # builds the viz bundle into static/city/ (the live city at /city/), then hugo - run: pnpm --filter @codegraph/website build - uses: actions/upload-artifact@v4 diff --git a/METAMODEL.md b/METAMODEL.md index dd5d08a..35a4b3e 100644 --- a/METAMODEL.md +++ b/METAMODEL.md @@ -193,6 +193,14 @@ Traits marked *(marker)* contribute **no attributes**: they declare a capability whose data lives in `edges[]` (edges are stored once, outgoing direction only — see §4). +![The nineteen traits around the entity that composes them, by family; a dashed box is a marker trait](website/assets/diagrams/traits-metamodel.svg) + +The whole vocabulary on one sheet: an entity composes at most one of each +trait, a dashed box is a marker, and the three red dots are the composition of +§3.7. Generated by `website/scripts/traits-diagram.mjs` and pinned to +`schemas/` by its test; on the documentation site the same file is a map, each +box a link to its section. + ### 3.1 Base | Trait | Attributes contributed | Notes | diff --git a/website/README.md b/website/README.md index 4600657..9e2214d 100644 --- a/website/README.md +++ b/website/README.md @@ -30,6 +30,9 @@ one deployment. | `layouts/_partials/landing/` | the landing's four partials, namespaced | | `assets/css/colors_and_type.css` | the design system's token layer, copied from the Defsquare Design System project | | `assets/css/landing.css` | layout only — every colour, size and space is a token | +| `assets/css/custom.css` | the documentation's additions to Hextra (the theme's own hook); the landing never loads it | +| `assets/diagrams/` | the generated metamodel diagrams, inlined by the `metamodel-diagram` shortcode | +| `layouts/_shortcodes/` | the documentation's own shortcodes | | `static/fonts/` | IBM Plex Sans Condensed, self-hosted. EB Garamond and Fira Code come from Google Fonts, as the design system specifies | | `static/city/` | the live cities: one laid-out `-city.json` per corpus (committed) beside the `@codegraph/viz` bundle `pnpm run viz` copies in (gitignored). `/city/?src=-city.json` opens one; the landing's corpora tabs embed them in iframes | | `static/img/` | the codegraph logo (four variants), the generated city, the Defsquare logo, the favicon | @@ -113,6 +116,39 @@ node scripts/city-svg.mjs city.json > static/img/gson-city.svg # counts on std Then update the caption and the `alt` text in `content/_index.md` with the counts the script prints. +## The metamodel diagrams are generated, and inlined + +`assets/diagrams/traits-metamodel.svg` is the trait vocabulary of +`METAMODEL.md` §3, drawn by `scripts/traits-diagram.mjs` in the style of the +Specy metamodel diagrams: the entity in the middle, the seven trait families +around it, one box per trait saying what it contributes, a dashed box for a +marker. To regenerate it after the vocabulary or a label changes: + +```bash +node scripts/traits-diagram.mjs > assets/diagrams/traits-metamodel.svg +pnpm --filter @codegraph/website test # node --test scripts/traits-diagram.test.mjs +``` + +The test is what keeps a committed drawing honest. It fails when the file is +not the script's output, when the boxes are not exactly the traits and trait +keys of `schemas/` (the committed contract `core` generates), and when a box +links to a heading its page no longer has. + +A page embeds it with `{{< metamodel-diagram "traits-metamodel" >}}`, which +**inlines** the SVG rather than pointing an `` at it: every box is a link +to its section, and the hover and focus states are rules in the SVG's own +stylesheet — neither exists inside an ``. The figure opts out of Hextra's +prose typography with `not-prose`, and its card is styled in +`assets/css/custom.css`, the hook Hextra concatenates after its own sheet. + +The same file is the image of `METAMODEL.md` on GitHub, where it *is* an +`` and no webfont loads. So it follows the logo's rule, above: every label +carries `textLength`, the width the script measured in the real face (it reads +the advance widths from `static/fonts/*.ttf` itself, no dependency), which is a +no-op where IBM Plex Sans Condensed loads and fits the fallback's glyphs to the +same boxes where it does not. A character the face has no glyph for stops the +script, as does a label wider than its box. + ## Links between the two halves Landing → documentation goes through `params.docsBase` in `hugo.yaml` diff --git a/website/assets/css/custom.css b/website/assets/css/custom.css new file mode 100644 index 0000000..8b837f5 --- /dev/null +++ b/website/assets/css/custom.css @@ -0,0 +1,46 @@ +/* The documentation's additions to Hextra, through the theme's own hook: Hextra + concatenates this file after its stylesheet. The landing page never loads it. */ + +/* A generated metamodel diagram, inlined by the metamodel-diagram shortcode. + The card stays light in the dark theme: the drawing carries its own ground, + as a printed figure does, and its inks are chosen against that ground. The + hover and focus states of the boxes are in the SVG's own stylesheet, so the + file behaves the same when it is opened alone. */ +.mm-figure { + margin: 1.5rem 0 2rem; + padding: 0.35rem; + border-radius: 12px; + background: #ebebeb; + box-shadow: + 0 0 0 1px rgb(0 0 0 / 0.06), + 0 10px 28px rgb(0 0 0 / 0.12); +} +/* Below the width at which its labels stop being readable the drawing keeps + its size and the card scrolls, rather than shrinking into a thumbnail. */ +.mm-figure__canvas { + overflow-x: auto; + border-radius: 9px; +} +.mm-figure .mm-diagram { + display: block; + width: 100%; + min-width: 38rem; + height: auto; +} +.mm-figure figcaption { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + gap: 0.25rem 1.5rem; + padding: 0.5rem 0.75rem 0.35rem; + font-size: 0.8rem; + color: #4a5967; +} +.mm-figure figcaption a { + color: inherit; + text-decoration: underline; + text-underline-offset: 2px; +} +.mm-figure figcaption a:hover { + color: #c2413f; +} diff --git a/website/assets/diagrams/traits-metamodel.svg b/website/assets/diagrams/traits-metamodel.svg new file mode 100644 index 0000000..6dae801 --- /dev/null +++ b/website/assets/diagrams/traits-metamodel.svg @@ -0,0 +1,185 @@ + +An entity composes traits. The 19 traits of the closed vocabulary, in 7 families, each with the attributes it contributes; a dashed box is a marker trait, which contributes none. + + + + + +Codegraph Metamodel +Traits + + + +BASE + + + +CONTAINMENT & ATTACHMENT + + + +STRUCTURE + + + +MODULARITY + + + +MEASURES + + + +TYPES + + + +BEHAVIOR + + + + + + + + + + + + + +0..3 +0..3 +0..3 +0..1 +0..1 +0..4 +0..4 +1 + + + + +Entity +{ id, kind, traits[], …trait keys } + + + +Language Profile +licenses, per kind, the traits an +entity must and may compose + + + +TNamed +name: string + + + + +TSourceAnchor +anchor: SourceAnchor + + + +TComment +comments: string[] + + + +TWithChildren +children: derived from parent + + + +TChildOf +parent: EntityId + + + +TAttachedTo +attachedTo: EntityId + + + +TStructural +value holder, target of accesses + + + + +TWithAccesses +→ access edges + + + +TWithValue +value: Literal + + + +TModule +definedIn: string[] +isStub: boolean + + + +TMetrics +metrics: Record<string, number> +measured, never defaulted + + + +TType +isStub: boolean + + + +TWithInheritances +→ inheritance edges + + + +TWithImplements +→ interfaceImplementation edges + + + +TTypedEntity +declaredType?: EntityId + + + +TInvocable +signature: string + + + + +TWithParameters +parameters: EntityId[] + + + +TWithLocalVariables +localVariables: EntityId[] + + + +TWithInvocations +→ invocation edges + + + + +contributes attributes to the entity + +marker: contributes no attribute + + +example, a Clojure fn-var: +TNamed + TStructural + TInvocable + + + + diff --git a/website/content/docs/reference/metamodel/traits.md b/website/content/docs/reference/metamodel/traits.md index b9d8c1d..b4c12ff 100644 --- a/website/content/docs/reference/metamodel/traits.md +++ b/website/content/docs/reference/metamodel/traits.md @@ -7,6 +7,10 @@ A trait is a named micro-capability: a partial schema contributing zero or more Traits marked *(marker)* contribute **no attributes**: they declare a capability whose data lives in edges, which are stored once, outgoing direction only. +{{< metamodel-diagram "traits-metamodel" >}} + +The diagram is a map of this page: an entity composes at most one of each trait, so zero to all of a family's. A dashed box is a marker, and the three red dots are [the composition that motivates the design](#the-composition-that-motivates-the-design). + ## Base | Trait | Attributes contributed | Notes | diff --git a/website/layouts/_shortcodes/metamodel-diagram.html b/website/layouts/_shortcodes/metamodel-diagram.html new file mode 100644 index 0000000..739ca5d --- /dev/null +++ b/website/layouts/_shortcodes/metamodel-diagram.html @@ -0,0 +1,14 @@ +{{- /* A generated metamodel diagram (assets/diagrams/.svg), INLINED: an would keep + the drawing and lose what makes it a map — its boxes are links, and its own stylesheet + gives them a hover. Usage: {{< metamodel-diagram "traits-metamodel" >}} + `not-prose` keeps Hextra's link and figure typography off the drawing. */ -}} +{{- $name := .Get 0 -}} +{{- $svg := resources.Get (printf "diagrams/%s.svg" $name) -}} +{{- if not $svg }}{{ errorf "metamodel-diagram: no asset diagrams/%s.svg (%s)" $name .Position }}{{ end -}} +
+
{{ $svg.Content | safeHTML }}
+
+ Select a box to jump to its section. + Open the diagram alone (SVG) +
+
diff --git a/website/package.json b/website/package.json index 887ae3b..e872449 100644 --- a/website/package.json +++ b/website/package.json @@ -9,7 +9,7 @@ "build": "pnpm run viz && if command -v hugo >/dev/null 2>&1; then hugo --minify --gc; else echo 'hugo not installed: website build skipped' >&2; fi", "dev": "pnpm run viz && hugo server -D --port 1314", "typecheck": "true", - "test": "true" + "test": "node --test scripts/traits-diagram.test.mjs" }, "devDependencies": { "@codegraph/viz": "workspace:*" diff --git a/website/scripts/traits-diagram.mjs b/website/scripts/traits-diagram.mjs new file mode 100644 index 0000000..caf1b2e --- /dev/null +++ b/website/scripts/traits-diagram.mjs @@ -0,0 +1,421 @@ +#!/usr/bin/env node +/** + * Draws the trait vocabulary of the metamodel (METAMODEL.md §3) as one SVG: + * the entity in the middle, the seven trait families around it, every trait a + * box that says what it contributes, every box a link to its section of the + * traits page. + * + * node scripts/traits-diagram.mjs > assets/diagrams/traits-metamodel.svg + * + * The file is committed, and `pnpm --filter @codegraph/website test` fails + * when it is not this script's output. It is read three ways, so it carries + * everything it needs: inlined in a page (the metamodel-diagram shortcode; + * links and hover live), opened on its own, and as an in METAMODEL.md + * on GitHub — where no webfont loads, which is why every label is pinned to + * its measured width with textLength, as the logo's wordmark is. + */ +import { readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; + +const site = join(dirname(fileURLToPath(import.meta.url)), ".."); + +// ----------------------------------------------------------------- content +const DOCS = "/docs/reference/metamodel/"; +const PAGE = `${DOCS}traits/`; + +/** + * The closed vocabulary, by family. A family's `id` is its section anchor on + * the traits page; `keys` are the attributes the trait contributes, and a + * trait with none is a marker. `example` tags the motivating composition. + */ +export const FAMILIES = [ + { + id: "base", + label: "Base", + cell: [0, 0], + traits: [ + { name: "TNamed", sub: "name: string", keys: ["name"], example: true }, + { name: "TSourceAnchor", sub: "anchor: SourceAnchor", keys: ["anchor"] }, + { name: "TComment", sub: "comments: string[]", keys: ["comments"] }, + ], + }, + { + id: "containment-and-attachment", + label: "Containment & attachment", + cell: [1, 0], + traits: [ + { name: "TWithChildren", sub: "children: derived from parent", keys: [] }, + { name: "TChildOf", sub: "parent: EntityId", keys: ["parent"] }, + { name: "TAttachedTo", sub: "attachedTo: EntityId", keys: ["attachedTo"] }, + ], + }, + { + id: "structure", + label: "Structure", + cell: [2, 0], + traits: [ + { name: "TStructural", sub: "value holder, target of accesses", keys: [], example: true }, + { name: "TWithAccesses", sub: "→ access edges", keys: [] }, + { name: "TWithValue", sub: "value: Literal", keys: ["value"] }, + ], + }, + { + id: "modularity", + label: "Modularity", + cell: [0, 1], + traits: [{ name: "TModule", sub: ["definedIn: string[]", "isStub: boolean"], keys: ["definedIn", "isStub"] }], + }, + { + id: "measures", + label: "Measures", + cell: [2, 1], + traits: [{ name: "TMetrics", sub: ["metrics: Record", "measured, never defaulted"], keys: ["metrics"] }], + }, + { + id: "types", + label: "Types", + cell: [0, 2], + traits: [ + { name: "TType", sub: "isStub: boolean", keys: ["isStub"] }, + { name: "TWithInheritances", sub: "→ inheritance edges", keys: [] }, + { name: "TWithImplements", sub: "→ interfaceImplementation edges", keys: [] }, + { name: "TTypedEntity", sub: "declaredType?: EntityId", keys: ["declaredType"] }, + ], + }, + { + id: "behavior", + label: "Behavior", + cell: [2, 2], + traits: [ + { name: "TInvocable", sub: "signature: string", keys: ["signature"], example: true }, + { name: "TWithParameters", sub: "parameters: EntityId[]", keys: ["parameters"] }, + { name: "TWithLocalVariables", sub: "localVariables: EntityId[]", keys: ["localVariables"] }, + { name: "TWithInvocations", sub: "→ invocation edges", keys: [] }, + ], + }, +]; + +const ENTITY = { + label: "Entity", + sub: "{ id, kind, traits[], …trait keys }", + href: `${DOCS}#entity`, +}; +const PROFILE = { + label: "Language Profile", + sub: "licenses, per kind, the traits an entity must and may compose", + href: `${DOCS}profiles/#validation`, +}; +const LEGEND = [ + { swatch: "solid", lines: ["contributes attributes to the entity"] }, + { swatch: "marker", lines: ["marker: contributes no attribute"] }, + { + swatch: "example", + lines: ["example, a Clojure fn-var:", "TNamed + TStructural + TInvocable"], + href: `${PAGE}#the-composition-that-motivates-the-design`, + }, +]; + +// ------------------------------------------------------------------- style +// The Specy metamodel diagrams' palette and type, restated: an reads no +// CSS variable, and the two sites' figures are meant to look like one family. +const BG = "#ebebeb"; +const FRAME = "#dedede"; +const BOX = "#fbfbfb"; +const INK = "#1b2935"; +const RED = "#e46765"; +const HOVER = "#e46863"; +const SANS = `"IBM Plex Sans Condensed","Arial Narrow","Liberation Sans Narrow",Arial,Helvetica,sans-serif`; +const SERIF = `"EB Garamond",Garamond,"Times New Roman",Times,serif`; +const SIZE = { hub: 45.1, node: 28.65, sub: 21.5, card: 28.65, group: 22, title: 63.1 }; +const TRACKING = 1.6; // letter-spacing of a family's label + +const W = 1320; +const MARGIN = 46; +const COL = 384; // a family frame; three of them and two gutters fill the width +const GUTTER = 38; +const PAD = 22; // frame to box +const HEAD = 56; // a frame's label band +const BOX_W = COL - 2 * PAD; +const BOX_GAP = 16; +const ROW_GAP = 84; // room for a connector and its cardinality +const TOP = 250; // below the title card +const HUB_W = 320; +const TEXT_PAD = 20; + +// ----------------------------------------------------------------- metrics +/** Advance widths straight from a TrueType file: `cmap` (format 4) and `hmtx`. */ +function loadFont(file) { + const b = readFileSync(join(site, "static", "fonts", file)); + const table = {}; + for (let i = 0, n = b.readUInt16BE(4); i < n; i++) { + const record = 12 + i * 16; + table[b.toString("latin1", record, record + 4)] = b.readUInt32BE(record + 8); + } + const unitsPerEm = b.readUInt16BE(table.head + 18); + const longMetrics = b.readUInt16BE(table.hhea + 34); + let sub = 0; + for (let i = 0, n = b.readUInt16BE(table.cmap + 2); i < n; i++) { + const record = table.cmap + 4 + i * 8; + if (b.readUInt16BE(record) === 3 && b.readUInt16BE(record + 2) === 1) sub = table.cmap + b.readUInt32BE(record + 4); + } + if (sub === 0 || b.readUInt16BE(sub) !== 4) throw new Error(`${file}: no format-4 Unicode BMP cmap`); + const segments = b.readUInt16BE(sub + 6) / 2; + const ends = sub + 14; + const starts = ends + 2 * segments + 2; + const deltas = starts + 2 * segments; + const offsets = deltas + 2 * segments; + const glyph = (code) => { + for (let i = 0; i < segments; i++) { + if (code > b.readUInt16BE(ends + 2 * i)) continue; + const start = b.readUInt16BE(starts + 2 * i); + if (code < start) return 0; + const offset = b.readUInt16BE(offsets + 2 * i); + if (offset === 0) return (code + b.readUInt16BE(deltas + 2 * i)) & 0xffff; + const g = b.readUInt16BE(offsets + 2 * i + offset + 2 * (code - start)); + return g === 0 ? 0 : (g + b.readUInt16BE(deltas + 2 * i)) & 0xffff; + } + return 0; + }; + return (text, size) => { + let units = 0; + for (const ch of text) { + const g = glyph(ch.codePointAt(0)); + // A missing glyph would be drawn from another font, at a width nobody measured. + if (g === 0) throw new Error(`${file} has no glyph for "${ch}" (in "${text}")`); + units += b.readUInt16BE(table.hmtx + 4 * Math.min(g, longMetrics - 1)); + } + return (units / unitsPerEm) * size; + }; +} + +const widthOf = { + b: loadFont("IBMPlexSansCondensed-Bold.ttf"), + r: loadFont("IBMPlexSansCondensed-Regular.ttf"), +}; + +// ------------------------------------------------------------------ pieces +const f = (n) => String(Math.round(n * 10) / 10); +const esc = (s) => String(s).replace(/[<>&"]/g, (c) => ({ "<": "<", ">": ">", "&": "&", '"': """ })[c]); + +/** One line of text, pinned to its measured width; `x` is its left, centre or right edge. */ +function text(cls, size, x, y, string, { anchor = "middle", tracking = 0 } = {}) { + const width = widthOf[cls](string, size) + tracking * string.length; + const left = anchor === "start" ? x : anchor === "end" ? x - width : x - width / 2; + const spaced = tracking ? ` letter-spacing="${tracking}"` : ""; + return `${esc(string)}`; +} + +function wrap(string, size, max) { + const lines = []; + let line = ""; + for (const word of string.split(" ")) { + const candidate = line ? `${line} ${word}` : word; + if (line && widthOf.r(candidate, size) > max) { + lines.push(line); + line = word; + } else line = candidate; + } + return [...lines, line]; +} + +const roundedPath = (x, y, w, h, r = 7.5) => + `M${f(x + r)},${f(y)} Q${f(x)},${f(y)} ${f(x)},${f(y + r)} L${f(x)},${f(y + h - r)} Q${f(x)},${f(y + h)} ${f(x + r)},${f(y + h)} ` + + `L${f(x + w - r)},${f(y + h)} Q${f(x + w)},${f(y + h)} ${f(x + w)},${f(y + h - r)} L${f(x + w)},${f(y + r)} Q${f(x + w)},${f(y)} ${f(x + w - r)},${f(y)} Z`; + +/** + * A box: a label over its subtitle lines, on the vertical rhythm of the Specy + * diagrams (label baseline at +44, +28 a subtitle line; a hub's at +61, +33). + */ +function box({ label, sub = [], w, hub = false, marker = false, example = false, tabStop = true, href, aria }) { + const subs = (Array.isArray(sub) ? sub : [sub]).flatMap((s) => wrap(s, SIZE.sub, w - 2 * TEXT_PAD)); + const size = hub ? SIZE.hub : SIZE.node; + if (widthOf.b(label, size) > w - TEXT_PAD) throw new Error(`"${label}" does not fit a box ${w} wide`); + const labelY = hub ? 61 : 44 - 4 * (subs.length - 1); + const subY = labelY + (hub ? 33 : 28); + const h = subY + 28 * (subs.length - 1) + (hub ? 14 : 18); + const draw = (x, y) => { + const cx = x + w / 2; + const parts = [ + ``, + ``, + text("b", size, cx, y + labelY, label), + ...subs.map((s, i) => text("r", SIZE.sub, cx, y + subY + 28 * i, s)), + ]; + if (example) parts.push(``); + parts.push(""); + return parts.join("\n"); + }; + return { w, h, draw }; +} + +const port = (r, side, t = 0.5) => + side === "left" ? [r.x, r.y + r.h * t] : side === "right" ? [r.x + r.w, r.y + r.h * t] : side === "top" ? [r.x + r.w * t, r.y] : [r.x + r.w * t, r.y + r.h]; +const OUT = { left: [-1, 0], right: [1, 0], top: [0, -1], bottom: [0, 1] }; + +/** A connector: one cubic, each handle half the gap along its port's axis, so it never overshoots. */ +function connector(from, fromSide, fromT, to, toSide, toT) { + const p0 = port(from, fromSide, fromT); + const p3 = port(to, toSide, toT); + const handle = (p, q, side) => { + const along = (q[0] - p[0]) * OUT[side][0] + (q[1] - p[1]) * OUT[side][1]; + return along <= 0 ? 30 : Math.max(along / 2, Math.min(30, along)); + }; + const k0 = handle(p0, p3, fromSide); + const k3 = handle(p3, p0, toSide); + const p1 = [p0[0] + OUT[fromSide][0] * k0, p0[1] + OUT[fromSide][1] * k0]; + const p2 = [p3[0] + OUT[toSide][0] * k3, p3[1] + OUT[toSide][1] * k3]; + return ``; +} + +/** A cardinality beside the port it qualifies, on the side the connector leaves free. */ +function cardinality(string, [px, py], side, lean) { + const dx = lean === "left" ? -10 : 10; + const anchor = lean === "left" ? "end" : "start"; + const y = side === "bottom" ? py + 32 : side === "top" ? py - 11 : py - 9; + return text("r", SIZE.card, px + dx, y, string, { anchor }); +} + +function logo(x, y) { + const source = readFileSync(join(site, "static", "img", "codegraph-logo-long-dark-blue-text-red-brackets.svg"), "utf8"); + // The wordmark's ids would become ids of whatever page inlines the diagram. + const inner = /]*>([\s\S]*)<\/svg>/.exec(source)[1].replace(/[\s\S]*?<\/title>/, "").replace(/\sid="[^"]*"/g, ""); + return `<a href="/" class="mm-logo" aria-label="Codegraph"><g transform="translate(${f(x)},${f(y)})">${inner.replace(/\s*\n\s*/g, "").trim()}</g></a>`; +} + +// ------------------------------------------------------------------- render +export function render() { + const colX = (c) => MARGIN + c * (COL + GUTTER); + + const families = FAMILIES.map((family) => ({ + ...family, + href: `${PAGE}#${family.id}`, + boxes: family.traits.map((t) => + box({ + label: t.name, + sub: t.sub, + w: BOX_W, + marker: t.keys.length === 0, + example: t.example, + // Its family's frame already leads to the same section: one tab stop a family, not one a trait. + tabStop: false, + href: `${PAGE}#${family.id}`, + aria: `${t.name} (${family.label})`, + }), + ), + })); + for (const family of families) { + family.content = family.boxes.reduce((sum, b) => sum + b.h, 0) + BOX_GAP * (family.boxes.length - 1); + } + // A row is as tall as its tallest family; every frame of the row takes that height. + const rowH = [0, 1, 2].map((r) => HEAD + PAD + Math.max(...families.filter((fam) => fam.cell[1] === r).map((fam) => fam.content))); + const rowY = [TOP, TOP + rowH[0] + ROW_GAP, TOP + rowH[0] + rowH[1] + 2 * ROW_GAP]; + const H = rowY[2] + rowH[2] + MARGIN + 10; + for (const family of families) { + family.rect = { x: colX(family.cell[0]), y: rowY[family.cell[1]], w: COL, h: rowH[family.cell[1]] }; + } + const frame = Object.fromEntries(families.map((family) => [family.id, family.rect])); + + const hub = box({ ...ENTITY, w: HUB_W, hub: true }); + const hubRect = { x: colX(1) + (COL - HUB_W) / 2, y: rowY[1] + (rowH[1] - hub.h) / 2, w: hub.w, h: hub.h }; + const profile = box({ ...PROFILE, w: BOX_W }); + const profileRect = { x: colX(1) + PAD, y: rowY[2], w: profile.w, h: profile.h }; + + const groups = families.map((family) => { + const { x, y, w, h } = family.rect; + const label = family.label.toUpperCase(); + if (widthOf.b(label, SIZE.group) + TRACKING * label.length > w - 2 * PAD) throw new Error(`"${label}" does not fit its frame`); + return [ + `<a href="${esc(family.href)}" class="mm-group" aria-label="${esc(`${family.label}: ${family.traits.length} trait${family.traits.length > 1 ? "s" : ""}`)}">`, + `<rect x="${f(x)}" y="${f(y)}" width="${f(w)}" height="${f(h)}" rx="14" fill="${FRAME}" stroke="${INK}" stroke-width="2"/>`, + text("b", SIZE.group, x + PAD, y + 37, label, { anchor: "start", tracking: TRACKING }), + "</a>", + ].join("\n"); + }); + + // The entity composes at most one of each trait: zero to all of a family's. + const upTo = (id) => `0..${families.find((family) => family.id === id).traits.length}`; + const links = [ + ["containment-and-attachment", "top", 0.5, "bottom", 0.5, "right"], + ["base", "top", 0.14, "bottom", 0.8, "left"], + ["structure", "top", 0.86, "bottom", 0.2, "right"], + ["modularity", "left", 0.5, "right", 0.5, "right"], + ["measures", "right", 0.5, "left", 0.5, "left"], + ["types", "bottom", 0.14, "top", 0.8, "left"], + ["behavior", "bottom", 0.86, "top", 0.2, "right"], + ]; + const edges = links.map(([id, side, t, toSide, toT]) => connector(hubRect, side, t, frame[id], toSide, toT)); + const cards = links.map(([id, , , toSide, toT, lean]) => cardinality(upTo(id), port(frame[id], toSide, toT), toSide, lean)); + edges.push(connector(hubRect, "bottom", 0.5, profileRect, "top", 0.5)); + cards.push(cardinality("1", port(profileRect, "top"), "top", "right")); + + const nodes = [hub.draw(hubRect.x, hubRect.y), profile.draw(profileRect.x, profileRect.y)]; + for (const family of families) { + let y = family.rect.y + HEAD; + for (const b of family.boxes) { + nodes.push(b.draw(family.rect.x + PAD, y)); + y += b.h + BOX_GAP; + } + } + + // The middle column's last cell: what the three looks mean, then the wordmark. + const legend = []; + const legendW = 58 + Math.max(...LEGEND.flatMap((entry) => entry.lines.map((line) => widthOf.r(line, SIZE.sub)))); + if (legendW > COL) throw new Error("the legend is wider than its column"); + let y = profileRect.y + profileRect.h + 52; + for (const entry of LEGEND) { + const x = colX(1) + (COL - legendW) / 2; + const swatch = + entry.swatch === "example" + ? `<circle cx="${f(x + 22)}" cy="${f(y + 14)}" r="7" fill="${RED}"/>` + : `<path fill="${BOX}" stroke="${INK}" stroke-width="2"${entry.swatch === "marker" ? ' stroke-dasharray="9 6"' : ""} d="${roundedPath(x, y, 44, 28, 6)}"/>`; + const lines = entry.lines.map((line, i) => text("r", SIZE.sub, x + 58, y + 21 + 27 * i, line, { anchor: "start" })); + const row = [swatch, ...lines].join("\n"); + legend.push(entry.href ? `<a href="${esc(entry.href)}" class="mm-link" aria-label="${esc(entry.lines.join(" "))}">\n${row}\n</a>` : row); + y += 27 * entry.lines.length + 21; + } + const mark = logo(colX(1) + (COL - 242) / 2, rowY[2] + rowH[2] - 49); + if (y > rowY[2] + rowH[2] - 49 - 20) throw new Error("the legend runs into the logo"); + + const css = [ + // First, as @import must be: the title's face. The page's own webfonts stay out of reach of an <img>. + `@import url("https://fonts.googleapis.com/css2?family=EB+Garamond:wght@700&display=swap");`, + `@font-face{font-family:"IBM Plex Sans Condensed";font-style:normal;font-weight:400;font-display:swap;src:url("/fonts/IBMPlexSansCondensed-Regular.ttf") format("truetype")}`, + `@font-face{font-family:"IBM Plex Sans Condensed";font-style:normal;font-weight:700;font-display:swap;src:url("/fonts/IBMPlexSansCondensed-Bold.ttf") format("truetype")}`, + `.mm-b{font-family:${SANS};font-weight:700;fill:${INK}}`, + `.mm-r{font-family:${SANS};font-weight:400;fill:${INK}}`, + `.mm-t{font-family:${SERIF};font-weight:700;fill:#1c2631}`, + `.mm-c{fill:none;stroke:${RED};stroke-linecap:round;stroke-linejoin:round}`, + `.mm-diagram a{text-decoration:none;cursor:pointer;outline:none}`, + `.mm-node path,.mm-group rect,.mm-diagram a text{transition:stroke .15s,fill .15s}`, + `.mm-node:hover path,.mm-node:focus-visible path{stroke:${HOVER};stroke-width:3;fill:#fff}`, + `.mm-group:hover rect,.mm-group:focus-visible rect{stroke:${HOVER};stroke-width:3}`, + `.mm-node:hover text,.mm-node:focus-visible text,.mm-group:hover text,.mm-group:focus-visible text,.mm-link:hover text,.mm-link:focus-visible text{fill:${HOVER}}`, + `.mm-logo:focus-visible{outline:2px solid ${HOVER};outline-offset:4px}`, + ].join(""); + + const titleW = 800; + const traitCount = FAMILIES.reduce((n, family) => n + family.traits.length, 0); + return [ + `<svg class="mm-diagram" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${f(H)}" role="group" aria-label="The traits of the Codegraph metamodel">`, + `<desc>An entity composes traits. The ${traitCount} traits of the closed vocabulary, in ${FAMILIES.length} families, each with the attributes it contributes; a dashed box is a marker trait, which contributes none.</desc>`, + // Escaped: the file is XML when it is an <img>, and the font URL holds an ampersand. + `<defs><style>${esc(css)}</style>`, + `<filter id="mm-shadow" x="-5%" y="-15%" width="112%" height="132%"><feDropShadow dx="6" dy="6" stdDeviation="3.5" flood-color="#000" flood-opacity="0.4"/></filter></defs>`, + `<rect width="${W}" height="${f(H)}" fill="${BG}"/>`, + `<rect x="11.5" y="10.5" width="${f(W - 23)}" height="${f(H - 21)}" fill="none" stroke="#1e2d3b" stroke-width="3" stroke-dasharray="11 8.634"/>`, + `<rect x="${f((W - titleW) / 2)}" y="35.5" width="${titleW}" height="159" rx="27" fill="#fff" stroke="#1c2631" stroke-width="3" filter="url(#mm-shadow)"/>`, + `<text class="mm-t" font-size="${SIZE.title}" text-anchor="middle" x="${W / 2}" y="101">Codegraph Metamodel</text>`, + `<text class="mm-t" font-size="${SIZE.title}" text-anchor="middle" x="${W / 2}" y="170">Traits</text>`, + `<g class="mm-groups">\n${groups.join("\n")}\n</g>`, + `<g class="mm-edges">\n${edges.join("\n")}\n</g>`, + `<g class="mm-cards">\n${cards.join("\n")}\n</g>`, + `<g class="mm-nodes">\n${nodes.join("\n")}\n</g>`, + `<g class="mm-legend">\n${legend.join("\n")}\n</g>`, + mark, + "</svg>", + "", + ].join("\n"); +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) process.stdout.write(render()); diff --git a/website/scripts/traits-diagram.test.mjs b/website/scripts/traits-diagram.test.mjs new file mode 100644 index 0000000..8d78675 --- /dev/null +++ b/website/scripts/traits-diagram.test.mjs @@ -0,0 +1,62 @@ +// The traits diagram is a committed, generated file that restates the metamodel +// and points into the documentation. Each test pins one way it could go stale: +// the vocabulary (against schemas/, the committed contract core generates), the +// file (against its generator), and its links (against the pages' headings). +// node --test scripts/traits-diagram.test.mjs (pnpm --filter @codegraph/website test) +import assert from "node:assert/strict"; +import { existsSync, readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { test } from "node:test"; +import { fileURLToPath } from "node:url"; +import { FAMILIES, render } from "./traits-diagram.mjs"; + +const site = join(dirname(fileURLToPath(import.meta.url)), ".."); +const schema = (name) => JSON.parse(readFileSync(join(site, "..", "schemas", `${name}.record.schema.json`), "utf8")); +const traits = FAMILIES.flatMap((family) => family.traits); + +test("the diagram names every trait of the closed vocabulary, once", () => { + const vocabulary = schema("header").properties.dict.properties.traits.items.enum; + const drawn = traits.map((t) => t.name); + assert.deepEqual([...drawn].sort(), [...vocabulary].sort()); +}); + +test("the attributes it shows are the entity record's trait keys, all of them", () => { + // What an `e` record carries of its own: tag, surrogate, kind, traits, the key (m, s, d), space. + const own = new Set(["t", "i", "k", "tr", "m", "s", "d", "space"]); + const onTheWire = Object.keys(schema("e").properties).filter((key) => !own.has(key)); + const drawn = new Set(traits.flatMap((t) => t.keys)); + assert.deepEqual([...drawn].sort(), onTheWire.sort()); + for (const t of traits) { + for (const key of t.keys) assert.ok([t.sub].flat().join(" ").includes(key), `${t.name} claims ${key} and does not show it`); + } +}); + +test("the committed SVG is the generator's output", () => { + const committed = readFileSync(join(site, "assets", "diagrams", "traits-metamodel.svg"), "utf8"); + assert.equal(committed, render(), "stale: node scripts/traits-diagram.mjs > assets/diagrams/traits-metamodel.svg"); +}); + +test("the SVG survives being an <img>: no bare ampersand, no label left unpinned", () => { + const svg = render(); + assert.doesNotMatch(svg, /&(?!amp;|lt;|gt;|quot;)/); + // The title alone is unpinned: it is centred in a card far wider than it. (The logo's + // wordmark holds no text of its own: its tspan carries the pin.) + const unpinned = [...svg.matchAll(/<text\b(?![^>]*\btextLength=)[^>]*>([^<]+)</g)].map((m) => m[1]); + assert.deepEqual(unpinned, ["Codegraph Metamodel", "Traits"]); +}); + +test("every box links to a heading that exists", () => { + // Hugo's default heading ids (GitHub's rule): lowercase, punctuation dropped, a hyphen per space. + const anchor = (heading) => heading.toLowerCase().replace(/[^\p{L}\p{N}\s_-]/gu, "").trim().replace(/\s/g, "-"); + const hrefs = [...render().matchAll(/<a href="([^"]+)"/g)].map((m) => m[1]); + assert.equal(hrefs.length, 1 + 1 + 1 + FAMILIES.length + traits.length + 1, "entity, profile, example, families, traits, logo"); + for (const href of new Set(hrefs)) { + if (href === "/") continue; + const [path, fragment] = href.split("#"); + const page = join(site, "content", path); + const file = [join(page, "_index.md"), `${page.replace(/\/$/, "")}.md`].find((candidate) => existsSync(candidate)); + assert.ok(file, `${href}: no page`); + const headings = readFileSync(file, "utf8").split("\n").filter((line) => /^#{1,6} /.test(line)).map((line) => anchor(line.replace(/^#+ /, ""))); + assert.ok(headings.includes(fragment), `${href}: no such heading in ${file}`); + } +});