Skip to content

Instantly share code, notes, and snippets.

@FRidh
Last active May 2, 2017 16:43
Show Gist options
  • Save FRidh/1a5f4c02ac72d4bf1f69ea7bc2ad1fd5 to your computer and use it in GitHub Desktop.
Save FRidh/1a5f4c02ac72d4bf1f69ea7bc2ad1fd5 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
"""
Filter a .bib file with keys referred to in an .aux file. Print result to STDOUT.
"""
import argparse
import bibtexparser
import re
import itertools
import toolz
def determine_citation_keys_and_source(aux):
"""Determine the citation keys that we need to keep. Returns a list of citation keys"""
with open(aux, 'r') as f:
text = f.read()
regex = re.compile('\citation{(.*)}')
keys = regex.findall(text)
# The argument to \citation{} may contain multiple, comma-separated values.
keys = sorted(list(set(list(itertools.chain.from_iterable(map(lambda x: x.split(','), keys))))))
source = re.compile('bibdata{(.*)}').findall(text)[0]
return keys, source
def _get_entry(entries, key):
for entry in entries:
if entry.ID == key:
return entry
def main():
parser = argparse.ArgumentParser()
parser.add_argument('aux', type=str)
args = parser.parse_args()
keys, source = determine_citation_keys_and_source(args.aux)
with open(source, 'r') as f:
db = bibtexparser.load(f)
entries = db.get_entry_dict()
selection = [entries[key] for key in keys]
# Update db with selected entries
db.entries = selection
db._entries_dict = {} # Wipe this ugly piece of state
print(bibtexparser.dumps(db))
#with open(args.dest, 'w') as f:
#bibtexparser.dump(db, f)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment