-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix_encoding.py
More file actions
65 lines (61 loc) · 2.2 KB
/
Copy pathfix_encoding.py
File metadata and controls
65 lines (61 loc) · 2.2 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
import os, glob
templates_dir = r'c:\Users\Admin\OneDrive - Technische Hochschule Deggendorf\Desktop\webapp\app\templates'
# Each tuple: (bad unicode string, correct unicode string)
# Bad strings are what you get when UTF-8 bytes are misread as Windows-1252/Latin-1
fixes = [
# © (U+00A9) -> UTF-8 C2 A9 misread as latin-1 = ©
('©', '©'),
# ★ (U+2605) -> UTF-8 E2 98 85 misread as win-1252 = ★
('★', '★'),
# ☆ (U+2606) -> UTF-8 E2 98 86 misread as win-1252 = ☆
('☆', '☆'),
# ' (U+2019) -> UTF-8 E2 80 99 misread = ’
('’', '’'),
# " (U+201C) -> UTF-8 E2 80 9C misread = “
('“', '“'),
# " (U+201D) -> UTF-8 E2 80 9D misread = â€
('â€', '”'),
# – (U+2013) -> UTF-8 E2 80 93 misread = â€"
('–', '–'),
# — (U+2014) -> UTF-8 E2 80 94 misread = â€"
('—', '—'),
# • (U+2022) -> UTF-8 E2 80 A2 misread = •
('•', '•'),
# … (U+2026) -> UTF-8 E2 80 A6 misread = …
('…', '…'),
# ─ (U+2500) -> UTF-8 E2 94 80 misread = â"€
('─', '─'),
# × (U+00D7) -> UTF-8 C3 97 misread = ×
('×', '×'),
# é -> UTF-8 C3 A9 misread = é
('é', 'é'),
# è -> UTF-8 C3 A8 misread = è
('è', 'è'),
# à -> UTF-8 C3 A0 misread = Ã
('Ã ', 'à'),
# ã -> UTF-8 C3 A3 misread = ã
('ã', 'ã'),
# ç -> UTF-8 C3 A7 misread = ç
('ç', 'ç'),
# ü -> UTF-8 C3 BC misread = ü
('ü', 'ü'),
# ñ -> UTF-8 C3 B1 misread = ñ
('ñ', 'ñ'),
# non-breaking space artifact
('Â ', ' '),
# stray  before other chars
('Â', ''),
]
total_fixed = 0
for path in glob.glob(os.path.join(templates_dir, '**', '*.html'), recursive=True):
with open(path, 'r', encoding='utf-8') as f:
text = f.read()
original = text
for bad, good in fixes:
text = text.replace(bad, good)
if text != original:
with open(path, 'w', encoding='utf-8', newline='') as f:
f.write(text)
total_fixed += 1
print('Fixed:', os.path.basename(path))
print(f'\nDone. {total_fixed} file(s) updated.')