-
Notifications
You must be signed in to change notification settings - Fork 0
/
references.py
executable file
·50 lines (47 loc) · 1.69 KB
/
references.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
#!/usr/bin/env python3
import argparse
import re
# Match '@' followed by one or more letters, four numbers,
# then zero or more letters.
regex = '@[a-zA-Z]+\d{4}[a-zA-Z]*'
# Pandoc keys begin with '@'.
key_prefix = '@'
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('target')
parser.add_argument('bibliography')
parser.add_argument('--unused', '-u', action='store_true')
args = parser.parse_args()
target = args.target
bibliography = args.bibliography
with open(bibliography) as bib_file:
set_of_keys =\
{record['id'] for record in BibTexParser(bib_file.read(), ignore_nonstandard_types=False).entries}
with open(target) as file:
target_string = file.read()
list_of_references = re.findall(regex, target_string)
# The set needs to take out the beginning of pandoc keys.
set_of_references =\
{x.strip().lstrip(key_prefix) for x in list_of_references}
if args.unused:
unused = [x for x in set_of_keys if x not in set_of_references]
if unused:
print(
'The following keys in {} were not used in {}:'.format(
bibliography,
target
)
)
for reference in sorted(unused):
print(reference)
else:
missing = [x for x in set_of_references if x not in set_of_keys]
if missing:
print(
'The following references in {} were missing from {}:'.format(
target,
bibliography
)
)
for reference in sorted(missing):
print(reference)