#!/usr/bin/env bash # Publish the documentation in docs/ to the GitHub wiki. # # The pages in docs/ are the source of truth; the wiki is a generated copy for # people who look there first. Each run checks every link in docs/, clones the # wiki, replaces all of its pages with the ones listed in SECTIONS below, # rewrites links between them to wiki page names and every other repository # link to an absolute GitHub URL, copies the images they show, writes the # sidebar and footer, and commits. Nothing is pushed without --push. # # --check only checks the links and that every page in docs/ is listed or # excluded, without touching the wiki. CI runs it for every change to docs/, # and every push to main that passes it publishes. # # GitHub creates the wiki repository only when the first page is saved, so # create any page once in the web UI before the first run. WIKI_REMOTE # overrides the remote, which is how the workflow pushes with its token. # # usage: scripts/publish-wiki.sh [--check | --push] [WORKDIR] set -euo pipefail repo_url="https://github.com/narrrl/proton-stream" wiki_remote="${WIKI_REMOTE:-git@github.com:narrrl/proton-stream.wiki.git}" mode=commit case "${1:-}" in --check) mode=check; shift ;; --push) mode=push; shift ;; esac root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" workdir="${1:-$(mktemp -d)}" wiki="$workdir/wiki" if [ "$mode" = check ]; then wiki="$workdir/check" mkdir -p "$wiki" elif [ -d "$wiki/.git" ]; then git -C "$wiki" pull --ff-only --quiet else git clone --quiet "$wiki_remote" "$wiki" fi rev="$(git -C "$root" rev-parse --short HEAD)" version="$(grep -m1 '^version' "$root/Cargo.toml" | cut -d'"' -f2)" python3 - "$root" "$wiki" "$repo_url" "$version" "$rev" <<'EOF' import pathlib import re import shutil import sys root, wiki, repo_url, version, rev = sys.argv[1:] root = pathlib.Path(root) wiki = pathlib.Path(wiki) docs = root / "docs" HOME = ("README.md", "Home") # Sidebar sections: docs/ file -> wiki page name, in order. SECTIONS = [ ("Start here", [ ("GETTING_STARTED.md", "Getting-Started"), ("INSTALL.md", "Installation"), ("FAQ.md", "FAQ"), ]), ("Using it", [ ("USER_GUIDE.md", "User-Guide"), ("ACCOUNT_AND_SYNC.md", "Account-and-Sync"), ("MOBILE.md", "Android-and-iOS"), ]), ("Reference", [ ("CLI.md", "Command-Line-Reference"), ("PRIVACY.md", "Privacy"), ]), ("When something goes wrong", [ ("TROUBLESHOOTING.md", "Troubleshooting"), ]), ("Contributing", [ ("DEVELOPMENT.md", "Development"), ("ARCHITECTURE.md", "Architecture"), ("TESTING.md", "Testing"), ("METADATA.md", "Metadata"), ("DESIGN-TOKENS.md", "Design-Tokens"), ("ANDROID.md", "Android-Development"), ("IOS.md", "iOS-Development"), ]), ] # Too long or too internal for the wiki; links to them go to GitHub. EXCLUDED = {"BUGS.md"} # Files outside docs/ whose links are checked but not published. CHECKED = ["README.md"] PAGES = dict([HOME] + [page for _, pages in SECTIONS for page in pages]) images_url = f"https://raw.githubusercontent.com/wiki/{repo_url.removeprefix('https://github.com/')}" LINK = re.compile(r"(\]\(|src=\")([^)\"\s]+)([)\"])") FENCE = re.compile(r"^\s*(```|~~~)") INLINE_CODE = re.compile(r"(`[^`\n]*`)") HEADING = re.compile(r"^(#{1,6})\s+(.*?)\s*#*\s*$") EXPLICIT_ID = re.compile(r"\n\n" ) for source, page in PAGES.items(): (wiki / f"{page}.md").write_text(note + without_title(converted[source])) for image in published_images: (wiki / image).parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(root / image, wiki / image) sidebar = ["**[Home](Home)**\n"] for section, pages in SECTIONS: sidebar.append(f"\n**{section}**\n\n") sidebar += [f"- [{title(source)}]({page})\n" for source, page in pages] sidebar += [ "\n---\n\n", f"[Releases]({repo_url}/releases) ยท [Issues]({repo_url}/issues)\n", ] (wiki / "_Sidebar.md").write_text(note + "".join(sidebar)) (wiki / "_Footer.md").write_text( note + f"Documentation of proton-stream {version}, generated from " + f"[`docs/`]({repo_url}/tree/{rev}/docs) at `{rev}`. " + f"To change a page, edit it in the [repository]({repo_url}).\n" ) EOF if [ "$mode" = check ]; then echo "Documentation links are fine." exit 0 fi cd "$wiki" git add -A if git diff --cached --quiet; then echo "Wiki is already up to date." exit 0 fi git commit --quiet -m "Update from docs at $rev" git show --stat --format='%s' HEAD if [ "$mode" = push ]; then git push --quiet echo "Pushed to the wiki." else echo "Committed in $wiki. Review it, then push from there or re-run with --push." fi