forked from yeKcim/hpmor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-chapters.py
executable file
·196 lines (152 loc) · 5.85 KB
/
check-chapters.py
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
#!/usr/bin/env python3
# by Torben Menke https://entorb.net
# checks chapter .tex files for known issues and propose fixes
# reads hpmor.tex for list of uncommented/relevant/e.g. translated) chapter files
# ignores all lines starting with '%'
# improvements are proposed via chapters/*-autofix.tex files
# configuration in check-chapters.json
# lang: EN, DE, FR, ...
# raise_error: true -> script exits with error, used for autobuild of releases
# print_diff: true : print line of issues
import glob
import os
import re
import json
import difflib
# TODO:
with open("check-chapters.json", mode="r", encoding="utf-8") as fh:
settings = json.load(fh)
def get_list_of_chapter_files() -> list:
list_of_chapter_files = []
# read list of relevant chapter files from hpmor.tex
with open("hpmor.tex", mode="r", encoding="utf-8") as fh:
lines = fh.readlines()
lines = [elem for elem in lines if elem.startswith("\include{chapters/")]
for line in lines:
fileName = re.search("^.*include\{(chapters/.+?)\}.*$", line).group(1)
list_of_chapter_files.append(fileName + ".tex")
return list_of_chapter_files
def process_file(fileIn: str) -> bool:
"""
checks file for know issues
return issues_found = True if we have a finding
the proposal is written to chapters/*-autofix.tex
"""
issue_found = False
with open(fileIn, mode="r", encoding="utf-8") as fh:
cont = fh.read()
# end of line
if "\r" in cont:
issue_found = True
cont = re.sub(r"\r\n?", r"\n", cont)
# more than 1 empty line
if "\n\n\n" in cont:
issue_found = True
cont = re.sub(r"\n\n\n+", r"\n\n", cont)
l_cont = cont.split("\n")
del cont
l_cont_2 = []
for line in l_cont:
lineOrig = line
# do not modify commented out lines
if not re.match("^\s*%", line):
line = fix_line(s=line)
if issue_found == False and lineOrig != line:
issue_found = True
l_cont_2.append(line)
if issue_found:
print(" issues found!")
fileOut = fileIn.replace(".tex", "-autofix.tex")
# inline fixing: use with CAUTION
# fileOut = fileIn
# issue_found = False
with open(fileOut, mode="w", encoding="utf-8", newline="\n") as fh:
fh.write("\n".join(l_cont_2))
if settings["print_diff"]:
file1 = open(fileIn, "r", encoding="utf-8")
file2 = open(fileOut, "r", encoding="utf-8")
diff = difflib.ndiff(file1.readlines(), file2.readlines())
delta = "".join(l for l in diff if l.startswith("+ ") or l.startswith("- "))
print(delta)
return issue_found
def fix_line(s: str) -> str:
# TODO:
s1 = s
# multiple spaces
s = re.sub(r"[ \t][ \t]+", " ", s)
# trailing spaces
s = re.sub(r" +$", "", s)
# remove spaces from empty lines
s = re.sub(r"^\s+$", "", s)
# ' ' at start of emph
s = s.replace("\emph{ ", " \emph{")
# simple
# ... without spaces around
s = s.replace(" *... *", "…")
s = s.replace(" … ", "…")
# NOT for '… ' as in ', no… “I'
# s = re.sub(r" *… *", r"…", s)
s = s.replace(" … ", "…")
# … at end of quotation ' …"' -> '…"'
s = s.replace(' …"', '…"')
# … at end of line
s = re.sub(r" …\n", r'…\n"', s)
# Word…"Word -> Word…" Word
s = re.sub(r"(\w…\")(\w)", r"\1 \2", s)
# Mr / Mrs
s = s.replace("Mr. H. Potter", "Mr~H.~Potter")
s = s.replace("Mr. Potter", "Mr~Potter")
s = re.sub(r"\b(Mrs?)\.?~?\s*", r"\1~", s)
# quotations
if settings["lang"] == "EN":
# in EN the quotations “...”
# "..." -> “...”
s = re.sub(r'"([^"]+)"', r"“\1”", s)
# ” } -> ”}
s = s.replace("” }", "”}")
# quotation marks should go outside of \emph{“...”} -> “\emph{...}”
s = re.sub(r"\\(emph|shout)\{“([^”]+?)”\}", r"“\\\1{\2}”", s)
# lone “ at end of \emph
s = re.sub(r"(\\emph\{[^„]+?)“\}", r"\1}“", s)
if settings["lang"] == "DE":
# in DE the quotations are „...“
# "..." -> “...”
s = re.sub(r'"([^"]+)"', r"“\1”", s)
# “ } -> “}
s = s.replace("“ }", "“}")
# fixing ' "A..."' and ' "\..."'
s = re.sub(r'(^|\s)"((\\|\w).*?)"', r"\1„\2“", s)
# at first word of chapter
s = re.sub(r"\\(lettrine|lettrinepara)\[ante=“\]", r"\\\1[ante=„]", s)
# migrate EN quotations
s = re.sub(r"“([^“”]+?)”", r"„\1“", s)
# quotation marks should go outside of \emph{„...“} -> „\emph{...}“
s = re.sub(r"\\(emph|shout)\{„([^“]+?)“\}", r"„\\\1{\2}“", s)
# lone “ at end of \emph
s = re.sub(r"(\\emph\{[^„]+?)“\}", r"\1}“", s)
# TODO: single quotes
# DE: ‚...‘
# hyphens: (space-hyphen-space) should be "—" (em dash).
s = s.replace("---", "—")
s = s.replace(" — ", "—")
# NOT for '— ' as in ', no… “I'
# s = re.sub(r" — ", r"—", s)
# TODO: there is a shorter dash as well..
# - -> — and – -> —
# Note: good, but many false positives
# \emph{...} word \emph{...} -> \emph{... \emph{word} ...
# s = re.sub(r"(\\emph\{[^\}]+)\} ([^ ]+) \\emph\{", r"\1 \\emph{\2} ", s)
return s
if __name__ == "__main__":
# cleanup first
for fileOut in glob.glob("chapters/*-autofix.tex"):
os.remove(fileOut)
list_of_chapter_files = get_list_of_chapter_files()
any_issue_found = False
for fileIn in list_of_chapter_files:
print(fileIn)
issue_found = process_file(fileIn=fileIn)
if issue_found:
any_issue_found = True
if settings["raise_error"]:
assert any_issue_found == False, "Issues found, please fix!"