Skip to content

Instantly share code, notes, and snippets.

@hexylena
Last active May 6, 2024 11:12
Show Gist options
  • Save hexylena/88841f0f5d566bca0d01e0940d9207a1 to your computer and use it in GitHub Desktop.
Save hexylena/88841f0f5d566bca0d01e0940d9207a1 to your computer and use it in GitHub Desktop.
Replace \cite{10.1234/some-key} with actual citation, doi2bib must be installed.
#!/usr/bin/env python
# license: cc0/public domain
import re
import sys
import subprocess
input_file = sys.argv[1]
output_file = input_file.replace('.tex', '.bib')
with open(input_file, "r") as f:
content = f.read()
# Find all DOI references within \cite{}
dois = re.findall(r"\\cite\{(10[\w./()-]+)\}", content)
bibs = ""
for doi in dois:
# Run doi2bib command
doi2bib_output = subprocess.check_output(["doi2bib", doi]).decode('utf-8').strip()
print(doi)
print(doi2bib_output)
if doi2bib_output:
# Extract the citation key from the doi2bib output
citation_key = re.search(r"@[\w]+{([\w:_/]+),", doi2bib_output).group(1)
bibs += doi2bib_output
else:
print("Failed to replace " + doi)
continue
print(citation_key)
# Replace \cite{$doi} with the citation key
content = content.replace("\\cite{" + doi + "}", "\\cite{" + citation_key + "}")
with open(input_file, 'w') as f:
f.write(content)
with open(output_file, "a") as f:
f.write(bibs)
# consider: bibtex-tidy to cleanup after.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment