Files
Physics-Notebook/scripts/check_static_accessibility.py

102 lines
3.3 KiB
Python

from html.parser import HTMLParser
from pathlib import Path
import re
import sys
ROOT = Path(__file__).resolve().parents[1]
class HTMLAuditParser(HTMLParser):
def __init__(self, path: Path):
super().__init__(convert_charrefs=True)
self.path = path
self.issues = []
def handle_starttag(self, tag, attrs):
attrs_dict = dict(attrs)
classes = attrs_dict.get("class", "").split()
if tag == "a" and attrs_dict.get("target") == "_blank":
rel_tokens = set(attrs_dict.get("rel", "").split())
if {"noopener", "noreferrer"} - rel_tokens:
self.issues.append(
f"{self.path}: anchor with target=_blank is missing rel=\"noopener noreferrer\""
)
if tag == "button" and "ai-btn" in classes:
has_label = bool(attrs_dict.get("aria-label") or attrs_dict.get("title"))
if not has_label:
onclick = attrs_dict.get("onclick", "")
if not re.search(r"openAI\('[^']+',\s*'[^']+'\)", onclick):
self.issues.append(
f"{self.path}: AI button is missing an accessible label and provider hint"
)
def check_html_files():
issues = []
for path in sorted(ROOT.rglob("*.html")):
parser = HTMLAuditParser(path.relative_to(ROOT))
parser.feed(path.read_text(encoding="utf-8"))
issues.extend(parser.issues)
return issues
def check_index_cards():
index_path = ROOT / "index.html"
content = index_path.read_text(encoding="utf-8")
issues = []
if "document.createElement('a')" not in content:
issues.append("index.html: library cards are no longer rendered as anchors")
if "card.onclick =" in content:
issues.append("index.html: found mouse-only card click handler")
return issues
def check_local_font_setup():
issues = []
banned_references = ("fonts.googleapis.com", "fonts.gstatic.com")
doc_paths = sorted(ROOT.rglob("*.html")) + [ROOT / "CONCEPT_PATTERN.md"]
for path in doc_paths:
if not path.exists():
continue
content = path.read_text(encoding="utf-8")
if any(reference in content for reference in banned_references):
issues.append(f"{path.relative_to(ROOT)}: found remote Google Fonts reference")
required_font_files = [
ROOT / "assets" / "fonts" / "dm-serif-display-latin.woff2",
ROOT / "assets" / "fonts" / "dm-serif-display-latin-ext.woff2",
ROOT / "assets" / "fonts" / "inter-latin.woff2",
ROOT / "assets" / "fonts" / "inter-latin-ext.woff2",
ROOT / "assets" / "fonts" / "jetbrains-mono-latin.woff2",
ROOT / "assets" / "fonts" / "jetbrains-mono-latin-ext.woff2",
]
for path in required_font_files:
if not path.exists():
issues.append(f"{path.relative_to(ROOT)}: missing local font asset")
return issues
def main():
issues = []
issues.extend(check_html_files())
issues.extend(check_index_cards())
issues.extend(check_local_font_setup())
if issues:
print("Static accessibility check failed:")
for issue in issues:
print(f"- {issue}")
return 1
print("Static accessibility check passed.")
return 0
if __name__ == "__main__":
sys.exit(main())