]> git.sthu.org Git - shutils.git/blob - bib2html.py
vim: update
[shutils.git] / bib2html.py
1 #!/usr/bin/env python2
2 """Creates a webpage with all entries of a .bib file"""
3
4 __version__ = "1.0"
5
6 __author__ = "Stefan Huber"
7 __email__ = "shuber@sthu.org"
8 __copyright__ = "Copyright 2013, Stefan Huber"
9
10 __license__ = "MIT"
11
12 # Permission is hereby granted, free of charge, to any person
13 # obtaining a copy of this software and associated documentation
14 # files (the "Software"), to deal in the Software without
15 # restriction, including without limitation the rights to use,
16 # copy, modify, merge, publish, distribute, sublicense, and/or sell
17 # copies of the Software, and to permit persons to whom the
18 # Software is furnished to do so, subject to the following
19 # conditions:
20 #
21 # The above copyright notice and this permission notice shall be
22 # included in all copies or substantial portions of the Software.
23 #
24 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
26 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
28 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 # OTHER DEALINGS IN THE SOFTWARE.
32
33
34 import os, sys, getopt
35
36
37 def format_latex(text):
38 return text.replace('{', '').replace('}', '').replace('\\', '')
39
40 def format_field_span(type, value):
41 return "<span class=bibentry_" + type + ">" + format_latex(value) + "</span>"
42
43 def format_field(bibentry, field, pre='', post=''):
44 if field in bibentry.fields:
45 if bibentry.fields[field] != "":
46 return format_field_span(field, pre + bibentry.fields[field] + post)
47 return ""
48
49 def format_author(a):
50 return ' '.join(' '.join(p) for p in (a.first(), a.middle(), a.prelast(), a.last(), a.lineage()) if p)
51
52 def format_authors(entry):
53 return ", ".join([format_author(a) for a in entry.persons['author']])
54
55
56 def format_details_article(entry):
57
58 where = format_field(entry, 'journal')
59
60 line = []
61 line.append(format_field(entry, 'pages', pre='pp. '))
62 line.append(format_field(entry, 'volume', pre='vol. ') + \
63 format_field(entry, 'number', pre='(', post=')'))
64 line.append(format_field(entry, 'month', post=' ') + \
65 format_field(entry, 'year'))
66 line = filter(lambda l: l != "", line)
67
68 return [where, ", ".join(line)]
69
70 def format_details_inproceedings(entry):
71 where = format_field(entry, 'booktitle')
72
73 line = []
74 line.append(format_field(entry, 'pages', pre='pp. '))
75 line.append(format_field(entry, 'address'))
76 line.append(format_field(entry, 'month', post=' ') + \
77 format_field(entry, 'year'))
78 line = filter(lambda l: l != "", line)
79 return [where, ", ".join(line)]
80
81 def format_details_thesis(entry):
82 line = []
83 line.append(format_field(entry, 'school'))
84 line.append(format_field(entry, 'month', post=' ') + \
85 format_field(entry, 'year'))
86 line = filter(lambda l: l != "", line)
87 return [", ".join(line)]
88
89 def format_details_book(entry):
90 line = []
91 line.append(format_field(entry, 'publisher'))
92 line.append(format_field(entry, 'isbn', pre='ISBN '))
93 line.append(format_field(entry, 'month', post=' ') + \
94 format_field(entry, 'year'))
95 line = filter(lambda l: l != "", line)
96 return [", ".join(line)]
97
98 def format_links(entry):
99 doi = format_field(entry, 'doi', pre='<a href="http://dx.doi.org/', post='">[DOI]</a>')
100 webpdf = format_field(entry, 'webpdf', pre='<a href="', post='">[PDF]</a>')
101 weblink = format_field(entry, 'weblink', pre='<a href="', post='">[link]</a>')
102 webslides = format_field(entry, 'webslides', pre='<a href="', post='">[slides]</a>')
103 weberrata = format_field(entry, 'weberrata', pre='<a href="',
104 post='">[errata]</a>')
105 return " ".join([doi, webpdf, weblink, webslides, weberrata])
106
107 def format_entry(entry):
108 lines = []
109 lines.append(format_field(entry, 'title', pre="<b>", post="</b>"))
110 lines.append(format_field_span('author', format_authors(entry)))
111
112 if entry.type=='article':
113 lines.extend(format_details_article(entry))
114 elif entry.type=='inproceedings':
115 lines.extend(format_details_inproceedings(entry))
116 elif entry.type=='book':
117 lines.extend(format_details_book(entry))
118 elif entry.type in ['mastersthesis', 'phdthesis']:
119 lines.extend(format_details_thesis(entry))
120 else:
121 lines.append("Unknown type <b>'" + entry.type + "'</b>")
122
123 lines.append(format_field(entry, 'webnote'))
124 lines.append(format_links(entry))
125
126 lines = filter(lambda l: l != "", lines)
127 return "<br/>\n".join(lines)
128
129
130 def entryCompareDate(p1, p2):
131 k1, e1 = p1
132 k2, e2 = p2
133
134
135 def toStr(e):
136 month2num = { 'jan' : '01', 'feb' : '02', 'mar' : '03', \
137 'apr' : '04', 'may' : '05', 'jun' : '06', \
138 'jul' : '07', 'aug' : '08', 'sep' : '09', \
139 'oct' : '10', 'nov' : '11', 'dec' : '12'}
140 month = e.fields['month'].lower()[0:3]
141 if month in month2num:
142 month = month2num[month]
143 else:
144 month = ""
145 return e.fields['year'] + "-" + month
146
147 return cmp(toStr(e1), toStr(e2))
148
149
150 def usage():
151 """Print usage text of this program"""
152
153 print("""Usage:
154 {0} -i FILE
155 {0} -h
156
157 OPTIONS:
158 -h print this text
159 -i .bib file
160 """.format(sys.argv[0]))
161
162 if __name__ == "__main__":
163
164 bibfile = None
165
166 try:
167 opts, args = getopt.getopt(sys.argv[1:], "hi:")
168
169 for opt, arg in opts:
170 if opt == "-h":
171 usage()
172 sys.exit(os.EX_OK)
173 elif opt == "-i":
174 bibfile = arg
175 else:
176 print("Unknown option '", opt, "'.")
177
178 except getopt.GetoptError as e:
179 print("Error parsing arguments:", e)
180 usage()
181
182 if bibfile == None:
183 print("You need to specify a bibfile")
184 usage()
185 sys.exit(os.EX_USAGE)
186
187
188 from pybtex.database.input import bibtex
189 parser = bibtex.Parser()
190
191 from pybtex.style.formatting.unsrt import Style
192
193 bib_data = parser.parse_file(bibfile)
194 entries = bib_data.entries
195
196 years = list(set([ b.fields['year'] for b in entries.values() ]))
197 years.sort(reverse=True)
198
199 for year in years:
200
201 print("<h2>" + year + "</h2>")
202
203 iteritems = list(entries.iteritems())
204 iteritems.sort(cmp=entryCompareDate, reverse=True)
205 for key, entry in iteritems:
206
207 if entry.fields['year'] != year:
208 continue
209
210 print("<div class=bibentry>")
211 print("<a class=bibentry_key id=" + key + ">[" + key + "]</a><br/>")
212
213 print(format_entry(entry).encode('utf8'))
214
215 print("</div>\n")
216