-
Notifications
You must be signed in to change notification settings - Fork 4
/
bib2markdown.py
executable file
·47 lines (36 loc) · 1.16 KB
/
bib2markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# Usage: python bib2markdown.py infile.bib > outfile.md
import bibtexparser
import sys
with open(sys.argv[1]) as bibtex_file:
bibtex_str = bibtex_file.read()
bib_database = bibtexparser.loads(bibtex_str)
print "References"
print "=========="
for entry in bib_database.entries:
# this is an HTML anchor so we can refer to the reference entry in markdown as:
# [Foo2017](../REFERENCES#Foo2017)
print '<a name="{}"></a>'.format(entry['ID'])
# bibtex separates authors with ' and '
authors = entry['author'].replace(' and ', ', ')
# these keys are (supposed to be) in every entry
print '**{}** {}. {}. _{}_.'.format(
authors,
entry['year'],
entry['title'],
entry['journal']),
# not every entry has these
if 'volume' in entry:
print '**{}**'.format(entry['volume']),
if 'number' in entry:
print '({}):'.format(entry['number']),
else:
print ':',
else:
if 'number' in entry:
print '{}:'.format(entry['number']),
if 'pages' in entry:
print '{}.'.format(entry['pages']),
# should appear in every entry:
print '[[{}](http://doi.org/{})]'.format(entry['doi'],entry['doi']),
print '`[id:{}]`\n'.format(entry['ID'])