-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_bib.py
executable file
·208 lines (167 loc) · 6.2 KB
/
generate_bib.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
# encoding: utf-8
"""
File: generate_bib.py
Author: Florian Wagner <[email protected]>
Description: Create RST reference list from bibtex file.
Created on: 2015-01-17
"""
from __future__ import print_function
import re
from collections import defaultdict
from operator import itemgetter
from bibtexparser import load
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import convert_to_unicode
gimli = re.compile(re.escape('pygimli'), re.IGNORECASE)
def parse_bib(fname):
""" Read bibtex file and sort by year. """
with open(fname) as bibfile:
parser = BibTexParser()
parser.customization = convert_to_unicode
bp = load(bibfile, parser=parser)
references = bp.get_entry_list()
references.sort(key=lambda x: x['year'], reverse=True)
refs = defaultdict(list)
for r in references:
refs[r['year']].append(r)
refsbyyear = []
for year in refs.keys():
yearlist = sorted(refs[year], key=itemgetter("author"), reverse=True)
sortedlist = []
for entry in yearlist:
if entry["author"].startswith("Wagner") or entry["author"].startswith("F. M."):
sortedlist.insert(0, entry)
else:
sortedlist.append(entry)
refsbyyear.append((year, sortedlist))
# Newest year first
refsbyyear.sort(key=lambda x: x[0], reverse=True)
return refsbyyear
def subs(string):
"""Perform some string substitutions."""
string = string.replace("CO2", "CO\ :sub:`2`")
string = string.replace("1st", "1\ :sup:`st`")
string = string.replace("2nd", "2\ :sup:`nd`")
string = string.replace("3rd", "3\ :sup:`rd`")
string = re.sub(r"(\d+)th", r"\1\ :sup:`th`", string)
string = gimli.sub('`pyGIMLi\ <http://www.pygimli.org/>`_', string)
return string
def write_entry(entry, fhandle):
""" Write beginning on entry. """
if " and " in entry["author"]:
authors = entry["author"].split(" and ")
ands = True
else:
authors = entry["author"].split(',')
ands = False
for i, author in enumerate(authors):
if ands:
name = [n.strip() for n in author.split(",")]
else:
name = [n.strip() for n in reversed(author.split())]
if "Wagner" in name:
fhandle.write("**%s, %s**" % (name[0], "F\. M\."))
else:
fhandle.write("%s, %s" % (name[0], name[1][0] + "\."))
if i < len(authors) - 1:
fhandle.write(", ")
else:
fhandle.write(" (" + entry["year"] + "): ")
fhandle.write(subs(entry["title"]) + ". ")
articles = parse_bib("content/articles.bib")
conference = parse_bib("content/conference.bib")
link = " :raw-html:`<a target=\"_blank\" href=\"/javascript/pdfjs/web/viewer.html?file=%s#pagemode=thumbs\"><i class=\"icon-%s\" title=\"Download PDF\" style=\"color:green\"></i></a>`"
link_ex = " :raw-html:`<a target=\"_blank\" href=\"%s\"><i class=\"icon-%s\" style=\"color:green\" title=\"Download PDF\"></i></a>`"
#citations = " :raw-html:`<object height=\"50\" data=\"http://api.elsevier.com/content/abstract/citation-count?doi=%s&httpAccept=text/html&apiKey=557b7437b48874840f9cb4d8b0650079\"></object>`"
# Write main file
f = open("content/pages/publications.rst", "w")
f.write("""
Publications
============
:slug: publications
.. role:: raw-html(raw)
:format: html
Journal articles
----------------
""")
num_articles = 0
for year in articles:
f.write(year[0] + "\n")
f.write("^^^^\n\n")
for article in year[1]:
try:
write_entry(article, f)
except:
print(article)
raise
f.write("*" + article["journal"] + "*")
if "volume" in article:
f.write(", ")
f.write(article["volume"] + ", ")
f.write(article["pages"])
else:
print("No volume info found for", article)
if "doi" in article:
if len(article["doi"]) > 3:
f.write(", ")
f.write("`DOI:" + article["doi"] + " <https://doi.org/" + article["doi"] + ">`_")
if "note" in article:
f.write(" (%s)" % article["note"])
f.write(". ")
if "url" in article and len(article["url"]) > 10:
if article["url"].lower().endswith("pdf"):
icon = "file-pdf-o"
else:
icon = "external-link"
if not article["url"].startswith("."):
f.write(link_ex % (article["url"], icon))
else:
f.write(link % (article["url"][1:], icon))
#if len(article["doi"]) > 3:
#f.write(citations % article["doi"])
f.write("\n\n")
num_articles += 1
f.write("Conference contributions\n")
f.write("------------------------\n\n")
num_conference = 0
for year in conference:
f.write(year[0] + "\n")
f.write("^^^^\n\n")
for article in year[1]:
try:
write_entry(article, f)
except:
print(article)
raise
if 'booktitle' in article:
f.write(subs(article["booktitle"]))
elif 'series' in article:
f.write(subs(article["series"]))
else:
f.write("*Conference Proceeding*")
if 'doi' in article:
f.write(", `DOI:" + article["doi"] + " <https://doi.org/" +
article["doi"] + ">`_")
if "note" in article:
f.write(" (%s)" % article["note"])
f.write(".")
if "url" in article and len(article["url"]) > 10:
if article["url"].lower().endswith(".pdf"):
icon = "file-pdf-o"
else:
icon = "external-link"
if not article["url"].startswith("."):
f.write(link_ex % (article["url"], icon))
else:
f.write(link % (article["url"][1:], icon))
f.write("\n\n")
num_conference += 1
print(
"Wrote %d journals articles and %d conference contributions to publications.rst."
% (num_articles, num_conference))
f.write("""
.. class:: sidenote
:icon:`file-pdf-o` A PDF version of my CV including this list of publications can be downloaded `here </static/cv_fwagner.pdf>`_.
""")
f.close()