Skip to content

Commit c521a73

Browse files
Merge pull request #34 from OpenScience-Collective/feature/oscar-examples-docs
Render OSCAR worked examples on docs.osc.earth
2 parents 3b80752 + eec79d1 commit c521a73

3 files changed

Lines changed: 60 additions & 19 deletions

File tree

mkdocs.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,10 @@ nav:
203203
- Research lab sites: oscar/archetypes/lab-website.md
204204
- Standards & specs: oscar/archetypes/standard.md
205205
- Checklist: oscar/checklist.md
206-
- Templates & examples: https://github.com/OpenScience-Collective/oscar
206+
- Examples:
207+
- oscar/examples/index.md
208+
- NEMAR: oscar/examples/nemar/index.md
209+
- HED: oscar/examples/hed/index.md
210+
- EEGLAB: oscar/examples/eeglab/index.md
211+
- HEDit: oscar/examples/hedit/index.md
212+
- Templates: https://github.com/OpenScience-Collective/oscar/tree/main/templates

scripts/gen_oscar.py

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
"""Render the OSCAR doctrine into the docs site at build time.
1+
"""Render the OSCAR doctrine and worked examples into the docs site at build time.
22
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.
3+
The OSCAR content lives in the OSCAR repo (added here as the ``oscar/`` submodule),
4+
a single source of truth. This mkdocs-gen-files script copies the doctrine
5+
(``oscar/docs``) and the worked-example write-ups (``oscar/examples/*/README.md``)
6+
into the virtual site tree under ``oscar/`` on every build, rewriting links so they
7+
resolve either to the rendered doctrine pages or to the OSCAR repo on GitHub.
88
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.
9+
Nothing is duplicated in git: edit the docs in the OSCAR repo, bump the submodule
10+
here, and docs.osc.earth reflects the change on the next build.
1111
"""
1212

1313
from __future__ import annotations
@@ -17,28 +17,63 @@
1717

1818
import mkdocs_gen_files
1919

20-
SRC = Path("oscar/docs")
2120
REPO = "https://github.com/OpenScience-Collective/oscar"
21+
DOCS_SRC = Path("oscar/docs")
22+
EXAMPLES_SRC = Path("oscar/examples")
2223

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.
24+
# Doctrine: repo-relative "up and out" links (templates, and the like) -> GitHub blob.
2525
_UP_AND_OUT = re.compile(r"\]\((?:\.\./)+([^)]+)\)")
26+
# Example: after/ links (files or directories) -> GitHub.
27+
_AFTER = re.compile(r"\]\((after/[^)]*)\)")
28+
# Examples index: sibling directory links like ](nemar/) -> the rendered page.
29+
_EX_DIR = re.compile(r"\]\(([a-z][a-z0-9-]*)/\)")
2630

2731

28-
def rewrite_links(text: str) -> str:
32+
def rewrite_doctrine(text: str) -> str:
2933
return _UP_AND_OUT.sub(lambda m: f"]({REPO}/blob/main/{m.group(1)})", text)
3034

3135

32-
if not SRC.is_dir():
36+
def rewrite_example(text: str, name: str) -> str:
37+
# Doctrine cross-links (../../docs/X) stay on the site as ../../X.
38+
text = text.replace("](../../docs/", "](../../")
39+
40+
# after/ links point at files that are not rendered here; send them to GitHub.
41+
def _after(m: re.Match) -> str:
42+
target = m.group(1)
43+
kind = "tree" if target.endswith("/") else "blob"
44+
return f"]({REPO}/{kind}/main/examples/{name}/{target})"
45+
46+
return _AFTER.sub(_after, text)
47+
48+
49+
if not DOCS_SRC.is_dir():
3350
raise SystemExit(
34-
f"OSCAR docs not found at {SRC}. Initialise the submodule with: "
51+
f"OSCAR docs not found at {DOCS_SRC}. Initialise the submodule with: "
3552
"git submodule update --init oscar"
3653
)
3754

38-
for src in sorted(SRC.rglob("*.md")):
39-
rel = src.relative_to(SRC)
55+
# Doctrine pages.
56+
for src in sorted(DOCS_SRC.rglob("*.md")):
57+
rel = src.relative_to(DOCS_SRC)
4058
dest = rel.with_name("index.md") if rel.name == "README.md" else rel
4159
out = (Path("oscar") / dest).as_posix()
4260
with mkdocs_gen_files.open(out, "w") as fh:
43-
fh.write(rewrite_links(src.read_text(encoding="utf-8")))
61+
fh.write(rewrite_doctrine(src.read_text(encoding="utf-8")))
4462
mkdocs_gen_files.set_edit_path(out, f"{REPO}/edit/main/docs/{rel.as_posix()}")
63+
64+
# Worked-example write-ups: the examples index, then one page per example.
65+
if EXAMPLES_SRC.is_dir():
66+
index = EXAMPLES_SRC / "README.md"
67+
if index.is_file():
68+
out = "oscar/examples/index.md"
69+
text = _EX_DIR.sub(lambda m: f"]({m.group(1)}/index.md)", index.read_text(encoding="utf-8"))
70+
with mkdocs_gen_files.open(out, "w") as fh:
71+
fh.write(text)
72+
mkdocs_gen_files.set_edit_path(out, f"{REPO}/edit/main/examples/README.md")
73+
74+
for readme in sorted(EXAMPLES_SRC.glob("*/README.md")):
75+
name = readme.parent.name
76+
out = f"oscar/examples/{name}/index.md"
77+
with mkdocs_gen_files.open(out, "w") as fh:
78+
fh.write(rewrite_example(readme.read_text(encoding="utf-8"), name))
79+
mkdocs_gen_files.set_edit_path(out, f"{REPO}/edit/main/examples/{name}/README.md")

0 commit comments

Comments
 (0)