Skip to content

Instantly share code, notes, and snippets.

@cgranade
Created June 6, 2017 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgranade/97fba23f087c50343ce83615a3d72611 to your computer and use it in GitHub Desktop.
Save cgranade/97fba23f087c50343ce83615a3d72611 to your computer and use it in GitHub Desktop.
BibTeX textconv plugin for Git
# 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