File size: 11,062 Bytes
2857cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Build the documentation figures owned by the Kneiff repository."""

from __future__ import annotations

from collections.abc import Callable, Sequence
from dataclasses import dataclass
from html import escape
from pathlib import Path
import shutil
from tempfile import TemporaryDirectory

from graphviz import Digraph


REPOSITORY_ROOT = Path(__file__).resolve().parent.parent
ASSET_DIR = REPOSITORY_ROOT / "docs" / "assets"
FigureBuilder = Callable[[], Digraph]


# ===============================================================
# == Figure Theme
# ===============================================================


@dataclass(frozen=True)
class FigureTheme:
    """Colors and typography shared by repository documentation figures."""

    blue: str = "#00a2ff"
    green: str = "#32bc00"
    orange: str = "#f4a261"
    purple: str = "#8b5cf6"
    teal: str = "#00a6a6"
    surface: str = "#ffffff"
    text: str = "#000000"
    inverse_text: str = "#ffffff"
    font: str = "sans-serif"
    monospace_font: str = "monospace"


THEME = FigureTheme()


class DocumentationFigure:
    """Apply one local diagram contract to a Graphviz graph.

    The wrapper keeps colors, node layout, and edge styling inside this
    repository so documentation rendering does not depend on a sibling
    checkout.

    :param name: Stable Graphviz graph identifier.
    :param direction: Graphviz rank direction such as ``"TB"`` or ``"LR"``.
    """

    def __init__(self, name: str, direction: str) -> None:
        self.graph = Digraph(name=name)
        self.graph.attr(
            "graph",
            rankdir=direction,
            bgcolor="transparent",
            pad="0.04",
            nodesep="0.20",
            ranksep="0.36",
            fontname=THEME.font,
            fontcolor=THEME.text,
        )
        self.graph.attr("node", shape="plain", fontname=THEME.font)
        self.graph.attr(
            "edge",
            arrowsize="0.72",
            penwidth="2.5",
            fontname=THEME.font,
            fontsize="9",
            fontcolor=THEME.text,
        )

    def box(
        self,
        node_id: str,
        title: str,
        lines: Sequence[str],
        *,
        border_color: str,
        stereotype: str | None = None,
        monospace: bool = False,
        graph: Digraph | None = None,
    ) -> None:
        """Add one readable documentation node.

        :param node_id: Stable identifier used by edges.
        :param title: Short heading displayed in the colored header.
        :param lines: Detail rows shown under the heading.
        :param border_color: Semantic theme color for the node.
        :param stereotype: Optional classifier text displayed above the title.
        :param monospace: Whether detail rows represent literal file names.
        :param graph: Optional cluster graph receiving the node.
        :return: None.
        """

        rows: list[str] = []
        if stereotype is not None:
            rows.append(
                '<TR><TD BORDER="0" CELLPADDING="3">'
                f'<FONT POINT-SIZE="9">&lt;&lt;{escape(stereotype)}&gt;&gt;</FONT>'
                "</TD></TR>"
            )
        rows.append(
            f'<TR><TD BGCOLOR="{border_color}" BORDER="0" CELLPADDING="5">'
            f'<FONT COLOR="{THEME.inverse_text}"><B>{escape(title)}</B></FONT>'
            "</TD></TR>"
        )
        detail_font = THEME.monospace_font if monospace else THEME.font
        for line in lines:
            rows.append(
                '<TR><TD ALIGN="LEFT" BORDER="0" CELLPADDING="3">'
                f'<FONT FACE="{detail_font}" POINT-SIZE="10">{escape(line)}</FONT>'
                "</TD></TR>"
            )
        label = (
            f'<<TABLE BORDER="2" COLOR="{border_color}" BGCOLOR="{THEME.surface}" '
            'CELLBORDER="0" CELLSPACING="0" CELLPADDING="0">'
            f"{''.join(rows)}</TABLE>>"
        )
        (graph or self.graph).node(node_id, label=label)

    def edge(
        self,
        source: str,
        target: str,
        label: str | Sequence[str],
        *,
        color: str,
        constraint: bool = True,
    ) -> None:
        """Connect two nodes with one semantic color.

        :param source: Origin node identifier.
        :param target: Destination node identifier.
        :param label: One label or multiple stacked label lines.
        :param color: Stroke and arrow color.
        :param constraint: Whether the edge controls rank placement.
        :return: None.
        """

        lines = (label,) if isinstance(label, str) else label
        text = '<BR ALIGN="CENTER"/>'.join(escape(line) for line in lines)
        html_label = (
            f'<<TABLE BORDER="1" COLOR="{color}" BGCOLOR="{color}" '
            'CELLBORDER="0" CELLSPACING="0" CELLPADDING="3">'
            f'<TR><TD><FONT COLOR="{THEME.inverse_text}" POINT-SIZE="9">'
            f"<B>{text}</B></FONT></TD></TR></TABLE>>"
        )
        self.graph.edge(
            source,
            target,
            label=html_label,
            color=color,
            constraint=str(constraint).lower(),
        )


# ===============================================================
# == Figure Definitions
# ===============================================================


def build_kneiff_workflow_map() -> Digraph:
    """Show how AppRC selection defines one complete Kneiff project.

    :return: Diagram of the selector, conventional project paths, and data
        flow.
    """

    figure = DocumentationFigure("kneiff_workflow_map", direction="TB")
    figure.graph.attr(nodesep="0.04", ranksep="0.32")
    figure.box(
        "selector",
        "AppRC project selector",
        ("KNF_STORAGE", "--storage NAME_OR_PATH", "knf project use NAME"),
        stereotype="single source of selection",
        border_color=THEME.blue,
    )

    with figure.graph.subgraph(name="cluster_project") as project:
        project.attr(
            label="Selected AppRC project root\n<<fixed shallow layout>>",
            color=THEME.orange,
            bgcolor="#f4a26119",
            fontcolor=THEME.orange,
            penwidth="2",
            style="rounded,filled",
        )
        project.attr(rank="same")
        figure.box(
            "project_inputs",
            "Configuration and resources",
            (
                ".env.apprc-storage (machine-local)",
                "vocabulary.knf.yaml",
                "prompts.knf.yaml",
                "configs/*.knf.yaml",
                "workflows/ (optional overrides)",
            ),
            border_color=THEME.teal,
            monospace=True,
            graph=project,
        )
        figure.box(
            "editable_dataset",
            "Editable dataset",
            ("SOURCE/", "MANIFEST.knf.xlsx", "MANIFEST.yaml (generated)"),
            border_color=THEME.green,
            monospace=True,
            graph=project,
        )
        figure.box(
            "generated_artifacts",
            "Rebuildable artifacts",
            ("HF/<config-id>/", "TRAINING/", ".old_manifests/"),
            border_color=THEME.purple,
            monospace=True,
            graph=project,
        )

    figure.edge(
        "selector",
        "project_inputs",
        "resolves one root",
        color=THEME.teal,
    )
    figure.edge(
        "project_inputs",
        "editable_dataset",
        ("defines schema", "and operations"),
        color=THEME.green,
        constraint=False,
    )
    figure.edge(
        "editable_dataset",
        "generated_artifacts",
        ("dataset sync", "train prepare/run"),
        color=THEME.purple,
        constraint=False,
    )
    return figure.graph


def build_docs_reading_map() -> Digraph:
    """Show the shortest route from the root README into detailed docs.

    :return: Diagram of documentation entry points and their responsibilities.
    """

    figure = DocumentationFigure("kneifftools_docs_map", direction="TB")
    figure.graph.attr(nodesep="0.28", ranksep="0.36")
    figure.box(
        "root_readme",
        "Root README",
        ("install and first project", "quick workflows"),
        stereotype="start here",
        border_color=THEME.blue,
    )
    figure.box(
        "docs_index",
        "Docs index",
        ("reading map", "shared terminology"),
        stereotype="choose by task",
        border_color=THEME.orange,
    )
    figure.box(
        "how_to",
        "How-To User Guides",
        ("Commands and procedures",),
        border_color=THEME.green,
    )
    figure.box(
        "development",
        "Development",
        ("Maintainer workflow",),
        border_color=THEME.blue,
    )
    figure.box(
        "references",
        "References",
        ("Exact names and paths",),
        border_color=THEME.purple,
    )
    figure.box(
        "explanations",
        "Explanations",
        ("System model and rationale",),
        border_color=THEME.orange,
    )

    figure.edge("root_readme", "docs_index", "continue", color=THEME.orange)
    figure.edge("docs_index", "how_to", "do a task", color=THEME.green)
    figure.edge("docs_index", "development", "change code", color=THEME.blue)
    figure.edge(
        "docs_index",
        "references",
        "look up a name",
        color=THEME.purple,
    )
    figure.edge(
        "docs_index",
        "explanations",
        "understand why",
        color=THEME.orange,
    )
    return figure.graph


FIGURES: tuple[tuple[str, FigureBuilder], ...] = (
    ("kneiff-workflow-map", build_kneiff_workflow_map),
    ("docs-reading-map", build_docs_reading_map),
)


# ===============================================================
# == Rendering
# ===============================================================


def render_figures() -> tuple[Path, ...]:
    """Render every Kneiff figure into ``docs/assets``.

    Graphviz writes SVG files into a temporary directory. The final copy keeps
    only repository-owned assets and avoids partial output when rendering
    fails.

    :return: Paths of the generated SVG assets.
    """

    ASSET_DIR.mkdir(parents=True, exist_ok=True)
    generated: list[Path] = []
    with TemporaryDirectory(prefix="kneiff-doc-figures-") as temp_dir:
        temp_path = Path(temp_dir)
        for figure_name, build_figure in FIGURES:
            rendered_path = Path(
                build_figure().render(
                    filename=figure_name,
                    directory=temp_path,
                    format="svg",
                    cleanup=True,
                )
            )
            output_path = ASSET_DIR / f"{figure_name}.svg"
            shutil.copyfile(rendered_path, output_path)
            generated.append(output_path)
    return tuple(generated)


def main() -> int:
    """Regenerate the repository-owned documentation assets.

    :return: Process exit code.
    """

    for path in render_figures():
        print(path.relative_to(REPOSITORY_ROOT))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())