forked from krr-up/bibliography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibfmt.py
executable file
·383 lines (328 loc) · 11.5 KB
/
bibfmt.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
"""
Script to cleanup bibtex records and pretty print them.
"""
import sys
from io import StringIO
from argparse import ArgumentParser
from difflib import ndiff
from collections import OrderedDict
from itertools import chain
import bibtexparser as bp
from bibtexparser.bibdatabase import BibDataStringExpression
from bibtexparser.bparser import BibTexParser
from bibtexparser.bwriter import BibTexWriter
from bibtexparser.latexenc import unicode_to_latex_map
from bibtexparser.customization import splitname
def check_min_version():
"""
Ensure that a new enough version of bibtexparser is used.
"""
vers = bp.__version__.split(".")
if (int(vers[0]), int(vers[1])) < (1, 2):
raise RuntimeError("The script requires at least bibtexparser version 1.2.")
def is_ascii(x):
"""
Reurn true if the given string contains ascii symbols only.
"""
try:
x.encode("ascii")
return True
except UnicodeEncodeError:
return False
# Map from unicode symbols to latex expressions.
#
# The bibtexparser.latexenc module also maps some ascii characters to unicode
# symbols. Such characters are ignored in the map.
UNICODE_TO_LATEX = {
key: value for key, value in unicode_to_latex_map.items() if not is_ascii(key)
}
def apply_on_expression(x, f):
"""
Apply the function f for converting strings to bibtex expressions as
returned by the bibtexparser module.
"""
if isinstance(x, str):
return f(x)
if isinstance(x, BibDataStringExpression):
x.apply_on_strings(f)
return x
def cleanup_expression(x):
"""
Convert the given string containing unicode symbols into a string with
latex escapes only.
"""
ret = []
for char in x:
if char in (" ", "{", "}"):
ret.append(char)
else:
ret.append(UNICODE_TO_LATEX.get(char, char))
return "".join(ret)
def cleanup_record(x):
"""
Cleanup a record as returned by the bibtexparser module.
"""
for val in x:
if val in ("ID",):
continue
x[val] = apply_on_expression(x[val], cleanup_expression)
if val.lower() == "pages":
x[val] = x[val].replace("--", "-")
return x
def _parser(customization=cleanup_record):
"""
Return a configured bibtex parser.
"""
parser = BibTexParser()
parser.interpolate_strings = False
parser.customization = customization
return parser
def _writer(sorted_entries=True):
"""
Return a configured bibtex writer.
"""
writer = BibTexWriter()
writer.indent = " "
writer.order_entries_by = ("ID",) if sorted_entries else None
writer.display_order = ["title", "author", "editor"]
return writer
def _fixdb(db):
"""
Currently sorts the strings in the database.
"""
db.strings = OrderedDict(sorted(db.strings.items()))
return db
def format_bib(path):
"""
Format the given bibliography file.
"""
# read bibliography
with open(path, "r") as f:
db = _fixdb(bp.load(f, _parser()))
# write the bibliography
with open(path, "w") as f:
bp.dump(db, f, _writer())
class NameFormatter:
"""
Formatter to abbreviate first names.
It does well with most names. In particular, all names composed of two words
are handled correctly. For names with more than two words, splitting uses
bibtex rules, so you may need to fix some of them manually. For example, `Juan
Carlos Nieves` is correctly parsed as `{Juan Carlos} Nieves` but `Manuel Ojeda
Aciego` is incorrectly parsed as `{Manuel Ojeda} Aciego`. It manually has to be
written as `Manuel {Ojeda Aciego}`.
Names that do not follow bibtex rules but are already present in the
bibliography are parsed correctly. Such names only have to be adjusted at the
moment they are introduced.
"""
def __init__(self, dbs):
"""
Initialize the name formatter with the special names already present in
the given databases.
"""
self.special_names = {}
self.partial_special_names = set()
self._init_special_names(chain(*(list(db.entries) for db in dbs)))
self._init_partial_special_names()
def _init_special_names(self, entries) -> None:
"""
Initialize the special names from the entries.
"""
for entry in entries:
self._init_special_name(entry.get("author", ""))
self._init_special_name(entry.get("editor", ""))
def _match_braces(self, name: str) -> list[tuple[int, int]]:
"""
Return the list of matching braces in the given string.
"""
stack = []
matched_braces = []
for i, c in enumerate(name):
if c == "{":
stack.append(i)
elif c == "}":
if stack:
matched_braces.append((stack.pop(), i))
return matched_braces
def _init_special_name(self, names: str) -> None:
"""
Initialize the special names in the given names as a list of
dictionaries representing the bibtex fields.
"""
for name in names.replace("\n", " ").split(" and "):
name_dict = splitname(name)
last_str = name_dict.get("last", [])
if not last_str:
continue
last = name_dict.get("last", [])
last_str = " ".join(last)
matched_braces = self._match_braces(last_str)
if (
matched_braces
and matched_braces[-1][0] == 0
and matched_braces[-1][1] == len(last_str) - 1
):
last = tuple(last_str[1:-1].split())
self.special_names[last] = name_dict
von = name_dict.get("von", [])
if not von:
continue
von = " ".join(name_dict.get("von", []))
matched_braces = self._match_braces(von)
if (
matched_braces
and matched_braces[-1][0] == 0
and matched_braces[-1][1] == 2
and von[1].isupper()
):
von = tuple([*(von[1] + von[3:]).split(), *last])
self.special_names[von] = name_dict
def _init_partial_special_names(self) -> None:
"""
Initialize the partial special names.
self.special_names must be initialized first.
"""
for words in self.special_names:
if len(words) > 1:
for i, _ in enumerate(words[1:]):
self.partial_special_names.add(tuple(words[1 + i :]))
def _format_first_name(self, name: str) -> str:
"""
Abbreviate the given first name.
"""
if len(name) > 2 and not name.startswith("{\\"):
name = f"{name[0]}."
return name
def _is_special_name(self, first: tuple, last: tuple) -> bool:
if last in self.special_names:
abbrev_first = self._format_first_name(" ".join(first))
if abbrev_first == " ".join(self.special_names[last]["first"]):
return True
return False
def _split_name(self, name: str) -> dict:
name_dict = splitname(name)
first = name_dict.get("first", [])
von = name_dict.get("von", [])
last = name_dict.get("last", [])
if len(first) + len(von) <= 1 or not last:
return name_dict
words = first + von + last[:1]
last = tuple(last[1:])
prev_s_name = None
while words:
lw = words.pop()
last = tuple([lw, *last])
if self._is_special_name(words, last):
prev_s_name = self.special_names[last]
if last not in self.partial_special_names:
if prev_s_name:
return prev_s_name.copy()
return name_dict
return name_dict
def _join_name(self, name: dict) -> str:
"""
Concatenate the name represented as a dictionary of its bibtex field into a string.
Braces are added to the last name if it contains spaces. Braces are added to the the
first letter of the von part if it is capitalized.
"""
first = " ".join(name.get("first", []))
von = " ".join(name.get("von", []))
if len(von) > 1 and von[0].isupper():
von = f"{{{von[0]}}}{von[1:]}"
last = " ".join(name.get("last", []))
n = len(last) - 1
if " " in last:
matched_braces = self._match_braces(last)
if (
not matched_braces
or not matched_braces[-1][0] == 0
or not matched_braces[-1][1] == n
):
last = f"{{{last}}}"
jr = " ".join(name.get("jr", []))
previous = first != ""
if previous and von:
von = f" {von}"
previous = previous or von != ""
if previous and last:
last = f" {last}"
previous = previous or last != ""
if previous and jr:
jr = f" {jr}"
return f"{first}{von}{last}{jr}"
def _format_name(self, name: str) -> str:
name_dict = self._split_name(name)
if "first" in name_dict:
first_name = " ".join(name_dict["first"])
name_dict["first"] = [self._format_first_name(first_name)]
return self._join_name(name_dict)
def __call__(self, names: str) -> str:
return " and ".join(
self._format_name(name) for name in names.replace("\n", " ").split(" and ")
)
def format_names(*paths):
"""
Format names in the given bibliography file.
"""
dbs = []
for path in paths:
with open(path, "r") as f:
dbs.append(bp.load(f, _parser()))
formatter = NameFormatter(dbs)
for db in dbs:
for entry in db.entries:
if "author" in entry:
entry["author"] = formatter(entry["author"])
if "editor" in entry:
entry["editor"] = formatter(entry["editor"])
for db, path in zip(dbs, paths):
with open(path, "w") as f:
bp.dump(db, f, _writer(sorted_entries=False))
def check_bib(path):
"""
Check if the given bibliography is correctly formatted.
"""
# read bibliography
with open(path, "r") as f:
in_ = f.read()
db = _fixdb(bp.loads(in_, _parser()))
# write the bibliography
out = StringIO()
bp.dump(db, out, _writer())
return [
x for x in ndiff(in_.splitlines(), out.getvalue().splitlines()) if x[0] != " "
]
def run():
"""
Run the application.
"""
check_min_version()
parser = ArgumentParser(
prog="bibfmt", description="Autoformat and check bibliography."
)
subparsers = parser.add_subparsers(
metavar="command", dest="command", help="available subcommands", required=True
)
subparsers.add_parser(
"check", help="check whether bibliography is correctly formatted"
)
subparsers.add_parser("format", help="format the bibliography")
subparsers.add_parser("format-names", help="format names in the bibliography")
res = parser.parse_args()
if res.command == "format":
format_bib("krr.bib")
format_bib("procs.bib")
return 0
if res.command == "format-names":
format_names("krr.bib", "procs.bib")
return 0
assert res.command == "check"
diff = check_bib("krr.bib") + check_bib("procs.bib")
if diff:
for x in diff:
print(x, file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
sys.exit(run())