diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..58ce1e7 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,38 @@ +name: Documentation +on: + push: + branches: + - main + pull_request: +permissions: + contents: read +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-python@v6 + with: + python-version: 3.x + - run: pip install zensical==0.1.5 + - run: zensical build --clean --strict + env: + PYTHONPATH: code/docs + - uses: actions/upload-pages-artifact@v5 + if: github.event_name == 'push' + with: + path: site + deploy: + if: github.event_name == 'push' + needs: build + permissions: + pages: write + id-token: write + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + runs-on: ubuntu-latest + steps: + - uses: actions/configure-pages@v6 + - uses: actions/deploy-pages@v5 + id: deployment diff --git a/code/docs/README.md b/code/docs/README.md index 87d5874..6b8f6f4 100644 --- a/code/docs/README.md +++ b/code/docs/README.md @@ -16,14 +16,17 @@ The histology figures read the JPEG 2000 sections and need `$PY_TIRL`; the other ## The documentation site -`mkdocs.yml`, at the top of the repository, builds the pages of `docs/` into a site as they are: a home page, the overview, the tutorials, the how-to pages, the brain identifiers, the FAQ and troubleshooting, with a search box. +[Zensical](https://zensical.org) reads `mkdocs.yml`, at the top of the repository, and builds the pages of `docs/` into a site as they are: a home page, the overview, the tutorials, the how-to pages, the brain identifiers, the FAQ and troubleshooting, with a search box. ```text -pip install mkdocs-material in any Python environment; the pipeline's environments do not need it -mkdocs build --strict writes site/; stops at a broken link or a link to a heading that does not exist -mkdocs serve shows the site on this machine while you edit +pip install zensical==0.1.5 in any Python 3.11 or newer environment; the pipeline's environments do not need it +PYTHONPATH=code/docs zensical build --strict writes site/ +PYTHONPATH=code/docs zensical serve shows the site on this machine while you edit ``` +Each push to `main` builds the site and publishes it on GitHub Pages; each pull request builds it without publishing (`.github/workflows/docs.yml`). + The pages keep their relative links, so they read the same on GitHub. -`site_links.py` sends the links that leave `docs/`, to the code, the settings or the tests, to the same file on GitHub. +`site_links.py` sends the links that leave `docs/`, to the code, the settings or the tests, to the same file on GitHub; `site_links_extension.py` applies it to each page as Zensical builds it. +Zensical checks links before `site_links.py` rewrites them, so its link check is off in `mkdocs.yml`; `tests/test_docs_site.py` checks that every relative link leads to a file. A new page goes into the `nav` list of `mkdocs.yml`; `tests/test_docs_site.py` says so if it is missing. diff --git a/code/docs/site_links.py b/code/docs/site_links.py index 5e66c4a..0619ae6 100644 --- a/code/docs/site_links.py +++ b/code/docs/site_links.py @@ -2,7 +2,7 @@ tests, goes to that file in the repository on GitHub. You need - nothing: MkDocs loads this file itself (mkdocs.yml, `hooks`). + nothing: site_links_extension.py applies it to every page. What it does The Markdown files keep their relative links, which GitHub follows as they are. A site built from docs/ holds only docs/, so on each page a link whose target is outside docs/ is rewritten to https://github.com/lincbrain/fiber_template/blob/main/. @@ -26,9 +26,9 @@ def outside(page_path, url): return None if target == "docs" or target.startswith("docs/") else target + sep + anchor -def on_page_markdown(markdown, page, **kwargs): +def rewrite(markdown, page_path): def fix(m): - target = outside(page.file.src_uri, m.group(2)) + target = outside(page_path, m.group(2)) return m.group(0) if target is None else f"[{m.group(1)}]({REPO}{target})" return LINK.sub(fix, markdown) diff --git a/code/docs/site_links_extension.py b/code/docs/site_links_extension.py new file mode 100644 index 0000000..63dcd85 --- /dev/null +++ b/code/docs/site_links_extension.py @@ -0,0 +1,23 @@ +"""The Markdown extension through which Zensical applies site_links.py to every page (mkdocs.yml, `markdown_extensions`). +Zensical imports it by name, so code/docs must be on PYTHONPATH. +""" +from markdown import Extension +from markdown.preprocessors import Preprocessor +from zensical.extensions.context import ContextPreprocessor + +from site_links import rewrite + + +class SiteLinks(Preprocessor): + def run(self, lines): + page = ContextPreprocessor.from_markdown(self.md).page + return rewrite("\n".join(lines), page.path).split("\n") + + +class SiteLinksExtension(Extension): + def extendMarkdown(self, md): + md.preprocessors.register(SiteLinks(md), "site_links", 40) + + +def makeExtension(**kwargs): + return SiteLinksExtension(**kwargs) diff --git a/mkdocs.yml b/mkdocs.yml index d58aa54..812f1a3 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,6 +1,7 @@ # The documentation site, built from docs/ as it is. How to build it: code/docs/README.md site_name: fiber_template site_description: Code and documentation for a multimodal macaque template that aligns ex vivo MRI and microscopy +site_url: https://lincbrain.github.io/fiber_template/ repo_url: https://github.com/lincbrain/fiber_template repo_name: lincbrain/fiber_template edit_uri: "" @@ -12,11 +13,12 @@ theme: - navigation.sections - navigation.top - content.code.copy -hooks: - - code/docs/site_links.py # a link that leaves docs/ goes to that file on GitHub +markdown_extensions: + - site_links_extension # a link that leaves docs/ goes to that file on GitHub validation: links: - anchors: warn # with --strict, a link to a heading that does not exist stops the build + not_found: ignore + anchors: ignore nav: - Home: README.md - Overview: overview.md diff --git a/tests/test_docs_site.py b/tests/test_docs_site.py index f45b22d..0098e5f 100644 --- a/tests/test_docs_site.py +++ b/tests/test_docs_site.py @@ -1,4 +1,4 @@ -"""Tests of the documentation site's settings (mkdocs.yml) against the pages of docs/ (CPU, no data, MkDocs not needed). +"""Tests of the documentation site's settings (mkdocs.yml) against the pages of docs/ (CPU, no data, Zensical not needed). python -m pytest tests/test_docs_site.py or python tests/run.py tests/test_docs_site.py @@ -35,7 +35,7 @@ def menu(nav): return out -def hook(): +def site_links(): spec = importlib.util.spec_from_file_location("site_links", REPO / "code" / "docs" / "site_links.py") mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) @@ -44,7 +44,8 @@ def hook(): def test_the_menu_names_every_page(): cfg = site() - assert cfg["docs_dir"] == "docs" and all((REPO / h).is_file() for h in cfg["hooks"]) + assert cfg["docs_dir"] == "docs" and "site_links_extension" in cfg["markdown_extensions"] + assert (REPO / "code" / "docs" / "site_links_extension.py").is_file() pages = menu(cfg["nav"]) assert pages and all((DOCS / p).is_file() for p in pages), [p for p in pages if not (DOCS / p).is_file()] notes = {p.relative_to(DOCS).as_posix() for p in DOCS.rglob("README.md")} - {"README.md"} # a subfolder's own note @@ -53,7 +54,7 @@ def test_the_menu_names_every_page(): def test_every_relative_link_leads_to_a_file(): - outside, broken, sent = hook().outside, [], 0 + outside, broken, sent = site_links().outside, [], 0 for page in sorted(DOCS.rglob("*.md")): rel = page.relative_to(DOCS).as_posix() text = re.sub(r"```.*?```", "", page.read_text(encoding="utf-8"), flags=re.S)