|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Awesome List lint (strict on resources, permissive on TOC) |
| 4 | +
|
| 5 | +Errors: |
| 6 | +- README.md missing |
| 7 | +- Missing "Contribute"/"Contributing" heading (accepts contribut* variants) |
| 8 | +- Missing "License" heading |
| 9 | +- TAB characters in README.md |
| 10 | +- Trailing whitespace on list lines that start with "- [" or "* [" |
| 11 | +- External resource entries must be in the format: |
| 12 | + - [Name](https://example.com) — Short, neutral description. |
| 13 | + (also allows '-' instead of '—') |
| 14 | +
|
| 15 | +Allows: |
| 16 | +- TOC / internal anchor bullets: |
| 17 | + - [Section](#section) |
| 18 | + (no description required) |
| 19 | +
|
| 20 | +Notes: |
| 21 | +- Ignores code blocks. |
| 22 | +- Duplicate headings are warnings, not failures. |
| 23 | +""" |
| 24 | +from __future__ import annotations |
| 25 | + |
| 26 | +import re |
| 27 | +from pathlib import Path |
| 28 | +from collections import Counter |
| 29 | + |
| 30 | +HEADING_RE = re.compile(r"^(#{1,6})\s+(.+?)\s*$") |
| 31 | +CODE_FENCE_RE = re.compile(r"^\s*```") |
| 32 | + |
| 33 | +# Basic markdown bullet + link capture |
| 34 | +BULLET_LINK_RE = re.compile(r"^[-*]\s+\[[^\]]+\]\(([^)]+)\)\s*$") |
| 35 | + |
| 36 | +# External resource entry with description |
| 37 | +RESOURCE_WITH_DESC_RE = re.compile( |
| 38 | + r"^[-*]\s+\[[^\]]+\]\((https?://[^\s)]+)\)\s*(—|-)\s+.+" |
| 39 | +) |
| 40 | + |
| 41 | +def strip_code_blocks(lines: list[str]) -> list[tuple[int, str]]: |
| 42 | + in_code = False |
| 43 | + out: list[tuple[int, str]] = [] |
| 44 | + for i, line in enumerate(lines, start=1): |
| 45 | + if CODE_FENCE_RE.match(line): |
| 46 | + in_code = not in_code |
| 47 | + continue |
| 48 | + if not in_code: |
| 49 | + out.append((i, line)) |
| 50 | + return out |
| 51 | + |
| 52 | +def main() -> int: |
| 53 | + readme = Path("README.md") |
| 54 | + if not readme.exists(): |
| 55 | + print("ERROR: README.md not found.") |
| 56 | + return 1 |
| 57 | + |
| 58 | + text = readme.read_text(encoding="utf-8", errors="ignore") |
| 59 | + if "\t" in text: |
| 60 | + print("ERROR: README.md contains TAB characters. Replace tabs with spaces.") |
| 61 | + return 1 |
| 62 | + |
| 63 | + lines = text.splitlines() |
| 64 | + noncode = strip_code_blocks(lines) |
| 65 | + |
| 66 | + # headings |
| 67 | + headings: list[str] = [] |
| 68 | + for _ln, line in noncode: |
| 69 | + m = HEADING_RE.match(line) |
| 70 | + if m: |
| 71 | + headings.append(m.group(2).strip()) |
| 72 | + |
| 73 | + lower = [h.lower().strip() for h in headings] |
| 74 | + |
| 75 | + if not any(h in ("contribute", "contributing") or "contribut" in h for h in lower): |
| 76 | + print("ERROR: Missing a 'Contribute'/'Contributing' section heading in README.md.") |
| 77 | + return 1 |
| 78 | + |
| 79 | + if not any("license" in h for h in lower): |
| 80 | + print("ERROR: Missing a 'License' section heading in README.md.") |
| 81 | + return 1 |
| 82 | + |
| 83 | + # Duplicate heading warning (non-fatal) |
| 84 | + c = Counter(lower) |
| 85 | + dups = [h for h, n in c.items() if n > 1] |
| 86 | + if dups: |
| 87 | + print("WARNING: Duplicate section headings detected:") |
| 88 | + for h in sorted(dups): |
| 89 | + print(f" - {h}") |
| 90 | + print() |
| 91 | + |
| 92 | + errors = 0 |
| 93 | + |
| 94 | + for ln, line in noncode: |
| 95 | + s = line.rstrip("\n") |
| 96 | + |
| 97 | + # Only lint bullets that look like markdown-link bullets |
| 98 | + if not (s.startswith("- [") or s.startswith("* [")): |
| 99 | + continue |
| 100 | + |
| 101 | + # Trailing whitespace (fail) |
| 102 | + if s != s.rstrip(): |
| 103 | + print(f"ERROR: Trailing whitespace on list line {ln}.") |
| 104 | + errors += 1 |
| 105 | + |
| 106 | + # If it's exactly a TOC-style bullet like "- [Section](#section)", allow it. |
| 107 | + m = BULLET_LINK_RE.match(s.strip()) |
| 108 | + if m: |
| 109 | + url = m.group(1).strip() |
| 110 | + if url.startswith("#"): |
| 111 | + continue # TOC anchor bullet is valid |
| 112 | + |
| 113 | + # For external resources, require description format |
| 114 | + if s.find("(http://") != -1 or s.find("(https://") != -1: |
| 115 | + if RESOURCE_WITH_DESC_RE.match(s): |
| 116 | + continue |
| 117 | + |
| 118 | + print(f"ERROR: Resource entry bullet malformed on line {ln}. Expected:") |
| 119 | + print(" - [Name](https://example.com) — Short, neutral description.") |
| 120 | + print("or") |
| 121 | + print(" - [Name](https://example.com) - Short, neutral description.") |
| 122 | + print(f" {s}") |
| 123 | + errors += 1 |
| 124 | + |
| 125 | + if errors: |
| 126 | + print(f"Found {errors} lint error(s).") |
| 127 | + return 1 |
| 128 | + |
| 129 | + print("Awesome list lint: OK") |
| 130 | + return 0 |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + raise SystemExit(main()) |
0 commit comments