Skip to content

Instantly share code, notes, and snippets.

@mommi84
Created February 1, 2018 15:41
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 mommi84/6f07d08f9840650417faea8267ee37c7 to your computer and use it in GitHub Desktop.
Save mommi84/6f07d08f9840650417faea8267ee37c7 to your computer and use it in GitHub Desktop.
Script to convert BibTeX files into RASH bibliography.
#!/usr/bin/env python
import sys
import bibtexparser
reload(sys)
sys.setdefaultencoding("utf-8")
def fd(s):
if s[-1] != '.':
s += '.'
return s
def parse_bib(bib):
with open(bib) as bibtex_file:
bibtex_str = bibtex_file.read()
bib_database = bibtexparser.loads(bibtex_str)
print "{} bib entries loaded.".format(len(bib_database.entries))
for entry in bib_database.entries:
print '<li id="{}" about="{}-expression" typeof="fabio:ResearchPaper" property="frbr:realizationOf" resource="{}-work" role="doc-biblioentry">'.format(entry['ID'], entry['ID'], entry['ID'])
try:
if 'journal' not in entry:
venue = entry['booktitle']
else:
venue = entry['journal']
except KeyError:
venue = "UNKNOWN"
authors = entry['author'].replace('{', '').replace('}', '').replace('\n', ' ').split(' and ')
auth = list()
for a in authors:
while '\\' in a:
i = a.index('\\')
a = a[:i] + a[i+2:]
if ',' in a:
aa = a.split(', ')
a = aa[0] + " " + aa[1][0] + "."
auth.append(a)
print '\t<p about="{}-expression" property="dcterms:bibliographicCitation">{} ({}). {} {}</p>'.format(entry['ID'], ", ".join(auth), entry['year'], fd(entry['title']), fd(venue))
print '</li>'
parse_bib('biblio.bib')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment