Skip to content

Instantly share code, notes, and snippets.

@mommi84 mommi84/bib4rash.py
Created Feb 1, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.