]>
git.sthu.org Git - shutils.git/blob - bib2html.py
7a1b7d93ce5bb5f4a43667941ec1cc4e701a8719
2 """Creates a webpage with all entries of a .bib file"""
6 __author__
= "Stefan Huber"
7 __email__
= "shuber@sthu.org"
8 __copyright__
= "Copyright 2013, Stefan Huber"
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
21 # The above copyright notice and this permission notice shall be
22 # included in all copies or substantial portions of the Software.
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.
34 import os
, sys
, getopt
, re
35 import dateutil
.parser
38 def format_latex(text
):
39 # Get rid of matching dollar signs
40 text
= re
.sub(r
'\$([^\$]*)\$', r
'\1', text
)
56 for a
, b
in subst
.items():
57 text
= text
.replace(a
, b
)
61 def format_field_span(type, value
):
62 return "<span class=bibentry_" + type + ">" + format_latex(value
) + "</span>"
64 def format_field(bibentry
, field
, pre
='', post
=''):
65 if field
in bibentry
.fields
:
66 if bibentry
.fields
[field
] != "":
67 return format_field_span(field
, pre
+ bibentry
.fields
[field
] + post
)
71 return ' '.join(' '.join(p
) for p
in (a
.first_names
, a
.middle_names
, a
.prelast_names
, a
.last_names
, a
.lineage_names
) if p
)
73 def format_authors(entry
):
74 return ", ".join([format_author(a
) for a
in entry
.persons
['author']])
77 def format_details_article(entry
):
79 where
= format_field(entry
, 'journal')
82 line
.append(format_field(entry
, 'pages', pre
='pp. '))
83 line
.append(format_field(entry
, 'volume', pre
='vol. ') + \
84 format_field(entry
, 'number', pre
='(', post
=')'))
85 line
.append(format_field(entry
, 'month', post
=' ') + \
86 format_field(entry
, 'year'))
87 line
.append(format_field(entry
, 'note'))
89 line
= filter(lambda l
: l
!= "", line
)
90 return [where
, ", ".join(line
)]
92 def format_details_inproceedings(entry
):
93 where
= format_field(entry
, 'booktitle')
96 line
.append(format_field(entry
, 'pages', pre
='pp. '))
97 line
.append(format_field(entry
, 'address'))
98 line
.append(format_field(entry
, 'month', post
=' ') + \
99 format_field(entry
, 'year'))
100 line
.append(format_field(entry
, 'isbn', pre
='ISBN '))
101 line
.append(format_field(entry
, 'note'))
103 line
= filter(lambda l
: l
!= "", line
)
104 return [where
, ", ".join(line
)]
106 def format_details_incollection(entry
):
107 where
= format_field(entry
, 'booktitle')
110 line
.append(format_field(entry
, 'publisher'))
111 line
.append(format_field(entry
, 'pages', pre
='pp. '))
112 line
.append(format_field(entry
, 'address'))
113 line
.append(format_field(entry
, 'month', post
=' ') + \
114 format_field(entry
, 'year'))
115 line
.append(format_field(entry
, 'isbn', pre
='ISBN '))
116 line
.append(format_field(entry
, 'note'))
118 line
= filter(lambda l
: l
!= "", line
)
119 return [where
, ", ".join(line
)]
121 def format_details_thesis(entry
):
123 line
.append(format_field(entry
, 'school'))
124 line
.append(format_field(entry
, 'month', post
=' ') + \
125 format_field(entry
, 'year'))
126 line
.append(format_field(entry
, 'note'))
128 line
= filter(lambda l
: l
!= "", line
)
129 return [", ".join(line
)]
131 def format_details_book(entry
):
133 line
.append(format_field(entry
, 'publisher'))
134 line
.append(format_field(entry
, 'isbn', pre
='ISBN '))
135 line
.append(format_field(entry
, 'month', post
=' ') + \
136 format_field(entry
, 'year'))
137 line
.append(format_field(entry
, 'note'))
139 line
= filter(lambda l
: l
!= "", line
)
140 return [", ".join(line
)]
142 def format_details_patent(entry
):
144 line
.append(format_field(entry
, 'number', pre
='Pat. '))
145 line
.append(format_field(entry
, 'month', post
=' ') + \
146 format_field(entry
, 'year'))
147 line
.append(format_field(entry
, 'note'))
149 line
= filter(lambda l
: l
!= "", line
)
150 return [", ".join(line
)]
152 def format_links(entry
):
153 doi
= format_field(entry
, 'doi', pre
='<a href="http://dx.doi.org/', post
='">[DOI]</a>')
154 webpdf
= format_field(entry
, 'webpdf', pre
='<a href="', post
='">[PDF]</a>')
155 weblink
= format_field(entry
, 'weblink', pre
='<a href="', post
='">[link]</a>')
156 url
= format_field(entry
, 'url', pre
='<a href="', post
='">[url]</a>')
157 webslides
= format_field(entry
, 'webslides', pre
='<a href="', post
='">[slides]</a>')
158 weberrata
= format_field(entry
, 'weberrata', pre
='<a href="',
159 post
='">[errata]</a>')
160 return " ".join([doi
, webpdf
, weblink
, url
, webslides
, weberrata
])
162 def format_entry(entry
):
164 lines
.append(format_field(entry
, 'title', pre
="<b>", post
="</b>"))
165 lines
.append(format_field_span('author', format_authors(entry
)))
167 if entry
.type=='article':
168 lines
.extend(format_details_article(entry
))
169 elif entry
.type=='inproceedings':
170 lines
.extend(format_details_inproceedings(entry
))
171 elif entry
.type=='incollection':
172 lines
.extend(format_details_incollection(entry
))
173 elif entry
.type=='book':
174 lines
.extend(format_details_book(entry
))
175 elif entry
.type=='patent':
176 lines
.extend(format_details_patent(entry
))
177 elif entry
.type in ['mastersthesis', 'phdthesis']:
178 lines
.extend(format_details_thesis(entry
))
180 lines
.append("Unknown type <b>'" + entry
.type + "'</b>")
182 lines
.append(format_field(entry
, 'webnote'))
183 lines
.append(format_links(entry
))
185 lines
= filter(lambda l
: l
!= "", lines
)
186 return "<br/>\n".join(lines
)
189 def entryDateSortKey(p
):
192 if 'date' in e
.fields
:
193 return e
.fields
['date']
195 month2num
= { 'jan' : '01', 'feb' : '02', 'mar' : '03', \
196 'apr' : '04', 'may' : '05', 'jun' : '06', \
197 'jul' : '07', 'aug' : '08', 'sep' : '09', \
198 'oct' : '10', 'nov' : '11', 'dec' : '12'}
200 if not 'month' in e
.fields
:
201 return e
.fields
['year']
203 month
= e
.fields
['month'].lower()[0:3]
204 if month
in month2num
:
205 month
= month2num
[month
]
209 return e
.fields
['year'] + "-" + month
213 if 'year' in e
.fields
:
214 return e
.fields
['year']
216 if 'date' in e
.fields
:
217 dt
= dateutil
.parser
.isoparse(e
.fields
['date'])
224 """Print usage text of this program"""
233 """.format(sys
.argv
[0]))
235 if __name__
== "__main__":
240 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hi:")
242 for opt
, arg
in opts
:
249 print("Unknown option '", opt
, "'.")
251 except getopt
.GetoptError
as e
:
252 print("Error parsing arguments:", e
)
256 print("You need to specify a bibfile")
258 sys
.exit(os
.EX_USAGE
)
261 from pybtex
.database
.input import bibtex
262 parser
= bibtex
.Parser()
264 from pybtex
.style
.formatting
.unsrt
import Style
266 bib_data
= parser
.parse_file(bibfile
)
267 entries
= bib_data
.entries
269 years
= list(set([entryGetYear(e
) for e
in entries
.values()]))
270 years
.sort(reverse
=True)
274 print("<h2>" + year
+ "</h2>")
276 iteritems
= list(entries
.items())
277 iteritems
.sort(key
=entryDateSortKey
, reverse
=True)
278 for key
, entry
in iteritems
:
280 if entryGetYear(entry
) != year
:
283 print("<div class=bibentry>")
284 print("<a class=bibentry_key id={}>[{}]</a><br/><span class=bibentry_type>{}</span><br/>".format(key
, key
, entry
.type))
285 e
= format_entry(entry
)