-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_encoding.py
More file actions
212 lines (174 loc) · 6.61 KB
/
fix_encoding.py
File metadata and controls
212 lines (174 loc) · 6.61 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
"""
Phase 17: Fix UTF-8 mojibake across all Helios OS templates.
Root cause: UTF-8 bytes were double-encoded through a Windows-1252 (cp1252) layer.
Original UTF-8 bytes were misread as cp1252, then re-encoded as UTF-8.
Fix: iterate through text, find mojibake lead-byte characters, attempt
sloppy-cp1252 round-trip to recover original UTF-8 characters.
Uses a custom encoder that handles the 5 "undefined" cp1252 positions
(0x81, 0x8D, 0x8F, 0x90, 0x9D) as pass-through.
"""
import os
import glob
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
# ── Build sloppy cp1252 encode/decode tables ───────────────────────────
# Standard cp1252 leaves 5 positions undefined. Many real-world encoders
# treat them as pass-through (U+0081 <-> 0x81, etc). Python's codec rejects
# them, so we build our own.
# Forward table: byte (0-255) -> Unicode code point (for decoding)
_FWD = {}
for b in range(256):
try:
_FWD[b] = bytes([b]).decode('cp1252')
except UnicodeDecodeError:
_FWD[b] = chr(b) # Pass-through for undefined positions
# Reverse table: Unicode char -> byte value (for encoding)
_REV = {ch: b for b, ch in _FWD.items()}
# Build the set of characters that result from sloppy-cp1252-decoding
# UTF-8 continuation bytes (0x80-0xBF).
CONT_CHARS = set()
for byte_val in range(0x80, 0xC0):
CONT_CHARS.add(_FWD[byte_val])
def sloppy_cp1252_encode(text):
"""Encode a Unicode string to bytes using sloppy cp1252 (with pass-through)."""
result = bytearray()
for ch in text:
byte_val = _REV.get(ch)
if byte_val is not None:
result.append(byte_val)
else:
raise UnicodeEncodeError('sloppy-cp1252', ch, 0, 1, 'character not in sloppy cp1252')
return bytes(result)
def fix_mojibake(text):
"""
Iterate through text and fix mojibake sequences by attempting
sloppy-cp1252 encode -> utf-8 decode round-trip.
Returns (fixed_text, fix_count).
"""
result = []
fixes = 0
i = 0
n = len(text)
while i < n:
ch = text[i]
code = ord(ch)
fixed = False
# 4-byte UTF-8 lead (0xF0-0xF4)
if 0xF0 <= code <= 0xF4 and i + 3 < n:
seq = text[i:i+4]
if all(c in CONT_CHARS for c in seq[1:]):
try:
raw = sloppy_cp1252_encode(seq)
recovered = raw.decode('utf-8')
result.append(recovered)
fixes += 1
i += 4
fixed = True
except (UnicodeDecodeError, UnicodeEncodeError):
pass
# 3-byte UTF-8 lead (0xE0-0xEF)
if not fixed and 0xE0 <= code <= 0xEF and i + 2 < n:
seq = text[i:i+3]
if all(c in CONT_CHARS for c in seq[1:]):
try:
raw = sloppy_cp1252_encode(seq)
recovered = raw.decode('utf-8')
result.append(recovered)
fixes += 1
i += 3
fixed = True
except (UnicodeDecodeError, UnicodeEncodeError):
pass
# 2-byte UTF-8 lead (0xC2-0xDF)
if not fixed and 0xC2 <= code <= 0xDF and i + 1 < n:
seq = text[i:i+2]
if seq[1] in CONT_CHARS:
try:
raw = sloppy_cp1252_encode(seq)
recovered = raw.decode('utf-8')
result.append(recovered)
fixes += 1
i += 2
fixed = True
except (UnicodeDecodeError, UnicodeEncodeError):
pass
if not fixed:
result.append(ch)
i += 1
return ''.join(result), fixes
# ASCII chars that may have replaced smart-quote cp1252 chars
# Order matters: try right-quote first (more common in mojibake contexts)
QUOTE_SUBS = {
'"': ['\u201d', '\u201c'], # ASCII " -> try right then left double smart quotes
"'": ['\u2019', '\u2018'], # ASCII ' -> try right then left single smart quotes
}
def fix_partial_mojibake(text):
"""
Fix mojibake where the last byte's smart quote was normalized to ASCII.
E.g. a euro + ASCII-quote instead of smart-quote after a 3-byte lead.
"""
result = []
fixes = 0
i = 0
n = len(text)
while i < n:
ch = text[i]
code = ord(ch)
fixed = False
# 3-byte lead where third char is an ASCII quote
if 0xE0 <= code <= 0xEF and i + 2 < n:
c2, c3 = text[i+1], text[i+2]
if c2 in CONT_CHARS and c3 in QUOTE_SUBS:
# Try substituting the ASCII quote with each smart-quote variant
for smart_q in QUOTE_SUBS[c3]:
seq = ch + c2 + smart_q
try:
raw = sloppy_cp1252_encode(seq)
recovered = raw.decode('utf-8')
result.append(recovered)
fixes += 1
i += 3
fixed = True
break
except (UnicodeDecodeError, UnicodeEncodeError):
continue
if not fixed:
result.append(ch)
i += 1
return ''.join(result), fixes
def fix_file(filepath):
"""Fix all mojibake in a single template file. Returns count of fixes."""
with open(filepath, 'r', encoding='utf-8') as f:
text = f.read()
original = text
total_fixes = 0
# Run up to 3 passes (in case of triple-encoding or nested artifacts)
for _ in range(3):
text, fixes = fix_mojibake(text)
total_fixes += fixes
if fixes == 0:
break
# Fix partial mojibake (smart quotes normalized to ASCII quotes)
text, partial_fixes = fix_partial_mojibake(text)
total_fixes += partial_fixes
if text != original:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(text)
return total_fixes
def main():
templates = sorted(glob.glob(os.path.join(TEMPLATE_DIR, '*.html')))
print(f"Scanning {len(templates)} template files...\n")
grand_total = 0
for fpath in templates:
fname = os.path.basename(fpath)
fixes = fix_file(fpath)
if fixes > 0:
print(f" FIXED: {fname} ({fixes} replacements)")
grand_total += fixes
else:
print(f" CLEAN: {fname}")
print(f"\n{'='*50}")
print(f"Total fixes applied: {grand_total}")
print(f"Files processed: {len(templates)}")
print("Done.")
if __name__ == '__main__':
main()