chore: enforce prose punctuation checks in docs and commits

Add commit-msg and public documentation checks for em dashes and prose
semicolons outside code blocks, inline code, URLs, and HTML entities so
public docs and commit history stay consistent.
This commit is contained in:
courtmanr@gmail.com
2026-11-23 17:09:22 +00:00
parent 48973a2fe2
commit fe9eb11444
6 changed files with 410 additions and 1 deletions

View File

@@ -103,6 +103,8 @@ jobs:
fi
- name: Validate public documentation
env:
PUBLIC_DOCS_BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
run: python3 scripts/check_public_docs.py
- name: Check getting-started safety and current plans

View File

@@ -27,3 +27,43 @@ if [ "$body_lines" -eq 0 ]; then
echo "commit-msg: git commit -m \"<subject>\" -m \"<why>\" is enough." >&2
exit 1
fi
if command -v python3 >/dev/null 2>&1; then
printf '%s\n' "$message" | python3 -c '
import re, sys
TRAILER = re.compile(r"^(Change-source|Contract-Neutral|Signed-off-by|Co-authored-by|Fixes|Closes|Resolves|Refs|Reviewed-by|Cc|See-also): ")
FENCE_OPEN = re.compile(r"^ {0,3}(`{3,}|~{3,})(.*)$")
CODE_SPAN = re.compile(r"(?<!`)(`+)(?!`)(.*?)(?<!`)\1(?!`)")
URL = re.compile(r"https?://\S+")
HTML_ENTITY = re.compile(r"&(?:#\d+|#x[0-9a-fA-F]+|[A-Za-z][A-Za-z0-9]+);")
closing = None
violations = []
for idx, raw in enumerate(sys.stdin.read().splitlines(), 1):
if closing is not None:
if closing.fullmatch(raw):
closing = None
continue
m = FENCE_OPEN.fullmatch(raw)
if m is not None:
fence, info = m.groups()
if fence[0] != "`" or "`" not in info:
closing = re.compile(r" {0,3}" + re.escape(fence[0]) + "{" + str(len(fence)) + r",}[ \t]*")
continue
if idx > 1 and TRAILER.match(raw):
continue
prose = CODE_SPAN.sub("", raw)
prose = URL.sub("", prose)
prose = HTML_ENTITY.sub("", prose)
if "\u2014" in prose:
violations.append(f"line {idx}: avoid em dashes in commit messages")
if ";" in prose:
violations.append(f"line {idx}: avoid semicolons in commit messages")
if violations:
for item in violations:
print(f"commit-msg: {item}. Use periods, commas, colons, or parentheses instead.", file=sys.stderr)
sys.exit(1)
'
fi

View File

@@ -90,6 +90,11 @@ if staged_files_match '^docs/|^frontend-modern/public/docs/|^(SECURITY|TERMS|ARC
python3 scripts/check_docs_mirror.py --staged
fi
if staged_files_match '^docs/|^frontend-modern/public/docs/|^\.github/ISSUE_TEMPLATE/|^(README|CHANGELOG|SECURITY|TERMS|ARCHITECTURE|CONTRIBUTING)\.md$'; then
echo "Running public docs check..."
python3 scripts/check_public_docs.py --staged
fi
echo "Running browser verification guard..."
python3 scripts/release_control/browser_verification_guard.py

View File

@@ -3,7 +3,10 @@
from __future__ import annotations
import argparse
import os
import re
import subprocess
import sys
from pathlib import Path
from urllib.parse import unquote
@@ -33,6 +36,11 @@ HTML_ASSET = re.compile(r"(?:src|href)=[\"']([^\"']+)[\"']")
HEADING = re.compile(r"^#{1,6}\s+(.+?)\s*#*\s*$", re.MULTILINE)
CODE_SPAN = re.compile(r"(?<!`)(`+)(?!`)(.*?)(?<!`)\1(?!`)")
FENCE_OPEN = re.compile(r"^ {0,3}(`{3,}|~{3,})(.*)$")
HTML_TAG = re.compile(r"<[^>]+>")
HTML_ENTITY = re.compile(r"&(?:#\d+|#x[0-9a-fA-F]+|[A-Za-z][A-Za-z0-9]+);")
BARE_URL = re.compile(r"https?://\S+")
MARKDOWN_LINK_WITH_TEXT = re.compile(r"(!?\[[^\]]*\])\([^)]*\)")
DIFF_HUNK = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@")
MISLEADING_PUBLIC_CLAIMS = {
r"dead-man checks can notify when an expected external signal stops arriving": (
@@ -223,11 +231,211 @@ def check_current_claims() -> list[str]:
return errors
def main() -> int:
def strip_non_prose_inline(line: str) -> str:
"""Remove inline code spans, link targets, HTML tags, URLs, and entities."""
value = CODE_SPAN.sub("", line)
value = MARKDOWN_LINK_WITH_TEXT.sub(r"\1", value)
value = HTML_TAG.sub("", value)
value = BARE_URL.sub("", value)
value = HTML_ENTITY.sub("", value)
return value
def prose_lines_by_number(text: str) -> dict[int, str]:
"""Return 1-based line numbers outside fenced code blocks with non-prose stripped."""
lines: dict[int, str] = {}
closing: re.Pattern[str] | None = None
for index, raw in enumerate(text.splitlines(), 1):
if closing is not None:
if closing.fullmatch(raw):
closing = None
continue
opening = FENCE_OPEN.fullmatch(raw)
if opening is not None:
fence, info = opening.groups()
if fence[0] != "`" or "`" not in info:
closing = re.compile(
r" {0,3}" + re.escape(fence[0]) + "{" + str(len(fence)) + r",}[ \t]*"
)
continue
lines[index] = strip_non_prose_inline(raw)
return lines
def check_prose_punctuation_in_text(
display_path: str,
text: str,
line_numbers: set[int] | None = None,
) -> list[str]:
"""Reject em dashes and semicolons in public documentation prose."""
errors: list[str] = []
for line_no, prose in prose_lines_by_number(text).items():
if line_numbers is not None and line_no not in line_numbers:
continue
if "\u2014" in prose:
errors.append(
f"{display_path}:{line_no}: avoid em dashes in public docs prose"
)
if ";" in prose:
errors.append(
f"{display_path}:{line_no}: avoid semicolons in public docs prose"
)
return errors
def _git_output(*args: str) -> str | None:
try:
result = subprocess.run(
["git", *args],
cwd=ROOT,
text=True,
capture_output=True,
timeout=10,
check=False,
)
except (OSError, subprocess.SubprocessError):
return None
if result.returncode != 0:
return None
return result.stdout
def _parse_added_diff_lines(diff_text: str) -> dict[str, set[int]]:
added: dict[str, set[int]] = {}
current_path: str | None = None
for line in diff_text.splitlines():
if line.startswith("+++ b/"):
current_path = line[6:]
continue
if line.startswith("+++ /dev/null"):
current_path = None
continue
if current_path is None:
continue
match = DIFF_HUNK.match(line)
if match is None:
continue
start = int(match.group(1))
count = 1 if match.group(2) is None else int(match.group(2))
if count == 0:
continue
added.setdefault(current_path, set()).update(range(start, start + count))
return added
def _resolve_base_commit(base_ref: str | None) -> str | None:
if base_ref and re.fullmatch(r"[0-9a-fA-F]{7,40}|[\w./-]+", base_ref):
if _git_output("rev-parse", "--verify", f"{base_ref}^{{commit}}") is not None:
return base_ref
if base_ref:
# Shallow CI push checkouts may omit older base SHAs. Fall back to HEAD~1 when present.
if _git_output("rev-parse", "--verify", "HEAD~1^{commit}") is not None:
return "HEAD~1"
return None
def check_changed_prose_punctuation(
files: list[Path],
*,
staged: bool = False,
base_ref: str | None = None,
) -> list[str]:
"""Check newly added or edited lines in public docs for em dashes and semicolons."""
if _git_output("rev-parse", "--is-inside-work-tree") is None:
return []
public_by_rel = {path.relative_to(ROOT).as_posix(): path for path in files}
if not public_by_rel:
return []
if staged:
diff_text = _git_output(
"diff", "--cached", "-U0", "--no-ext-diff", "--no-color", "--", *sorted(public_by_rel)
)
added_lines = _parse_added_diff_lines(diff_text or "")
errors: list[str] = []
for rel_path, line_numbers in sorted(added_lines.items()):
if rel_path not in public_by_rel:
continue
blob = _git_output("show", f":{rel_path}")
if blob is None:
continue
errors.extend(check_prose_punctuation_in_text(rel_path, blob, line_numbers))
return errors
if base_ref:
resolved = _resolve_base_commit(base_ref)
if resolved is None:
return []
diff_text = _git_output(
"diff", "-U0", "--no-ext-diff", "--no-color", resolved, "HEAD", "--", *sorted(public_by_rel)
)
added_lines = _parse_added_diff_lines(diff_text or "")
errors = []
for rel_path, line_numbers in sorted(added_lines.items()):
path = public_by_rel.get(rel_path)
if path is None or not path.exists():
continue
text = path.read_text(encoding="utf-8", errors="replace")
errors.extend(check_prose_punctuation_in_text(rel_path, text, line_numbers))
return errors
# Plain local invocation: inspect working tree + staged changes and untracked public docs.
diff_text = _git_output(
"diff", "-U0", "--no-ext-diff", "--no-color", "HEAD", "--", *sorted(public_by_rel)
)
added_lines = _parse_added_diff_lines(diff_text or "")
untracked = _git_output("ls-files", "--others", "--exclude-standard", "--", *sorted(public_by_rel))
errors = []
seen: set[str] = set()
for rel_path, line_numbers in sorted(added_lines.items()):
path = public_by_rel.get(rel_path)
if path is None or not path.exists():
continue
seen.add(rel_path)
text = path.read_text(encoding="utf-8", errors="replace")
errors.extend(check_prose_punctuation_in_text(rel_path, text, line_numbers))
for rel_path in sorted((untracked or "").splitlines()):
if rel_path in seen or rel_path not in public_by_rel:
continue
path = public_by_rel[rel_path]
if not path.exists():
continue
text = path.read_text(encoding="utf-8", errors="replace")
errors.extend(check_prose_punctuation_in_text(rel_path, text))
return errors
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Validate public documentation.")
parser.add_argument(
"--staged",
action="store_true",
help="Check added or modified lines in staged public docs.",
)
parser.add_argument(
"--base-ref",
default=os.environ.get("PUBLIC_DOCS_BASE_SHA") or None,
help="Git base commit or ref to diff HEAD against for prose checks.",
)
args = parser.parse_args(argv)
files = public_markdown_files()
errors = check_links(files)
errors.extend(check_public_claims(files))
errors.extend(check_current_claims())
errors.extend(
check_changed_prose_punctuation(
files,
staged=args.staged,
base_ref=args.base_ref,
)
)
if errors:
print("Public documentation check failed:", file=sys.stderr)

View File

@@ -0,0 +1,75 @@
#!/usr/bin/env python3
"""Verify .husky/commit-msg enforces subject, body, and prose punctuation rules."""
from __future__ import annotations
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
HOOK = ROOT / ".husky" / "commit-msg"
class CommitMsgHookTest(unittest.TestCase):
def run_hook(self, message: str) -> subprocess.CompletedProcess[str]:
with tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False) as handle:
handle.write(message)
path = Path(handle.name)
try:
return subprocess.run(
["sh", str(HOOK), str(path)],
cwd=ROOT,
text=True,
capture_output=True,
timeout=10,
check=False,
)
finally:
path.unlink(missing_ok=True)
def test_accepts_clean_commit_message(self) -> None:
result = self.run_hook(
"docs: update installation guide\n\n"
"Align the container setup steps with the current release image.\n"
)
self.assertEqual(result.returncode, 0, result.stderr)
def test_rejects_em_dash_in_subject(self) -> None:
result = self.run_hook(
"docs: update guide \u2014 clearer steps\n\n"
"Clarify the container setup instructions.\n"
)
self.assertNotEqual(result.returncode, 0)
self.assertIn("avoid em dashes", result.stderr)
def test_rejects_semicolon_in_body(self) -> None:
result = self.run_hook(
"docs: update installation guide\n\n"
"Clarify the container setup; remove stale flags.\n"
)
self.assertNotEqual(result.returncode, 0)
self.assertIn("avoid semicolons", result.stderr)
def test_allows_code_spans_fences_and_urls(self) -> None:
result = self.run_hook(
"fix: quote command arguments\n\n"
"Handle `cmd1; cmd2` cleanly in shell examples and links like "
"https://example.invalid/a;b.\n\n"
"```sh\n"
"echo one; echo two # \u2014 example\n"
"```\n"
)
self.assertEqual(result.returncode, 0, result.stderr)
def test_still_requires_body_and_short_subject(self) -> None:
no_body = self.run_hook("docs: short subject\n")
self.assertNotEqual(no_body.returncode, 0)
long_subject = self.run_hook("x" * 73 + "\n\nReasonable body.\n")
self.assertNotEqual(long_subject.returncode, 0)
if __name__ == "__main__":
unittest.main()

View File

@@ -236,5 +236,84 @@ class PublicDocsLinksTest(unittest.TestCase):
self.assertEqual(public_docs.check_links([source]), [])
class PublicDocsProsePunctuationTest(unittest.TestCase):
def test_rejects_em_dash_and_semicolon_in_prose_only(self) -> None:
text = (
"Clean line with a period.\n"
"Line with an em dash \u2014 should fail.\n"
"Line with a semicolon; should fail.\n"
"Inline code `foo; bar \u2014 baz` and HTML &amp; entity and "
"[link](https://example.invalid/a;b) are allowed.\n"
"```sh\n"
"echo one; echo two # \u2014 inside code fence\n"
"```\n"
)
errors = public_docs.check_prose_punctuation_in_text("README.md", text)
self.assertEqual(
errors,
[
"README.md:2: avoid em dashes in public docs prose",
"README.md:3: avoid semicolons in public docs prose",
],
)
def test_diff_mode_checks_only_added_lines_and_ignores_existing_history(self) -> None:
import os
import subprocess
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
env = dict(
os.environ,
GIT_AUTHOR_NAME="Fixture",
GIT_AUTHOR_EMAIL="fixture@example.invalid",
GIT_COMMITTER_NAME="Fixture",
GIT_COMMITTER_EMAIL="fixture@example.invalid",
)
def git(*args: str) -> str:
return subprocess.run(
["git", *args],
cwd=root,
env=env,
text=True,
check=True,
capture_output=True,
timeout=5,
).stdout.strip()
git("init", "-q")
readme = root / "README.md"
readme.write_text("Legacy line; untouched.\n", encoding="utf-8")
git("add", "README.md")
git("commit", "-qm", "initial")
base_sha = git("rev-parse", "HEAD")
readme.write_text(
"Legacy line; untouched.\nAdded line with an em dash \u2014 here.\n",
encoding="utf-8",
)
git("add", "README.md")
with mock.patch.object(public_docs, "ROOT", root):
staged_errors = public_docs.check_changed_prose_punctuation([readme], staged=True)
self.assertEqual(
staged_errors,
["README.md:2: avoid em dashes in public docs prose"],
)
git("commit", "-qm", "add doc line")
with mock.patch.object(public_docs, "ROOT", root):
pr_errors = public_docs.check_changed_prose_punctuation(
[readme], base_ref=base_sha
)
self.assertEqual(
pr_errors,
["README.md:2: avoid em dashes in public docs prose"],
)
if __name__ == "__main__":
unittest.main()