|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Fail when a section page with children has no subpage cards. |
| 3 | +
|
| 4 | +WHY A CHECK RATHER THAN AUTOMATIC GENERATION (DOC-1509) |
| 5 | +
|
| 6 | +Cards are placed by an explicit `{{< subpage-cards >}}` marker, not injected, |
| 7 | +because placement carries meaning: every section page that had cards put them |
| 8 | +below its intro prose, and the generated API reference must have none at all |
| 9 | +(its `## Directory` table already lists the same children). |
| 10 | +
|
| 11 | +The one thing implicit generation genuinely bought was that a new section page |
| 12 | +could not ship with no navigation because its author did not know the |
| 13 | +convention. This check buys that back without taking the placement decision away |
| 14 | +from the author. Convention plus a gate -- the same shape as |
| 15 | +check_deleted_pages.py and check_generated_content.py, both of which exist |
| 16 | +because a convention on its own did not hold. |
| 17 | +
|
| 18 | +SCOPE |
| 19 | +
|
| 20 | +Authored content only. `content/api-reference/` is generated and deliberately |
| 21 | +has no cards; running this over it would fail on 70 pages by design. |
| 22 | +
|
| 23 | +A page opts out with `subpage_cards: false` in its front matter. Use it for a |
| 24 | +section whose landing page is a document in its own right rather than a |
| 25 | +signpost -- and say why in a comment, because the next person will wonder. |
| 26 | +
|
| 27 | +Usage: |
| 28 | + check_subpage_cards.py [content-dir] [--exclude-dir PATH ...] |
| 29 | +""" |
| 30 | + |
| 31 | +import argparse |
| 32 | +import re |
| 33 | +import sys |
| 34 | +from pathlib import Path |
| 35 | + |
| 36 | +MARKER = re.compile(r"\{\{[<%]\s*subpage-cards\b") |
| 37 | +LINK_CARD = re.compile(r"\{\{[<%]\s*link-card\b") |
| 38 | +OPT_OUT = re.compile(r"^subpage_cards:\s*false\s*$", re.MULTILINE) |
| 39 | + |
| 40 | +DEFAULT_EXCLUDES = ("api-reference", "__docs_builder__") |
| 41 | + |
| 42 | + |
| 43 | +def frontmatter(text: str) -> str: |
| 44 | + if not text.startswith("---"): |
| 45 | + return "" |
| 46 | + end = text.find("\n---", 3) |
| 47 | + return text[4:end] if end != -1 else "" |
| 48 | + |
| 49 | + |
| 50 | +def main() -> int: |
| 51 | + ap = argparse.ArgumentParser(description=__doc__, |
| 52 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 53 | + ap.add_argument("content", nargs="?", default="content", type=Path) |
| 54 | + ap.add_argument("--exclude-dir", action="append", default=[]) |
| 55 | + ap.add_argument("--baseline", type=Path, |
| 56 | + help="file of paths that predate this gate, one per line") |
| 57 | + args = ap.parse_args() |
| 58 | + |
| 59 | + if not args.content.is_dir(): |
| 60 | + print(f"check-subpage-cards: no such directory: {args.content}", file=sys.stderr) |
| 61 | + return 2 |
| 62 | + |
| 63 | + excludes = set(DEFAULT_EXCLUDES) | set(args.exclude_dir) |
| 64 | + |
| 65 | + baseline = set() |
| 66 | + if args.baseline and args.baseline.is_file(): |
| 67 | + for line in args.baseline.read_text(encoding="utf-8").splitlines(): |
| 68 | + line = line.split("#", 1)[0].strip() |
| 69 | + if line: |
| 70 | + baseline.add(line) |
| 71 | + |
| 72 | + missing, opted_out, legacy, grandfathered, ok = [], [], [], [], 0 |
| 73 | + for index in sorted(args.content.rglob("_index.md")): |
| 74 | + rel = index.relative_to(args.content) |
| 75 | + if rel.parts and rel.parts[0] in excludes: |
| 76 | + continue |
| 77 | + d = index.parent |
| 78 | + has_child = any(p.is_dir() for p in d.iterdir()) or \ |
| 79 | + any(p.name != "_index.md" for p in d.glob("*.md")) |
| 80 | + if not has_child: |
| 81 | + continue |
| 82 | + |
| 83 | + text = index.read_text(encoding="utf-8") |
| 84 | + if OPT_OUT.search(frontmatter(text)): |
| 85 | + opted_out.append(str(rel)) |
| 86 | + elif MARKER.search(text): |
| 87 | + ok += 1 |
| 88 | + elif LINK_CARD.search(text): |
| 89 | + legacy.append(str(rel)) |
| 90 | + elif str(rel) in baseline: |
| 91 | + grandfathered.append(str(rel)) |
| 92 | + else: |
| 93 | + missing.append(str(rel)) |
| 94 | + |
| 95 | + total = ok + len(opted_out) + len(legacy) + len(grandfathered) + len(missing) |
| 96 | + print(f"check-subpage-cards: {total} section page(s) with children " |
| 97 | + f"({ok} with cards, {len(opted_out)} opted out" |
| 98 | + + (f", {len(legacy)} still hand-written" if legacy else "") |
| 99 | + + (f", {len(grandfathered)} in the baseline" if grandfathered else "") + ")") |
| 100 | + |
| 101 | + if legacy: |
| 102 | + print("") |
| 103 | + print(f"NOTE: {len(legacy)} page(s) still use hand-written link-card blocks.") |
| 104 | + print(" Not a failure -- they render. Migrate to {{< subpage-cards >}}") |
| 105 | + print(" so a card and the page it points at cannot drift apart.") |
| 106 | + for p in legacy: |
| 107 | + print(f" {p}") |
| 108 | + |
| 109 | + if not missing: |
| 110 | + print("check-subpage-cards: OK") |
| 111 | + return 0 |
| 112 | + |
| 113 | + print("") |
| 114 | + print(f"FATAL: {len(missing)} section page(s) with children have no subpage cards.") |
| 115 | + print(" A reader landing there gets no way forward except the sidebar.") |
| 116 | + print("") |
| 117 | + print(" Add {{< subpage-cards >}} where the cards belong -- usually after") |
| 118 | + print(" the intro prose, not at the top. If this page genuinely should not") |
| 119 | + print(" have them, set `subpage_cards: false` in its front matter and say why.") |
| 120 | + print("") |
| 121 | + for p in missing: |
| 122 | + print(f" {p}") |
| 123 | + print("") |
| 124 | + return 1 |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + sys.exit(main()) |
0 commit comments