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
37 def format_latex(text
):
38 # Get rid of matching dollar signs
39 text
= re
.sub(r
'\$([^\$]*)\$', r
'\1', text
)
40 return text
.replace('\mathcal', '').replace('{', '').replace('}', '').replace('\\', '').replace('~', ' ').replace('--', '–')
42 def format_field_span(type, value
):
43 return "<span class=bibentry_" + type + ">" + format_latex(value
) + "</span>"
45 def format_field(bibentry
, field
, pre
='', post
=''):
46 if field
in bibentry
.fields
:
47 if bibentry
.fields
[field
] != "":
48 return format_field_span(field
, pre
+ bibentry
.fields
[field
] + post
)
52 return ' '.join(' '.join(p
) for p
in (a
.first_names
, a
.middle_names
, a
.prelast_names
, a
.last_names
, a
.lineage_names
) if p
)
54 def format_authors(entry
):
55 return ", ".join([format_author(a
) for a
in entry
.persons
['author']])
58 def format_details_article(entry
):
60 where
= format_field(entry
, 'journal')
63 line
.append(format_field(entry
, 'pages', pre
='pp. '))
64 line
.append(format_field(entry
, 'volume', pre
='vol. ') + \
65 format_field(entry
, 'number', pre
='(', post
=')'))
66 line
.append(format_field(entry
, 'month', post
=' ') + \
67 format_field(entry
, 'year'))
68 line
.append(format_field(entry
, 'note'))
70 line
= filter(lambda l
: l
!= "", line
)
71 return [where
, ", ".join(line
)]
73 def format_details_inproceedings(entry
):
74 where
= format_field(entry
, 'booktitle')
77 line
.append(format_field(entry
, 'pages', pre
='pp. '))
78 line
.append(format_field(entry
, 'address'))
79 line
.append(format_field(entry
, 'month', post
=' ') + \
80 format_field(entry
, 'year'))
81 line
.append(format_field(entry
, 'isbn', pre
='ISBN '))
82 line
.append(format_field(entry
, 'note'))
84 line
= filter(lambda l
: l
!= "", line
)
85 return [where
, ", ".join(line
)]
87 def format_details_thesis(entry
):
89 line
.append(format_field(entry
, 'school'))
90 line
.append(format_field(entry
, 'month', post
=' ') + \
91 format_field(entry
, 'year'))
92 line
.append(format_field(entry
, 'note'))
94 line
= filter(lambda l
: l
!= "", line
)
95 return [", ".join(line
)]
97 def format_details_book(entry
):
99 line
.append(format_field(entry
, 'publisher'))
100 line
.append(format_field(entry
, 'isbn', pre
='ISBN '))
101 line
.append(format_field(entry
, 'month', post
=' ') + \
102 format_field(entry
, 'year'))
103 line
.append(format_field(entry
, 'note'))
105 line
= filter(lambda l
: l
!= "", line
)
106 return [", ".join(line
)]
108 def format_links(entry
):
109 doi
= format_field(entry
, 'doi', pre
='<a href="http://dx.doi.org/', post
='">[DOI]</a>')
110 webpdf
= format_field(entry
, 'webpdf', pre
='<a href="', post
='">[PDF]</a>')
111 weblink
= format_field(entry
, 'weblink', pre
='<a href="', post
='">[link]</a>')
112 url
= format_field(entry
, 'url', pre
='<a href="', post
='">[url]</a>')
113 webslides
= format_field(entry
, 'webslides', pre
='<a href="', post
='">[slides]</a>')
114 weberrata
= format_field(entry
, 'weberrata', pre
='<a href="',
115 post
='">[errata]</a>')
116 return " ".join([doi
, webpdf
, weblink
, url
, webslides
, weberrata
])
118 def format_entry(entry
):
120 lines
.append(format_field(entry
, 'title', pre
="<b>", post
="</b>"))
121 lines
.append(format_field_span('author', format_authors(entry
)))
123 if entry
.type=='article':
124 lines
.extend(format_details_article(entry
))
125 elif entry
.type=='inproceedings':
126 lines
.extend(format_details_inproceedings(entry
))
127 elif entry
.type=='book':
128 lines
.extend(format_details_book(entry
))
129 elif entry
.type in ['mastersthesis', 'phdthesis']:
130 lines
.extend(format_details_thesis(entry
))
132 lines
.append("Unknown type <b>'" + entry
.type + "'</b>")
134 lines
.append(format_field(entry
, 'webnote'))
135 lines
.append(format_links(entry
))
137 lines
= filter(lambda l
: l
!= "", lines
)
138 return "<br/>\n".join(lines
)
141 def entryDateSortKey(p
):
144 month2num
= { 'jan' : '01', 'feb' : '02', 'mar' : '03', \
145 'apr' : '04', 'may' : '05', 'jun' : '06', \
146 'jul' : '07', 'aug' : '08', 'sep' : '09', \
147 'oct' : '10', 'nov' : '11', 'dec' : '12'}
149 if not 'month' in e
.fields
:
150 return e
.fields
['year']
152 month
= e
.fields
['month'].lower()[0:3]
153 if month
in month2num
:
154 month
= month2num
[month
]
158 return e
.fields
['year'] + "-" + month
162 """Print usage text of this program"""
171 """.format(sys
.argv
[0]))
173 if __name__
== "__main__":
178 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hi:")
180 for opt
, arg
in opts
:
187 print("Unknown option '", opt
, "'.")
189 except getopt
.GetoptError
as e
:
190 print("Error parsing arguments:", e
)
194 print("You need to specify a bibfile")
196 sys
.exit(os
.EX_USAGE
)
199 from pybtex
.database
.input import bibtex
200 parser
= bibtex
.Parser()
202 from pybtex
.style
.formatting
.unsrt
import Style
204 bib_data
= parser
.parse_file(bibfile
)
205 entries
= bib_data
.entries
207 years
= list(set([ b
.fields
['year'] for b
in entries
.values() ]))
208 years
.sort(reverse
=True)
212 print("<h2>" + year
+ "</h2>")
214 iteritems
= list(entries
.items())
215 iteritems
.sort(key
=entryDateSortKey
, reverse
=True)
216 for key
, entry
in iteritems
:
218 if entry
.fields
['year'] != year
:
221 print("<div class=bibentry>")
222 print("<a class=bibentry_key id=" + key
+ ">[" + key
+ "]</a><br/>")
224 e
= format_entry(entry
)