diff --git a/.github/workflows/site.yml b/.github/workflows/site.yml index 64e7ba4..50e6922 100644 --- a/.github/workflows/site.yml +++ b/.github/workflows/site.yml @@ -64,9 +64,9 @@ jobs: with: go-version: ${{ inputs.go-version }} - run: pip install -r .site/brand/pages/requirements.txt - - run: .site/brand/tools/site.sh . prepare - if: inputs.prepare != '' run: ${{ inputs.prepare }} + - run: .site/brand/tools/site.sh . prepare - run: zensical build --strict - uses: actions/upload-pages-artifact@v5 with: diff --git a/docs/sites.md b/docs/sites.md index 87e0657..c6abd72 100644 --- a/docs/sites.md +++ b/docs/sites.md @@ -12,6 +12,7 @@ Material for MkDocs by the same team, and they all share what lives here: | `pages/theme/` | The theme's overrides: the suite's bar across the top, the Rainbow Book accent, the mark as logo and favicon | | `pages/requirements.txt` | The pinned Zensical version | | `tools/site.sh` | Puts the above into a repository and builds or previews its site | +| `pages/site_docs.py` | The site's copy of `docs/` for `docs_dir: .site/docs` | | `.github/workflows/site.yml` | The reusable workflow that builds every site and deploys it to Pages | A change here reaches every site on its next build. @@ -63,11 +64,17 @@ The workflow's inputs: | Input | Default | What it does | | --- | --- | --- | -| `prepare` | none | Shell commands run before the build, in the repository's root: the generators that write the reference pages | +| `prepare` | none | Shell commands run first, in the repository's root: the generators that write or check the reference pages | | `go-version` | none | A Go version to install first, for generators written in Go | | `submodules` | `false` | Passed to `actions/checkout` | | `brand-ref` | `main` | The brand branch or tag whose theme to use | +A site whose pages are `docs/` plus the README can set `docs_dir: .site/docs`: +`tools/site.sh` then builds from a copy of `docs/` (`pages/site_docs.py`) +whose home page is the README and whose links that leave `docs/` (to +`../LICENSE`, to source files) go to the files on GitHub, so the same +Markdown reads right on GitHub and on the site. + `.site/` and `site/` go in the repository's `.gitignore`: the first is the theme copied in for the build, the second the built site. diff --git a/pages/site_docs.py b/pages/site_docs.py new file mode 100644 index 0000000..e8b037e --- /dev/null +++ b/pages/site_docs.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +# The site's copy of a repository's docs/, for sites whose mkdocs.yml says +# docs_dir: .site/docs (tools/site.sh runs this when it does): +# +# - docs/ is copied to .site/docs/, so nothing in docs/ changes; +# - the home page, .site/docs/index.md, is the repository's README.md +# (unless docs/ has an index.md of its own); +# - a link that leaves docs/ (../LICENSE, ../src/x.go, a README link to +# a file beside it) goes to the file on GitHub instead, as repo_url +# in mkdocs.yml names it. +# +# python3 site_docs.py REPO_DIR +import os +import re +import shutil +import sys + +LINK_RE = re.compile(r"(\]\()([^)\s]+)(\))") + + +def repo_url(repo): + with open(os.path.join(repo, "mkdocs.yml"), encoding="utf-8") as f: + m = re.search(r"^repo_url:\s*(\S+)", f.read(), re.M) + if not m: + sys.exit("site_docs: mkdocs.yml has no repo_url") + return m.group(1).rstrip("/") + + +def site_link(target, page_dir, blob): + """A link of a page in docs/ as the site has it.""" + if re.match(r"[a-z][a-z0-9+.-]*:|#", target): + return target + path, _, frag = target.partition("#") + if not path: + return target + whole = os.path.normpath(os.path.join("docs", page_dir, path)).replace(os.sep, "/") + if whole == "docs" or whole.startswith("docs/"): + return target + if whole == "README.md": + new = os.path.relpath("docs/index.md", os.path.join("docs", page_dir)) + else: + new = blob + whole + return new + ("#" + frag if frag else "") + + +def rewrite(text, page_dir, blob): + return LINK_RE.sub(lambda m: m.group(1) + site_link(m.group(2), page_dir, blob) + m.group(3), text) + + +def main(): + repo = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else ".") + blob = repo_url(repo) + "/blob/main/" + src = os.path.join(repo, "docs") + dst = os.path.join(repo, ".site", "docs") + shutil.rmtree(dst, ignore_errors=True) + if os.path.isdir(src): + shutil.copytree(src, dst) + else: + os.makedirs(dst) + for d, _, files in os.walk(dst): + page_dir = os.path.relpath(d, dst) + page_dir = "" if page_dir == "." else page_dir + for f in files: + if f.endswith(".md"): + path = os.path.join(d, f) + with open(path, encoding="utf-8") as fh: + text = fh.read() + with open(path, "w", encoding="utf-8") as fh: + fh.write(rewrite(text, page_dir, blob)) + index = os.path.join(dst, "index.md") + readme = os.path.join(repo, "README.md") + if not os.path.exists(index) and os.path.exists(readme): + with open(readme, encoding="utf-8") as fh: + text = fh.read() + + # the README's links are from the repository's root + def home_link(m): + t = m.group(2) + if re.match(r"[a-z][a-z0-9+.-]*:|#", t): + return m.group(0) + path, _, frag = t.partition("#") + rel = os.path.relpath(path, "docs") if path else path + return m.group(1) + site_link(rel + ("#" + frag if frag else ""), "", blob) + m.group(3) + + with open(index, "w", encoding="utf-8") as fh: + fh.write(LINK_RE.sub(home_link, text)) + + +if __name__ == "__main__": + main() diff --git a/tools/site.sh b/tools/site.sh index 50bf7b2..2a242d1 100755 --- a/tools/site.sh +++ b/tools/site.sh @@ -18,6 +18,11 @@ mkdir -p .site cp -R "$brand/pages/theme" .site/theme cp "$brand/pages/mkdocs-base.yml" .site/mkdocs-base.yml cp "$brand/avatar/mark.svg" .site/theme/assets/suite/mark.svg +# a site built from a copy of docs/ (docs_dir: .site/docs): the copy, its +# home page the README and its links out of docs/ to GitHub +if grep -q '^docs_dir: *\.site/docs' mkdocs.yml 2>/dev/null; then + python3 "$brand/pages/site_docs.py" . +fi case $what in prepare) ;; build) zensical build --strict ;;