Created
February 1, 2018 15:41
-
-
Save mommi84/6f07d08f9840650417faea8267ee37c7 to your computer and use it in GitHub Desktop.
Script to convert BibTeX files into RASH bibliography.
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
#!/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