|
| 1 | +"""Render the OSCAR doctrine into the docs site at build time. |
| 2 | +
|
| 3 | +The OSCAR doctrine lives in the OSCAR repo (added here as the ``oscar/`` |
| 4 | +submodule), so it has a single source of truth. This mkdocs-gen-files script |
| 5 | +copies the markdown from ``oscar/docs/`` into the virtual site tree under |
| 6 | +``oscar/`` on every build, rewriting repo-relative links (to templates, |
| 7 | +examples, and the like) so they resolve to the OSCAR repo on GitHub. |
| 8 | +
|
| 9 | +Nothing is duplicated in git: edit the docs in the OSCAR repo, bump the |
| 10 | +submodule here, and docs.osc.earth reflects the change on the next build. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import re |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import mkdocs_gen_files |
| 19 | + |
| 20 | +SRC = Path("oscar/docs") |
| 21 | +REPO = "https://github.com/OpenScience-Collective/oscar" |
| 22 | + |
| 23 | +# Links like ](../../templates/llms.txt) point at files that live in the OSCAR |
| 24 | +# repo, not the docs site. Rewrite any "up and out" link to a GitHub blob URL. |
| 25 | +_UP_AND_OUT = re.compile(r"\]\((?:\.\./)+([^)]+)\)") |
| 26 | + |
| 27 | + |
| 28 | +def rewrite_links(text: str) -> str: |
| 29 | + return _UP_AND_OUT.sub(lambda m: f"]({REPO}/blob/main/{m.group(1)})", text) |
| 30 | + |
| 31 | + |
| 32 | +if not SRC.is_dir(): |
| 33 | + raise SystemExit( |
| 34 | + f"OSCAR docs not found at {SRC}. Initialise the submodule with: " |
| 35 | + "git submodule update --init oscar" |
| 36 | + ) |
| 37 | + |
| 38 | +for src in sorted(SRC.rglob("*.md")): |
| 39 | + rel = src.relative_to(SRC) |
| 40 | + dest = rel.with_name("index.md") if rel.name == "README.md" else rel |
| 41 | + out = (Path("oscar") / dest).as_posix() |
| 42 | + with mkdocs_gen_files.open(out, "w") as fh: |
| 43 | + fh.write(rewrite_links(src.read_text(encoding="utf-8"))) |
| 44 | + mkdocs_gen_files.set_edit_path(out, f"{REPO}/edit/main/docs/{rel.as_posix()}") |
0 commit comments