Created
August 29, 2019 08:03
-
-
Save mmakowski/8d5e1a05e4ba1115351288e346f4c0dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import bibtexparser | |
from bibtexparser.bparser import BibTexParser | |
FIELDS_TO_KEEP = { | |
'*': ['title', 'author', 'year', 'pages', 'ENTRYTYPE', 'ID'], | |
'article': ['volume', 'number', 'journaltitle', 'shortjournal'] | |
} | |
def main(bibtex_file): | |
with open(bibtex_file) as f: | |
bib = bibtexparser.load(f, BibTexParser(ignore_nonstandard_types=False)) | |
for entry in bib.entries: | |
_add_year(entry) | |
_filter_fields(entry) | |
print(bibtexparser.dumps(bib)) | |
def _add_year(entry): | |
if 'year' not in entry and 'date' in entry: | |
entry['year'] = entry['date'][:4] | |
def _filter_fields(entry): | |
fields_to_keep = FIELDS_TO_KEEP['*'] + FIELDS_TO_KEEP.get(entry['ENTRYTYPE'], []) | |
for field in list(entry.keys()): | |
if field not in fields_to_keep: | |
del entry[field] | |
if __name__ == '__main__': | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment