-
Notifications
You must be signed in to change notification settings - Fork 4
/
check.py
executable file
·321 lines (277 loc) · 10.3 KB
/
check.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
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
#! /usr/bin/env python3
import os
from pathlib import Path
import re
import subprocess
import typing as t
import click
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
BIB = "bibitems"
CIT = "fullcites"
def extract_targets(filename: str) -> t.Mapping[str, t.Mapping[str, str]]:
"""Parses a LaTeX file and returns two mappings of IDs to target output
extracted from `bibexbox` environments. The outer mapping keys are
`fullcites` and `bibitems`.
"""
subprocess.run(["make", filename], check=True)
if not os.path.isfile(filename):
raise click.FileError(f"Could not generate {filename}.")
click.echo()
targets = {BIB: dict(), CIT: dict()}
undotted = list()
GOBBLE = 0
HEADER = 1
TARGET = 2
state = GOBBLE
head_buffer = ""
item_buffer = ""
current_id = None
category = CIT
with open(filename) as f:
for line in f:
if state == GOBBLE:
if "\\begin{bibexbox}" in line:
state = HEADER
category = CIT
head_buffer += line.strip()
else:
continue
if state == HEADER:
if current_id:
if line.startswith("["):
head_buffer += line.strip()
else:
head_buffer = ""
state = TARGET
elif line.startswith(("<", "[", "{")):
head_buffer += line.strip()
if head_buffer:
if m := re.search(
r"\\begin\{bibexbox\}"
r"(?P<state>\(.*?\))?"
r"(?P<source><.*?>)?"
r"(?P<note>\[.*?\])?"
r"\{(?P<id>[^}]*)\}"
r"(?P<opt>\[.*?\])?",
head_buffer,
):
current_id = m.group("id")
if m.group("opt"):
head_buffer = ""
state = TARGET
continue
if line.startswith("%"):
continue
if line.startswith("\\toggletrue"):
category = BIB
continue
if "\\tcblower" in line:
if not item_buffer.endswith("."):
undotted.append(current_id)
item_buffer += "."
targets[category][current_id] = item_buffer
item_buffer = ""
current_id = None
state = GOBBLE
continue
if item_buffer:
item_buffer += " "
item_buffer += line.strip().replace("\\@", "").replace("~", " ")
if undotted:
click.echo(f"Info: added full stop to entries {', '.join(undotted)}.")
return targets
def get_bibitems(filename: str) -> t.List[str]:
"""From a text file, extracts and returns a list of lines in a
recurring pattern such that there is a `\\bibitem` line, followed
by a line containing a formatted reference, followed by a blank
line signifying the end of the reference.
"""
# Ensure file exists and is up to date:
subprocess.run(["make", filename], check=True)
if not os.path.isfile(filename):
raise click.FileError(f"Could not generate {filename}.")
click.echo()
# Process BBI file:
preamble = True
lines = list()
current_line = list()
def finish_record():
line = " ".join(current_line)
line = re.sub(r"\\([A-Za-z]+)\s+\{", r"\\\1{", line)
lines.append(line)
lines.append("")
current_line.clear()
with open(filename) as f:
for line in f:
# Ignore first few lines:
if preamble:
if line.startswith("\\bibitem"):
preamble = False
else:
continue
# Check for end of record
clean_line = line.strip()
is_eor = False
if not clean_line:
continue
elif clean_line in ["{}", "\\end{thebibliography}"]:
is_eor = True
if is_eor:
finish_record()
continue
current_line.append(clean_line)
s = " ".join(current_line)
if (
s.startswith("\\bibitem")
and s.count("[") == s.count("]")
and s.count("{") == s.count("}")
):
# `\\bibitem` line is syntactically complete
lines.append(s)
current_line.clear()
continue
if current_line:
finish_record()
return lines
def parse_simple_bibitems(lines: t.List[str]) -> t.Mapping[str, t.Mapping[str, str]]:
"""Parses the output from biblatex2bibitem and returns a mapping of
IDs to actual bibitem output.
"""
outputs = {BIB: dict(), CIT: dict()}
category = BIB
current_id = None
for line in lines:
if current_id is None:
if m := re.search(r"\\bibitem\{(?P<id>[^}]*)\}", line):
current_id = m.group("id")
elif line.startswith("References"):
category = CIT
elif line == "":
current_id = None
else:
line = re.sub(r"“\{”(.*?)\}", r"\\enquote{\1}", line)
line = re.sub(r"‘(.*?)’(?!\w)", r"\\enquote{\1}", line)
line = re.sub(r"(?<!\w)\{(.*?)\}(?!\w)", r"\1", line)
outputs[category][current_id] = (
line.replace("’", "'").replace("\\@", "").replace("~", " ")
)
return outputs
def contrast_refs(**kwargs: t.Mapping[str, t.Mapping[str, str]]) -> t.Mapping[str, int]:
"""Performs a comparison between different sets of mappings from
bib database IDs to formatted references.
Arguments should be given in the form of label=mapping. The label
is used in the output printed to screen, to show the source of the
reference text.
"""
if not kwargs:
raise click.ClickException("No contrast to make.")
labels = list(kwargs.keys())
stats = {"passed": 0, "skipped": 0}
for id, target in kwargs[labels[0]].items():
errors = list()
skippable = "(not in book)" in target
ambiguous = ( # Should we ignore disambiguation letters?
(re.search(r"\(([–\d]+)\\emph\{[ab]\}\)", target) is None)
if not skippable
else False
)
for label in labels[1:]:
if id not in kwargs[label]:
errors.append(f"{label} is missing ID {id}.")
elif skippable:
stats["skipped"] += 1
else:
if ambiguous:
kwargs[label][id] = re.sub(
r"\(([–\d]+)\\emph\{[ab]\}\)", r"(\1)", kwargs[label][id]
)
if kwargs[label][id] == target:
stats["passed"] += 1
else:
errors.append(f"{label}: {kwargs[label][id]}")
if errors:
click.secho(id, bold=True)
click.echo(f"{labels[0]}: {target}")
for error in errors:
click.echo(error)
click.echo()
return stats
def parse_log(jobname: str):
logfile = Path(f"{jobname}.log")
if not logfile.is_file():
return
error_lines = list()
capture_til_blank = False
with open(logfile) as f:
for whole_line in f:
line = whole_line.strip()
if line.startswith("! ") or line.startswith("Package biblatex Warning"):
capture_til_blank = True
error_lines.extend(["", line])
continue
elif line.startswith("WARNING: biblatex-oxref"):
error_lines.extend(["", line])
continue
if capture_til_blank:
if not line:
capture_til_blank = False
else:
error_lines.append(line)
if error_lines:
click.echo("\nErrors and warnings from log file:")
for line in error_lines:
click.echo(f" {line}")
click.echo()
def run_lbx_check(language: str):
for i, style in enumerate(["oxalph", "oxnotes", "oxnum", "oxyear"]):
if i:
click.echo()
click.echo(f"--- Testing for errors in {language} translation of {style} ---")
jobname = f"test-{language}-{style}"
filename = f"{jobname}.pdf"
try:
subprocess.run(["make", filename], check=True)
except subprocess.CalledProcessError as e:
parse_log(jobname)
raise click.ClickException(f"LaTeX run did not complete successfully.\n{e}")
parse_log(jobname)
if not os.path.isfile(filename):
raise click.FileError(f"Could not generate {filename}.")
click.echo("Compilation successful.")
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument(
"style",
type=click.Choice(["oxalph", "oxnotes", "oxnum", "oxyear", "spanish", "polish"]),
)
def main(style):
"""Performs unit tests on LaTeX output from the biblatex-oxref
bibliography styles.
"""
if style in ["spanish", "polish"]:
return run_lbx_check(style)
targets = extract_targets(f"{style}-doc.tex")
lines = get_bibitems(f"{style}.bbi")
outputs = parse_simple_bibitems(lines)
click.echo(
click.style("Targets:", bold=True)
+ f" {len(targets[BIB])} {BIB}, {len(targets[CIT])} {CIT}. "
+ click.style("Outputs:", bold=True)
+ f" {len(outputs[BIB])} {BIB}, {len(outputs[CIT])} {CIT}.\n"
)
stats = dict()
stats[BIB] = contrast_refs(Target=targets[BIB], Output=outputs[BIB])
stats[CIT] = contrast_refs(Target=targets[CIT], Output=outputs[CIT])
for key in [BIB, CIT]:
fails = len(targets[key])
for stat in stats[key].values():
fails -= stat
stats[key]["failed"] = fails
click.echo(
click.style(f"Test {key}:", bold=True)
+ f" {stats[key]['passed']} passed;"
+ f" {stats[key]['skipped']} skipped;"
+ f" {stats[key]['failed']} failed."
)
parse_log(f"test-{style}")
if __name__ == "__main__":
main()