|
| 1 | +"""Generate the documentation site from the repository's own files. |
| 2 | +
|
| 3 | +The site has no content of its own beyond the landing page. Every chapter is a |
| 4 | +copy of a file that already exists at the repository root, with its relative |
| 5 | +links rewritten for the book's flat layout. That is deliberate: a documentation |
| 6 | +site with its own copy of the specification is a second specification, and the |
| 7 | +two drift. |
| 8 | +
|
| 9 | + python docs-site/build.py # regenerate src/ |
| 10 | + mdbook build docs-site # render |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import re |
| 16 | +import shutil |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +HERE = Path(__file__).resolve().parent |
| 20 | +ROOT = HERE.parent |
| 21 | +SRC = HERE / "src" |
| 22 | +PAGES = HERE / "pages" |
| 23 | + |
| 24 | +# chapter file <- repository file |
| 25 | +CHAPTERS = { |
| 26 | + "specification.md": "CONFORMANCE.md", |
| 27 | + "portability.md": "docs/PORTABILITY.md", |
| 28 | + "results.md": "docs/RESULTS.md", |
| 29 | + "findings.md": "docs/FINDINGS.md", |
| 30 | + "claims.md": "docs/CLAIMS.md", |
| 31 | + "conformance-package.md": "packages/agentic-dataset-conformance/README.md", |
| 32 | + "authorized-recall.md": "packages/authorized-recall/README.md", |
| 33 | + "contributing.md": "CONTRIBUTING.md", |
| 34 | + "licensing.md": "LICENSE.md", |
| 35 | + "brand.md": "brand/README.md", |
| 36 | + "plan.md": "PLAN.md", |
| 37 | +} |
| 38 | + |
| 39 | +GH = "https://github.com/doytsujin/ok-agentic-dataset-reference/blob/main/" |
| 40 | +GH_TREE = "https://github.com/doytsujin/ok-agentic-dataset-reference/tree/main/" |
| 41 | + |
| 42 | +# Links that resolve to a chapter of this book. |
| 43 | +INTERNAL = { |
| 44 | + "CONFORMANCE.md": "specification.md", |
| 45 | + "docs/PORTABILITY.md": "portability.md", |
| 46 | + "PORTABILITY.md": "portability.md", |
| 47 | + "docs/RESULTS.md": "results.md", |
| 48 | + "RESULTS.md": "results.md", |
| 49 | + "docs/FINDINGS.md": "findings.md", |
| 50 | + "FINDINGS.md": "findings.md", |
| 51 | + "docs/CLAIMS.md": "claims.md", |
| 52 | + "CLAIMS.md": "claims.md", |
| 53 | + "CONTRIBUTING.md": "contributing.md", |
| 54 | + "LICENSE.md": "licensing.md", |
| 55 | + "../LICENSE.md": "licensing.md", |
| 56 | + "PLAN.md": "plan.md", |
| 57 | + "brand/README.md": "brand.md", |
| 58 | + "../packages/authorized-recall/README.md": "authorized-recall.md", |
| 59 | + "packages/authorized-recall/README.md": "authorized-recall.md", |
| 60 | +} |
| 61 | + |
| 62 | +LINK = re.compile(r"\]\((?!https?://|#)([^)]+)\)") |
| 63 | + |
| 64 | +# Absolute links back into this repository resolve to a chapter when there is |
| 65 | +# one, so the book does not bounce the reader out to GitHub for a page it |
| 66 | +# already contains. |
| 67 | +ABSOLUTE = re.compile( |
| 68 | + r"\]\(https://github\.com/doytsujin/ok-agentic-dataset-reference/" |
| 69 | + r"(?:blob|tree)/main/([^)#]+)(#[^)]*)?\)" |
| 70 | +) |
| 71 | + |
| 72 | + |
| 73 | +def rewrite(text: str) -> str: |
| 74 | + def sub(m: re.Match) -> str: |
| 75 | + target = m.group(1) |
| 76 | + anchor = "" |
| 77 | + if "#" in target: |
| 78 | + target, anchor = target.split("#", 1) |
| 79 | + anchor = "#" + anchor |
| 80 | + target = target.lstrip("./") |
| 81 | + if target in INTERNAL: |
| 82 | + return f"]({INTERNAL[target]}{anchor})" |
| 83 | + # Everything else points at the repository, so the link still works. |
| 84 | + base = GH_TREE if target.endswith("/") else GH |
| 85 | + return f"]({base}{target}{anchor})" |
| 86 | + |
| 87 | + def absolute(m: re.Match) -> str: |
| 88 | + target, anchor = m.group(1), m.group(2) or "" |
| 89 | + if target in INTERNAL: |
| 90 | + return f"]({INTERNAL[target]}{anchor})" |
| 91 | + return m.group(0) |
| 92 | + |
| 93 | + return LINK.sub(sub, ABSOLUTE.sub(absolute, text)) |
| 94 | + |
| 95 | + |
| 96 | +def main() -> None: |
| 97 | + SRC.mkdir(parents=True, exist_ok=True) |
| 98 | + |
| 99 | + # Hand-authored pages. The mark is inlined as SVG rather than linked as an |
| 100 | + # image so that `currentColor` applies: one asset that is legible on both |
| 101 | + # the light and the dark theme, instead of two PNGs and a media query. |
| 102 | + mark = (ROOT / "brand/agentic-dataset-mark.svg").read_text() |
| 103 | + # Strip the XML comment and flatten to one line before inlining. Its lines |
| 104 | + # are indented four spaces, which Markdown reads as a code block, so the |
| 105 | + # design note would render as visible text above the title. |
| 106 | + mark = re.sub(r"<!--.*?-->", "", mark, flags=re.S) |
| 107 | + mark = " ".join(mark.split()) |
| 108 | + mark = mark.replace('width="64" height="64"', 'width="96" height="96"') |
| 109 | + for page in sorted(PAGES.glob("*.md")): |
| 110 | + body = page.read_text().replace("<!-- MARK -->", mark) |
| 111 | + (SRC / page.name).write_text(rewrite(body)) |
| 112 | + print(f" {'pages/' + page.name:<52} -> src/{page.name}") |
| 113 | + |
| 114 | + for chapter, source in CHAPTERS.items(): |
| 115 | + body = (ROOT / source).read_text() |
| 116 | + # The mark is referenced relatively from README-style files. |
| 117 | + body = body.replace('src="agentic-dataset-mark-128.png"', |
| 118 | + 'src="mark.png"') |
| 119 | + (SRC / chapter).write_text(rewrite(body)) |
| 120 | + print(f" {source:<52} -> src/{chapter}") |
| 121 | + shutil.copy(ROOT / "brand/agentic-dataset-mark-256.png", SRC / "mark.png") |
| 122 | + print(f"\n{len(CHAPTERS)} chapters generated, " |
| 123 | + f"{len(list(PAGES.glob('*.md')))} hand-authored pages copied") |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments