Repository navigation
Expand file tree
/
Copy pathmkdocs_hooks.py
More file actions
315 lines (292 loc) · 16.2 KB
/
Copy pathmkdocs_hooks.py
File metadata and controls
315 lines (292 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# MkDocs build-time hooks.
#
# The narrative guide lives at docs/core/*.md. Many guide pages cross-link
# to the normative spec under spec/*.md using the path ../../spec/ — that
# is correct when the guide is browsed via the GitHub source tree (where
# spec/ lives at the repo root, two levels up from docs/core/).
#
# At MkDocs build time the spec tree is staged under docs/spec/ (by
# `on_pre_build` below). From a guide page the correct relative path to
# the staged spec is therefore ../spec/, not ../../spec/. Without
# rewriting, every guide -> spec link 404s on the published site and
# emits a WARNING during `mkdocs build`.
#
# Rather than rewrite the source files (which would break GitHub
# source-tree browsing for anyone reading the guide on github.com), we
# rewrite the markdown at build time. Source on disk is correct for
# both contexts.
#
# Rewrite cases:
#
# 1. guide -> spec ../../spec/ -> ../spec/
# (spec is staged into docs/spec/, so it IS in the
# site; the relative-path depth changes)
#
# 2. cross-tree refs to trees that are NOT staged into the site:
# - examples/ (CLJS apps; not docs to render)
# - root README.md, CHANGELOG.md, VERSION, TESTING.md
# - .github/ (CI workflows; not docs)
# - implementation/ (CLJS source; not docs)
# - tools/ (tool source + per-tool spec; not staged into docs/)
# - skills/X/Y (skill internals — references/, SKILL.md, etc; not
# staged. Only the single-page summary docs/skills/X.md
# is staged via the Skills nav section.)
# These are rewritten to absolute GitHub blob URLs so the published
# site links to the source-tree view on github.com. Source files
# keep their GitHub-friendly relative paths.
#
# 3. directory-style links to in-tree pages:
# - guide pages link to ../../skills/X/ (the source-tree directory)
# where docs/skills/X.md exists as the published summary — rewrite
# to ../skills/X.md so the published site lands on the summary.
# - guide sub-chapter pages link to ../../xray/ where the published
# site's xray section opens at xray/index.md — rewrite the bare
# directory link to ../../xray/index.md.
# - spec/SPEC-AUTHORING.md links to conformance/ (sibling directory
# of spec/, staged into docs/spec/conformance/) — rewrite the bare
# directory link to conformance/README.md.
#
# Pre-build staging.
#
# The published site needs spec/ and migration/ to live inside docs_dir so
# MkDocs can see them. Without staging, a local `mkdocs build --strict`
# would see dozens of WARNINGs about missing spec/*.md and migration/*.md
# targets — link-rot that's not actually rot, just a missing staging step.
#
# This hook (on_pre_build) does the staging, so that ANY local or CI
# invocation of mkdocs (build, serve, --strict) auto-stages the two trees.
# CI's docs.yml also runs an explicit "Stage spec/ + migration/ into docs/"
# step as a redundant belt-and-braces; both are idempotent (rm -rf + copy).
# The staged dirs are .gitignored.
import re
import shutil
from pathlib import Path
GH_BLOB_BASE = "https://github.com/day8/re-frame2/blob/main"
# Trees that live at the repo root but need to render inside the site.
# Source dir (repo root) -> staged dir (under docs_dir).
_STAGE = (
("spec", "spec"),
("migration", "migration"),
)
def on_pre_build(config):
"""Mirror spec/ and migration/ into docs_dir before MkDocs scans files.
Idempotent: removes the existing staged copy first. The staged paths
are gitignored (see .gitignore: /docs/spec/, /docs/migration/) so this
never dirties the worktree. CI runs the equivalent shell step in
.github/workflows/docs.yml; both are safe to coexist.
"""
docs_dir = Path(config["docs_dir"])
repo_root = docs_dir.parent
for src_name, dest_name in _STAGE:
src = repo_root / src_name
if not src.is_dir():
# Source tree absent — skip silently. A clean checkout always
# has both; the guard keeps the hook safe in unusual layouts.
continue
dest = docs_dir / dest_name
if dest.exists():
shutil.rmtree(dest)
shutil.copytree(src, dest)
# Case 1a: depth-2 pages (guide/*, skills/*, api/*, EP/*, and the per-capability
# sections machines/*, resources/*, routing/*, ssr/*) link to spec via
# ../../spec/ — collapse one level. All live at docs/<tree>/X.md (depth 2 below
# repo root), so from the source tree the correct ref to spec/ is ../../spec/,
# while in the staged docs_dir (where spec/ is copied to docs/spec/) the
# correct ref is ../spec/. The rewrite is identical because they share depth.
_GUIDE_TO_SPEC = re.compile(r'\]\(\.\./\.\./spec/')
# Case 1a-mig: depth-2 pages (guide/*, skills/*, api/*) link to migration via
# ../../migration/. The staged tree puts migration/ at docs/migration/, so
# from a depth-2 page the correct path is ../migration/. Same depth-collapse
# rule as Case 1a, against the migration tree instead of the spec tree.
_GUIDE_TO_MIGRATION = re.compile(r'\]\(\.\./\.\./migration/')
# Case 1a-deep: chapter sub-pages live at docs/core/<chapter-dir>/X.md (depth
# 3 below repo root). From the source tree the correct ref to spec/ is
# ../../../spec/; in the staged docs_dir (docs/spec/) the correct ref is
# ../../spec/. The rewrite collapses one level, same as the depth-2 case.
_GUIDE_DEEP_TO_SPEC = re.compile(r'\]\(\.\./\.\./\.\./spec/')
# Case 1a-deep-mig: depth-3 sub-chapter pages (e.g. a capability tutorial or
# how-to under docs/resources/tutorial/) link to migration via
# ../../../migration/; staged at docs/migration/, the correct ref is
# ../../migration/. Same one-level collapse as Case 1a-deep.
_GUIDE_DEEP_TO_MIGRATION = re.compile(r'\]\(\.\./\.\./\.\./migration/')
# Case 1b: docs-root pages (e.g. docs/release-process.md) link to spec via
# ../spec/ — correct for the GitHub source tree (where spec/ lives at the
# repo root, one level up from docs/). In the staged tree spec/ is copied
# into docs/spec/, so from a docs-root page the correct path is plain
# spec/ (one fewer ../).
_DOCSROOT_TO_SPEC = re.compile(r'\]\(\.\./spec/')
# Case 1b-mig: docs-root pages link to the migration corpus via
# ../migration/ — correct for the source tree (migration/ at the repo
# root). The staged tree puts it at docs/migration/, so from a docs-root
# page the correct path is plain migration/ (one fewer ../).
_DOCSROOT_TO_MIGRATION = re.compile(r'\]\(\.\./migration/')
# Case 1c: spec/* pages link to docs/-root pages (e.g. release-process.md)
# via ../docs/ — correct for the GitHub source tree (where docs/ lives at
# the repo root, one level up from spec/). In the staged tree spec/X.md is
# at docs/spec/X.md and docs-root pages are at docs/X.md, so the correct
# relative path from spec/ is ../release-process.md, i.e. ../ (no docs/
# segment).
_SPEC_TO_DOCSROOT = re.compile(r'\]\(\.\./docs/')
# Case 3a: directory-style ../../skills/X/ from guide pages — rewrite to the
# in-tree summary page ../skills/X.md (one level up from docs/core/X.md is
# docs/, and docs/skills/X.md is the published nav entry). Only the bare
# directory form is matched; sub-paths fall through to the GitHub-URL rewrite.
_GUIDE_SKILL_DIR = re.compile(r'\]\(\.\./\.\./skills/([A-Za-z0-9._-]+)/\)')
# Case 3b: directory-style ../../xray/ from guide sub-chapter pages — the
# published xray section opens at docs/xray/index.md. Rewrite the bare
# directory link to the explicit index file. Source path is unchanged on
# GitHub (where a directory link opens the directory listing as the user
# expects).
_GUIDE_XRAY_DIR = re.compile(r'\]\(\.\./\.\./xray/\)')
# Case 3c: directory-style conformance/ from spec/SPEC-AUTHORING.md — the
# conformance corpus is staged at docs/spec/conformance/, with README.md as
# its index. MkDocs needs the explicit .md target.
#
# The trailing slash is OPTIONAL. GitHub renders `](conformance)` and
# `](conformance/)` identically, so authors write both; without the `/?`
# the slashless form would fall through every rule and ship as a 404,
# which `--strict` fails on (see the `validation:` block in mkdocs.yml).
_SPEC_CONFORMANCE_DIR = re.compile(r'\]\(conformance/?\)')
# Case 2: cross-tree refs that don't exist in the staged docs tree.
# These rewrites apply to ALL pages (guide/, spec/, and docs/-root pages
# like release-process.md), since each tree references examples/, root
# README.md, .github/, CHANGELOG.md, VERSION, and implementation/ from
# its own depth in the source layout.
#
# From guide/ source (docs/core/X.md), path to repo-root: ../../
# From spec/ source (spec/X.md, staged at docs/spec/X.md): ../
# From docs/ source (docs/X.md, e.g. release-process.md): ../
#
# Spec-depth and docs-depth share the same ../prefix, so a single ../-rule
# covers both. The guide-depth rule (../../) handles the third case.
#
# All depths are rewritten to the same absolute GitHub URL.
_REWRITES = (
# examples/ tree (anchor or no anchor; trailing path captured greedily
# up to the closing paren or whitespace). Depth-3 (capability tutorial /
# how-to pages at docs/<cap>/<sub>/X.md) link ../../../examples/; depth-2
# (docs/<tree>/X.md) link ../../examples/; depth-1 (docs/X.md) link
# ../examples/. All resolve to the same GitHub blob URL.
#
# The path after the tree root is OPTIONAL, so a bare `](../examples)`
# naming the whole tree rewrites too. The leading slash sits inside the
# group, so a form with a path maps to `.../examples/<path>` and the bare
# form to `.../examples`; `examples-old/x` does not match — after
# `examples` the regex needs either `/` or the closing paren.
(re.compile(r'\]\(\.\./\.\./\.\./examples(/[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/examples\1)'),
(re.compile(r'\]\(\.\./\.\./examples(/[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/examples\1)'),
(re.compile(r'\]\(\.\./examples(/[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/examples\1)'),
# root README.md
(re.compile(r'\]\(\.\./\.\./README\.md(#[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/README.md\1)'),
(re.compile(r'\]\(\.\./README\.md(#[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/README.md\1)'),
# .github/ tree (workflows, scripts, anything under .github/).
(re.compile(r'\]\(\.\./\.\./\.github/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/.github/\1)'),
(re.compile(r'\]\(\.\./\.github/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/.github/\1)'),
# root CHANGELOG.md
(re.compile(r'\]\(\.\./\.\./CHANGELOG\.md(#[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/CHANGELOG.md\1)'),
(re.compile(r'\]\(\.\./CHANGELOG\.md(#[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/CHANGELOG.md\1)'),
# root VERSION file
(re.compile(r'\]\(\.\./\.\./VERSION\)'),
rf']({GH_BLOB_BASE}/VERSION)'),
(re.compile(r'\]\(\.\./VERSION\)'),
rf']({GH_BLOB_BASE}/VERSION)'),
# implementation/ tree (source files; not docs to render). Depth-3 first:
# chapter sub-pages at docs/core/<chapter-dir>/X.md reach the repo root
# through ../../../, and without this rule MkDocs leaves the link as-is
# and the published link 404s. The depth-2 rule below must not see the depth-3 shape, and
# cannot: the alternatives are tried in order and this one consumes it.
(re.compile(r'\]\(\.\./\.\./\.\./implementation/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/implementation/\1)'),
(re.compile(r'\]\(\.\./\.\./implementation/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/implementation/\1)'),
(re.compile(r'\]\(\.\./implementation/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/implementation/\1)'),
# tools/ tree (per-tool source + per-tool spec/; not staged into docs/).
# The per-tool spec/* lives with its artefact (lockstep contract) per
# spec/Ownership.md §"Canonical homes outside /spec"; rewriting to a
# GitHub URL preserves both the in-source and on-site link targets.
(re.compile(r'\]\(\.\./\.\./tools/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/tools/\1)'),
(re.compile(r'\]\(\.\./tools/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/tools/\1)'),
# skills/X/Y (skill internals — references/, SKILL.md, etc.). The single
# per-skill summary page docs/skills/X.md is staged via the Skills nav;
# everything else under skills/ is repo-only. NOTE: this MUST run after
# the case-3a rewrite that turns guide-page ../../skills/X/ (bare dir)
# into ../skills/X.md (in-tree summary).
(re.compile(r'\]\(\.\./\.\./skills/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/skills/\1)'),
(re.compile(r'\]\(\.\./skills/([^)\s]*)\)'),
rf']({GH_BLOB_BASE}/skills/\1)'),
# root TESTING.md (canonical test-matrix; not staged into docs/).
(re.compile(r'\]\(\.\./\.\./TESTING\.md(#[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/TESTING.md\1)'),
(re.compile(r'\]\(\.\./TESTING\.md(#[^)\s]*)?\)'),
rf']({GH_BLOB_BASE}/TESTING.md\1)'),
)
def on_page_markdown(markdown, page, config, files):
"""Rewrite cross-tree links in guide/*, skills/*, api/*, EP/*, the
per-capability sections (machines/, resources/, async/, routing/, ssr/), and spec/* pages.
See the module docstring for the full rewrite catalogue. Order matters:
the in-tree directory-style rewrites (case 3) MUST run before the
GitHub-URL rewrites (case 2), so that e.g. ../../skills/X/ collapses to
../skills/X.md (in-tree summary) instead of expanding to a GitHub URL
for the directory listing.
"""
src = page.file.src_path.replace('\\', '/')
if (src.startswith('core/') or src.startswith('skills/')
or src.startswith('api/') or src.startswith('EP/')
or src.startswith('machines/') or src.startswith('resources/')
or src.startswith('routing/') or src.startswith('ssr/')
or src.startswith('async/')):
# Choose by source depth. A guide sub-chapter at
# docs/core/<chapter-dir>/X.md is depth 3; its source spec ref is
# ../../../spec/ and the staged path is ../../spec/. Plain
# docs/core/X.md (and docs/EP/X.md) is depth 2;
# ../../spec/ -> ../spec/.
# Applying both rules unconditionally would let the depth-2 rule
# re-rewrite the depth-3 rule's output ../../spec/ down to ../spec/,
# so we dispatch on src depth.
if src.count('/') >= 2:
# depth-3 (or deeper) — sub-chapter pages
markdown = _GUIDE_DEEP_TO_SPEC.sub('](../../spec/', markdown)
markdown = _GUIDE_DEEP_TO_MIGRATION.sub('](../../migration/', markdown)
# Bare ../../xray/ -> ../../xray/index.md (chapter overview).
# Only sub-chapter pages emit this shape; depth-2 guide pages
# would use ../xray/.
markdown = _GUIDE_XRAY_DIR.sub('](../../xray/index.md)', markdown)
else:
markdown = _GUIDE_TO_SPEC.sub('](../spec/', markdown)
markdown = _GUIDE_TO_MIGRATION.sub('](../migration/', markdown)
# Bare ../../skills/X/ -> ../skills/X.md (in-tree summary page).
# Only depth-2 guide pages emit this shape; skills/ sub-
# paths (e.g. ../../skills/X/SKILL.md) fall through to the
# GitHub-URL rewrite below since the published site only carries
# the per-skill summary page, not the skill internals.
markdown = _GUIDE_SKILL_DIR.sub(r'](../skills/\1.md)', markdown)
elif src.startswith('spec/'):
# Spec pages link to docs-root pages via ../docs/ — collapse the
# docs/ segment so the staged-tree path is ../release-process.md
# (sibling of docs/spec/ in the docs_dir).
markdown = _SPEC_TO_DOCSROOT.sub('](../', markdown)
# Bare conformance/ -> conformance/README.md. The conformance corpus
# is staged at docs/spec/conformance/; MkDocs needs the explicit .md.
markdown = _SPEC_CONFORMANCE_DIR.sub('](conformance/README.md)', markdown)
elif '/' not in src:
# Docs-root page (e.g. release-process.md). Rewrite ../spec/ -> spec/
# and ../migration/ -> migration/ (both staged trees are siblings of
# this docs-root page in the staged docs_dir).
markdown = _DOCSROOT_TO_SPEC.sub('](spec/', markdown)
markdown = _DOCSROOT_TO_MIGRATION.sub('](migration/', markdown)
for pattern, replacement in _REWRITES:
markdown = pattern.sub(replacement, markdown)
return markdown