-
Notifications
You must be signed in to change notification settings - Fork 1
/
bibtex-unused.py
executable file
·62 lines (52 loc) · 1.69 KB
/
bibtex-unused.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
#!/usr/bin/env python
#
# Script to check whether, in a latex file using the bibitem-\cite reference
# management technique, there are bibliography references that are never cited
# in the text. Idea stolen from citations.pl by "SIEi+e, IAC - sinfin [at] iac.es"
import re
import sys
def main():
args = sys.argv[1:]
if len(args) == 1:
bibfile = args[0] + ".bib"
auxfile = args[0] + ".aux"
elif len(args) == 2:
bibfile = args[0]
auxfile = args[1]
else:
print >>sys.stderr, "usage: bibtex-unused.py NAME -- or -- bibtex-unused.py BIBFILE AUXFILE"
sys.exit(1)
cites = set()
unused = []
pat = re.compile("\s*\\\\bibcite\{([^\}]+)\}")
aux = open(auxfile, "r")
for line in aux:
m = pat.match(line)
if m is not None:
cites.add(m.group(1))
aux.close()
pat = re.compile("\s*@[^\{\(]+[\{\(]([^,]+),")
bib = open(bibfile, "r")
usedc = 0
for line in bib:
m = pat.match(line)
if m is not None:
key = m.group(1).strip().lower()
if key != "string":
if key not in cites:
unused.append(key)
else:
usedc += 1
bib.close()
if len(unused) == 0:
if usedc == len(cites):
print "%d citations found; all are used!" % usedc
else:
print "%d citations found in %s, %d citations found in %s (all used)" % \
(len(cites), auxfile, usedc, bibfile)
else:
print "%d unused citations found (plus %d used):" % (len(unused), len(cites))
for key in unused:
print key
if __name__ == '__main__':
main()