-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathfix_mathml.py
More file actions
executable file
·322 lines (288 loc) · 9.05 KB
/
fix_mathml.py
File metadata and controls
executable file
·322 lines (288 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python3
"""
Fix MathML HTML to remove Unicode characters and improve accessibility.
- Replace Unicode math characters with MathML entity references
- Fix heading hierarchy
- Add/improve ARIA attributes
"""
import re
import sys
# Unicode to MathML entity mapping
UNICODE_TO_ENTITY = {
'∞': '∞',
'→': '→',
'←': '←',
'↔': '↔',
'⇒': '⇒',
'⇐': '⇐',
'⇔': '⇔',
'∈': '∈',
'∉': '∉',
'∋': '∋',
'⊂': '⊂',
'⊃': '⊃',
'⊆': '⊆',
'⊇': '⊇',
'∪': '∪',
'∩': '∩',
'∖': '∖',
'×': '×',
'⋅': '⋅',
'±': '±',
'∓': '∓',
'≤': '≤',
'≥': '≥',
'≠': '≠',
'≈': '≈',
'≡': '≡',
'⟂': '⊥',
'∥': '∥',
'∠': '∠',
'∇': '∇',
'∂': '∂',
'∫': '∫',
'∮': '∮',
'∑': '∑',
'∏': '∏',
'√': '√',
'∀': '∀',
'∃': '∃',
'∄': '∄',
'¬': '¬',
'∧': '∧',
'∨': '∨',
'⊕': '⊕',
'⊗': '⊗',
'∅': '∅',
'ℝ': 'ℝ',
'ℂ': 'ℂ',
'ℕ': 'ℕ',
'ℤ': 'ℤ',
'ℚ': 'ℚ',
'α': 'α',
'β': 'β',
'γ': 'γ',
'δ': 'δ',
'ε': 'ε',
'ζ': 'ζ',
'η': 'η',
'θ': 'θ',
'ι': 'ι',
'κ': 'κ',
'λ': 'λ',
'μ': 'μ',
'ν': 'ν',
'ξ': 'ξ',
'π': 'π',
'ρ': 'ρ',
'σ': 'σ',
'τ': 'τ',
'υ': 'υ',
'φ': 'φ',
'χ': 'χ',
'ψ': 'ψ',
'ω': 'ω',
'Α': 'Α',
'Β': 'Β',
'Γ': 'Γ',
'Δ': 'Δ',
'Ε': 'Ε',
'Ζ': 'Ζ',
'Η': 'Η',
'Θ': 'Θ',
'Ι': 'Ι',
'Κ': 'Κ',
'Λ': 'Λ',
'Μ': 'Μ',
'Ν': 'Ν',
'Ξ': 'Ξ',
'Π': 'Π',
'Ρ': 'Ρ',
'Σ': 'Σ',
'Τ': 'Τ',
'Υ': 'Υ',
'Φ': 'Φ',
'Χ': 'Χ',
'Ψ': 'Ψ',
'Ω': 'Ω',
'↦': '↦',
'∘': '∘',
'⊆': '⊆',
}
def replace_unicode_with_entities(text):
"""Replace Unicode math characters with HTML/MathML entities."""
for unicode_char, entity in UNICODE_TO_ENTITY.items():
text = text.replace(unicode_char, entity)
return text
def fix_heading_hierarchy(html):
"""Fix heading hierarchy: first H1 stays, rest become H2."""
lines = html.split('\n')
h1_count = 0
result = []
for line in lines:
if '<h1' in line:
if h1_count == 0:
# Keep first H1 (main title)
result.append(line)
h1_count += 1
else:
# Convert subsequent H1s to H2
line = line.replace('<h1', '<h2').replace('</h1>', '</h2>')
result.append(line)
else:
result.append(line)
return '\n'.join(result)
def add_aria_to_math(html):
"""Add aria-label to math elements based on LaTeX annotation."""
def add_aria(match):
math_element = match.group(0)
# Extract LaTeX from annotation
latex_match = re.search(r'<annotation encoding="application/x-tex">([^<]+)</annotation>', math_element)
if latex_match:
latex = latex_match.group(1)
# Add aria-label to math tag
math_element = math_element.replace(
'<math ',
f'<math role="math" aria-label="{latex}" '
)
return math_element
# Match math elements
html = re.sub(
r'<math[^>]*>.*?</math>',
add_aria,
html,
flags=re.DOTALL
)
return html
def add_lang_attribute(html):
"""Add lang='en' attribute to <html> element."""
# First, remove any empty or existing lang attributes
html = re.sub(r'\s+lang=""', '', html)
html = re.sub(r'\s+lang="[^"]*"', '', html)
# Then add lang="en" to the html tag
html = re.sub(
r'<html([^>]*)>',
r'<html lang="en"\1>',
html
)
return html
def improve_title(html):
"""Extract title from H1 and set as page title."""
# Extract text from first H1
h1_match = re.search(r'<h1[^>]*>([^<]+)</h1>', html)
if h1_match:
h1_text = h1_match.group(1).strip()
# Replace the title
html = re.sub(
r'<title>[^<]*</title>',
f'<title>{h1_text} - UVA Mathematics</title>',
html
)
return html
def add_breadcrumb_navigation(html):
"""Add breadcrumb navigation at the top of the page."""
# Extract the title from H1 for the current page breadcrumb
h1_match = re.search(r'<h1[^>]*>([^<]+)</h1>', html)
current_page_title = h1_match.group(1).strip() if h1_match else "Current Exam"
breadcrumb_html = '''<nav aria-label="Breadcrumb" style="margin-bottom: 1em;">
<ol style="list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: 0.5em; font-size: 0.9em;">
<li><a href="/">Home</a></li>
<li aria-hidden="true" style="color: #999;">/</li>
<li><a href="/graduate/">Graduate</a></li>
<li aria-hidden="true" style="color: #999;">/</li>
<li><a href="/graduate/generals/">General Exams</a></li>
<li aria-hidden="true" style="color: #999;">/</li>
<li aria-current="page" style="color: #666;">''' + current_page_title + '''</li>
</ol>
</nav>
'''
# Insert after <body> tag
html = html.replace('<body>', '<body>\n' + breadcrumb_html)
return html
def add_back_button(html):
"""Add a back button navigation below breadcrumb."""
back_button_html = '''<nav aria-label="Page navigation" style="margin-bottom: 2em;">
<a href="/graduate/generals/" style="display: inline-block; padding: 0.5em 1em; background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 4px; text-decoration: none; color: #333;">← Back to General Exams</a>
</nav>
'''
# Insert after first </nav> (breadcrumb) before <main>
# First, find the position of the first </nav>
nav_close = html.find('</nav>')
if nav_close != -1:
# Find where to insert (after </nav> and any whitespace, before <main> or <h1>)
insert_pos = nav_close + len('</nav>')
# Skip any whitespace
while insert_pos < len(html) and html[insert_pos] in '\n ':
insert_pos += 1
# Insert the back button
html = html[:insert_pos] + '\n' + back_button_html + html[insert_pos:]
return html
def wrap_content_in_main(html):
"""Wrap main content in <main> landmark element."""
# Find the first H1 (start of main content)
h1_match = re.search(r'(<h1[^>]*>.*?</h1>)', html, re.DOTALL)
if h1_match:
# Insert <main> before the H1
html = html.replace(h1_match.group(1), '<main>\n' + h1_match.group(1))
# Insert </main> before </body>
html = html.replace('</body>', '</main>\n</body>')
return html
def add_exam_spacing(html):
"""Add proper spacing between exam problems."""
# Add CSS if not present
if '.problem-number' not in html:
css_insert = """ /* Exam problem styling */
.problem-number {
display: block;
margin-top: 1.5em;
margin-bottom: 0.5em;
}
</style>"""
html = html.replace(' </style>', css_insert)
# Try to find content in <main>, otherwise work on <body>
main_match = re.search(r'<main>(.*?)</main>', html, re.DOTALL)
if main_match:
content = main_match.group(1)
start_pos = main_match.start(1)
end_pos = main_match.end(1)
else:
# No main element, work on body content
body_match = re.search(r'<body>(.*?)</body>', html, re.DOTALL)
if not body_match:
return html
content = body_match.group(1)
start_pos = body_match.start(1)
end_pos = body_match.end(1)
# Add spacing between problem numbers (2), (3), etc.
# Add double line breaks before numbered problems
content = re.sub(
r'<br\s*/?\>[\s\n]*(\(\d+\))',
r'<br /><br /><span class="problem-number">\1</span>',
content
)
# Replace in original HTML
html = html[:start_pos] + content + html[end_pos:]
return html
def main():
if len(sys.argv) != 3:
print("Usage: fix_mathml.py input.html output.html")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
with open(input_file, 'r', encoding='utf-8') as f:
html = f.read()
# Apply fixes
html = replace_unicode_with_entities(html)
html = fix_heading_hierarchy(html)
html = add_aria_to_math(html)
html = add_lang_attribute(html)
html = improve_title(html)
html = add_breadcrumb_navigation(html)
html = add_back_button(html)
html = wrap_content_in_main(html)
html = add_exam_spacing(html)
with open(output_file, 'w', encoding='utf-8') as f:
f.write(html)
print(f"Fixed HTML written to {output_file}")
if __name__ == '__main__':
main()