|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +WCAG 2.1 Level AA Compliance Verification Script |
| 4 | +Checks HTML files for accessibility violations as required by the slash command. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import re |
| 9 | +import sys |
| 10 | + |
| 11 | +def check_unicode_violations(filepath): |
| 12 | + """Check for Unicode mathematical characters (U+1D400-U+1D7FF).""" |
| 13 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 14 | + content = f.read() |
| 15 | + matches = re.findall(r'[\U0001D400-\U0001D7FF]', content) |
| 16 | + return matches |
| 17 | + |
| 18 | +def check_h1_tag(filepath): |
| 19 | + """Check for H1 tag.""" |
| 20 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 21 | + content = f.read() |
| 22 | + return '<h1' in content |
| 23 | + |
| 24 | +def check_main_landmark(filepath): |
| 25 | + """Check for <main> landmark.""" |
| 26 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 27 | + content = f.read() |
| 28 | + return '<main' in content |
| 29 | + |
| 30 | +def check_mathml_role(filepath): |
| 31 | + """Check if all <math> tags have role='math'.""" |
| 32 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 33 | + content = f.read() |
| 34 | + math_tags = len(re.findall(r'<math[^>]*>', content)) |
| 35 | + role_math = len(re.findall(r'role="math"', content)) |
| 36 | + return (math_tags, role_math, math_tags == role_math or math_tags == 0) |
| 37 | + |
| 38 | +def check_breadcrumb(filepath): |
| 39 | + """Check for breadcrumb navigation.""" |
| 40 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 41 | + content = f.read() |
| 42 | + return 'aria-label="Breadcrumb"' in content |
| 43 | + |
| 44 | +def verify_file(filepath, verbose=True): |
| 45 | + """ |
| 46 | + Run all WCAG 2.1 Level AA checks on a single file. |
| 47 | + Returns: (passed: bool, violations: list) |
| 48 | + """ |
| 49 | + violations = [] |
| 50 | + |
| 51 | + # Check 1: Unicode violations |
| 52 | + unicode_chars = check_unicode_violations(filepath) |
| 53 | + if unicode_chars: |
| 54 | + violations.append(f"Unicode violations: {len(unicode_chars)} characters found") |
| 55 | + if verbose: |
| 56 | + from collections import Counter |
| 57 | + char_counts = Counter(unicode_chars) |
| 58 | + for char, count in char_counts.most_common(): |
| 59 | + codepoint = f"U+{ord(char):04X}" |
| 60 | + violations.append(f" {char} ({codepoint}): {count} occurrences") |
| 61 | + |
| 62 | + # Check 2: H1 tag |
| 63 | + if not check_h1_tag(filepath): |
| 64 | + violations.append("Missing H1 tag") |
| 65 | + |
| 66 | + # Check 3: <main> landmark |
| 67 | + if not check_main_landmark(filepath): |
| 68 | + violations.append("Missing <main> landmark") |
| 69 | + |
| 70 | + # Check 4: MathML role |
| 71 | + math_count, role_count, passed = check_mathml_role(filepath) |
| 72 | + if not passed: |
| 73 | + violations.append(f"MathML role mismatch: {math_count} <math> tags, {role_count} with role=\"math\"") |
| 74 | + |
| 75 | + # Check 5: Breadcrumb |
| 76 | + if not check_breadcrumb(filepath): |
| 77 | + violations.append("Missing breadcrumb navigation") |
| 78 | + |
| 79 | + return (len(violations) == 0, violations) |
| 80 | + |
| 81 | +def verify_directory(directory, verbose=True): |
| 82 | + """Verify all HTML files in a directory.""" |
| 83 | + html_files = [f for f in os.listdir(directory) if f.endswith('.html')] |
| 84 | + |
| 85 | + if not html_files: |
| 86 | + return (True, 0, 0, []) |
| 87 | + |
| 88 | + passed_files = 0 |
| 89 | + failed_files = 0 |
| 90 | + all_violations = {} |
| 91 | + |
| 92 | + for filename in sorted(html_files): |
| 93 | + filepath = os.path.join(directory, filename) |
| 94 | + passed, violations = verify_file(filepath, verbose=verbose) |
| 95 | + |
| 96 | + if passed: |
| 97 | + passed_files += 1 |
| 98 | + else: |
| 99 | + failed_files += 1 |
| 100 | + all_violations[filename] = violations |
| 101 | + |
| 102 | + return (failed_files == 0, passed_files, failed_files, all_violations) |
| 103 | + |
| 104 | +def main(): |
| 105 | + """Main entry point for standalone verification.""" |
| 106 | + import argparse |
| 107 | + |
| 108 | + parser = argparse.ArgumentParser(description='Verify WCAG 2.1 Level AA compliance for HTML files') |
| 109 | + parser.add_argument('path', nargs='?', help='File or directory to check') |
| 110 | + parser.add_argument('--quiet', '-q', action='store_true', help='Minimal output') |
| 111 | + args = parser.parse_args() |
| 112 | + |
| 113 | + if args.path: |
| 114 | + # Check specific file or directory |
| 115 | + if os.path.isfile(args.path): |
| 116 | + passed, violations = verify_file(args.path, verbose=not args.quiet) |
| 117 | + if passed: |
| 118 | + print(f"✅ {args.path}: PASS") |
| 119 | + return 0 |
| 120 | + else: |
| 121 | + print(f"❌ {args.path}: FAIL") |
| 122 | + for v in violations: |
| 123 | + print(f" - {v}") |
| 124 | + return 1 |
| 125 | + elif os.path.isdir(args.path): |
| 126 | + passed, passed_count, failed_count, violations = verify_directory(args.path, verbose=not args.quiet) |
| 127 | + if passed: |
| 128 | + print(f"✅ All {passed_count} files PASS") |
| 129 | + return 0 |
| 130 | + else: |
| 131 | + print(f"❌ {failed_count} files FAIL, {passed_count} files PASS") |
| 132 | + for filename, file_violations in violations.items(): |
| 133 | + print(f"\n{filename}:") |
| 134 | + for v in file_violations: |
| 135 | + print(f" - {v}") |
| 136 | + return 1 |
| 137 | + else: |
| 138 | + # Check all exam directories |
| 139 | + exam_dirs = [ |
| 140 | + 'graduate/exams/algebra', |
| 141 | + 'graduate/exams/analysis', |
| 142 | + 'graduate/exams/topology' |
| 143 | + ] |
| 144 | + |
| 145 | + total_passed = 0 |
| 146 | + total_failed = 0 |
| 147 | + all_violations = {} |
| 148 | + |
| 149 | + for directory in exam_dirs: |
| 150 | + if not os.path.exists(directory): |
| 151 | + continue |
| 152 | + |
| 153 | + passed, passed_count, failed_count, violations = verify_directory(directory, verbose=not args.quiet) |
| 154 | + total_passed += passed_count |
| 155 | + total_failed += failed_count |
| 156 | + |
| 157 | + if violations: |
| 158 | + for filename, file_violations in violations.items(): |
| 159 | + all_violations[f"{directory}/{filename}"] = file_violations |
| 160 | + |
| 161 | + print("=" * 80) |
| 162 | + print("WCAG 2.1 LEVEL AA VERIFICATION") |
| 163 | + print("=" * 80) |
| 164 | + print() |
| 165 | + print(f"Total files checked: {total_passed + total_failed}") |
| 166 | + print(f"✅ Passed: {total_passed}") |
| 167 | + print(f"❌ Failed: {total_failed}") |
| 168 | + print() |
| 169 | + |
| 170 | + if all_violations: |
| 171 | + print("VIOLATIONS:") |
| 172 | + print() |
| 173 | + for filepath, violations in sorted(all_violations.items()): |
| 174 | + print(f"{filepath}:") |
| 175 | + for v in violations: |
| 176 | + print(f" - {v}") |
| 177 | + print() |
| 178 | + return 1 |
| 179 | + else: |
| 180 | + print("✅ ALL FILES PRODUCTION READY") |
| 181 | + print("✅ Lawsuit Risk: MINIMAL") |
| 182 | + return 0 |
| 183 | + |
| 184 | +if __name__ == '__main__': |
| 185 | + sys.exit(main()) |
0 commit comments