-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexcheck
executable file
·225 lines (201 loc) · 7.23 KB
/
texcheck
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
#!/usr/bin/env python3
import sys
import argparse
import os
import re
parser = argparse.ArgumentParser(description='Check LaTeX files for technical writing mistakes.')
parser.add_argument('files', metavar='PATH', type=str, nargs='*',
help='path to check (default=.)', default=['.'])
_FOUND_MISTAKE=False
def main(args):
for path in args.files:
check(path, args)
if _FOUND_MISTAKE:
sys.exit(1)
_SKIP_NAMES = [".git", ".svn"]
def most_files(root):
"""Yields paths similar to os.walk, but skips stuff like .git"""
for file in os.listdir(root):
if file in _SKIP_NAMES:
continue
p = os.path.join(root,file)
if os.path.isdir(p):
yield from most_files(p)
else:
yield p
_EXTENSIONS = "tex md mdwn txt rst".split()
def accept_ext(path):
"""check for acceptable file paths"""
for ext in _EXTENSIONS:
if path.endswith("."+ext):
return True
return False
def check(path, args):
if accept_ext(path):
check_file(path, args)
else:
for p in most_files(path):
if not accept_ext(p):
continue
if p.startswith("./"):
p = p[2:]
check_file(p, args)
_IRREGULARS = "awoken|been|born|beat|become|begun|bent|beset|bet|bid|bidden|bound|bitten|bled|blown|broken|bred|brought|broadcast|built|burnt|burst|bought|cast|caught|chosen|clung|come|cost|crept|cut|dealt|dug|dived|done|drawn|dreamt|driven|drunk|eaten|fallen|fed|felt|fought|found|fit|fled|flung|flown|forbidden|forgotten|foregone|forgiven|forsaken|frozen|gotten|given|gone|ground|grown|hung|heard|hidden|hit|held|hurt|kept|knelt|knit|known|laid|led|leapt|learnt|left|lent|let|lain|lighted|lost|made|meant|met|misspelt|mistaken|mown|overcome|overdone|overtaken|overthrown|paid|pled|proven|put|quit|read|rid|ridden|rung|risen|run|sawn|said|seen|sought|sold|sent|set|sewn|shaken|shaven|shorn|shed|shone|shod|shot|shown|shrunk|shut|sung|sunk|sat|slept|slain|slid|slung|slit|smitten|sown|spoken|sped|spent|spilt|spun|spit|split|spread|sprung|stood|stolen|stuck|stung|stunk|stridden|struck|strung|striven|sworn|swept|swollen|swum|swung|taken|taught|torn|told|thought|thrived|thrown|thrust|trodden|understood|upheld|upset|woken|worn|woven|wed|wept|wound|won|withheld|withstood|wrung|written"
_PASSIVE_PREFIX = "am|are|were|being|is|been|was|be"
_RE_PASSIVE = re.compile(r"\b((%s)\s+(\w+ed|%s))\b" % (_PASSIVE_PREFIX, _IRREGULARS))
def check_passive(path, string):
"""Check string for use of passive voice"""
matches = _RE_PASSIVE.finditer(string)
for m in matches:
before = string[:m.start(0)]
if in_comment(before):
continue
l,c = position(before)
print("%s:%d:%d: %s [passive]" % (path, l, c, m.group(0)))
global _FOUND_MISTAKE
_FOUND_MISTAKE = True
_WEASEL_WORDS = "many|various|very|fairly|several|extremely|exceedingly|quite|remarkably|few|surprisingly|mostly|largely|huge|tiny|((are|is) a number)|excellent|interestingly|significantly|substantially|clearly|vast|relatively|completely|apparent|great|leading|remarkable|notable|honorable|award-winning|visionary|outstanding|prestigious|world-class|turns out|widely|often|most|some|recently|lately|currently|traditionally|soon|presently|occasionally|somehow|not(ice|e) that|major|minor|exciting|surprising|important|interesting|considerably|formally|informally|provably|first of all"
_RE_WEASEL = re.compile(r"\b(%s)\b" % _WEASEL_WORDS)
def check_weasel_words(path, string):
"""Check string for use of weasel words"""
matches = _RE_WEASEL.finditer(string)
for m in matches:
before = string[:m.start(0)]
if in_comment(before):
continue
l,c = position(before)
print("%s:%d:%d: %s" % (path, l, c, m.group(0)))
global _FOUND_MISTAKE
_FOUND_MISTAKE = True
_RE_WORD = re.compile(r"(\w\w+)")
def check_double_words(path, string):
"""Check string for use of double double words"""
prev = ""
matches = _RE_WORD.finditer(string)
for m in matches:
w = m.group(0)
if w != prev:
prev = w
continue
before = string[:m.start(0)]
if in_comment(before):
continue
l,c = position(before)
print("%s:%d:%d: %s %s" % (path, l, c, m.group(0), m.group(0)))
global _FOUND_MISTAKE
_FOUND_MISTAKE = True
_LATEX_MISTAKES = {
"[^~]\\\\cite{": "use ~ before",
"([^\w]et al[^\w])": "use et~al",
"([^\w]et~al\.\s)": "use et~al.\ ",
"([^\w]etc\.\s)": "use etc.\ ",
"([^\w]i\.e\.\s)": "use i.e.\ ",
"([^\w]e\.g\.\s)": "use e.g.\ ",
"([^\w]cf\.\s)": "use cf.\ ",
}
def check_latex(path, string):
"""Check for various common LaTeX mistakes"""
for pattern,msg in _LATEX_MISTAKES.items():
matches = re.compile(pattern).finditer(string)
for m in matches:
before = string[:m.start(0)]
if in_comment(before):
continue
l,c = position(before)
print("%s:%d:%d: %s [%s]" % (path, l, c, m.group(0),msg))
global _FOUND_MISTAKE
_FOUND_MISTAKE = True
_BUMPTIOUS = {
"persons": "people",
"individuals": "people",
"acquire": "get/gain",
"attempt": "try",
"endeavor": "try",
"constitutes": "is",
"supplement": "add to",
"continue": "keep up",
"concerns": "is about",
"employ": "use",
"utilize": "use",
"utilization": "use",
"along the lines of": "like",
"in the nature of": "like",
"in the neighborhood of": "about",
"in terms of": "in/for",
"prior to": "before",
"with reference to": "about/-",
"with regard to": "about/-",
"with the result that": "so that",
"for the purpose of": "for",
"for the reason that": "since/because",
"inasmuch as": "since/because",
"in the case of": "if",
"in the event that": "if",
"as to": "about",
"in favor of": "for/to",
"in order to": "to",
"in accordance with": "by/under",
"from the point of view of": "for",
"accordingly": "so",
"consequently": "so",
"for this reason": "so",
"furthermore": "so",
"hence": "so",
"that is to say": "in other words",
"in addition": "besides/also",
"indeed": "in fact",
"more specifically": "for instance/for example",
"to be sure": "of course",
"likewise": "and/also",
"concerned": "-",
"involved": "-",
"respectively": "-",
"basically": "-",
"essentially": "-",
"simply": "-",
"may be true that": "may",
"is often the case that": "often",
}
_RE_BUMPTIOUS = re.compile(r"[^\w](%s)" % ("|".join(_BUMPTIOUS.keys())))
def check_bumptious(path, string):
"""Check for bumptious phrases"""
matches = _RE_BUMPTIOUS.finditer(string)
for m in matches:
before = string[:m.start(1)]
if in_comment(before):
continue
l,c = position(before)
r = _BUMPTIOUS[m.group(1)]
print("%s:%d:%d: %s => %s" % (path, l, c, m.group(1), r))
global _FOUND_MISTAKE
_FOUND_MISTAKE = True
def check_file(path, args):
"""Check for various writing mistakes"""
assert accept_ext(path)
with open(path) as fh:
contents = fh.read()
check_passive(path, contents)
check_weasel_words(path, contents)
check_double_words(path, contents)
check_latex(path, contents)
check_bumptious(path, contents)
def position(string):
"""Returns (line,column) position of end of string """
line = string.count("\n") + 1
column = len(string) - string.rfind("\n")
return line, column
assert position("foo\nbar") == (2,4)
def in_comment(string):
"""Returns whether end of string is within a TeX comment"""
i = string.rfind("\n")
while True:
i = string.find("%", i+1)
if i == -1:
return False
if string[i-1] != '\\':
return True
assert in_comment("foo\nbla%baz")
assert not in_comment("foo%bla\nbaz")
if __name__ == "__main__":
args = parser.parse_args()
main(args)