Created
June 6, 2017 08:34
-
-
Save cgranade/97fba23f087c50343ce83615a3d72611 to your computer and use it in GitHub Desktop.
BibTeX textconv plugin for Git
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
# 2017 Christopher Granade. This is a silly experiment, so I am placing it | |
# in the public domain. No rights reserved. | |
# Use with the following .gitconfig: | |
# [diff "bibtex"] | |
# textconv=python \"path/to/pprint_bibtex.py\" | |
# and the following .gitattributes: | |
# *.bib diff=bibtex | |
# Have fun! | |
import bibtexparser | |
import pprint | |
import sys | |
def entry_key(entry_tuple): | |
entry_id, entry_data = entry_tuple | |
return entry_id | |
def format_entry(entry_data): | |
entry_id = entry_data['ID'] | |
entry_type = entry_data['ENTRYTYPE'] | |
return "@{entry_type}{{{entry_id},\n {entry_contents}\n}}\n".format( | |
entry_id=entry_id, | |
entry_type=entry_type, | |
entry_contents=",\n ".join( | |
"{}={{{}}}".format(key, value.encode('utf8')) | |
for key, value in entry_data.items() | |
if key not in ('ID', 'ENTRYTYPE') | |
) | |
) | |
if __name__ == "__main__": | |
with open(sys.argv[1]) as f: | |
data = bibtexparser.load(f) | |
for entry_id, entry_data in sorted(data.entries_dict.items(), key=entry_key): | |
print(format_entry(entry_data)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment