X-Git-Url: http://git.sthu.org/?a=blobdiff_plain;ds=sidebyside;f=bib2html.py;h=7a1b7d93ce5bb5f4a43667941ec1cc4e701a8719;hb=d7b49a5434e2001d5e04ab8d9ea290982933d037;hp=28abd58ecbfebd88bfcdc56099a1a33619139f13;hpb=3ac17777f987e1c78ea411f149e450015da3dfc4;p=shutils.git
diff --git a/bib2html.py b/bib2html.py
index 28abd58..7a1b7d9 100755
--- a/bib2html.py
+++ b/bib2html.py
@@ -1,7 +1,7 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
"""Creates a webpage with all entries of a .bib file"""
-__version__ = "1.0"
+__version__ = "1.1"
__author__ = "Stefan Huber"
__email__ = "shuber@sthu.org"
@@ -31,22 +31,44 @@ __license__ = "MIT"
# OTHER DEALINGS IN THE SOFTWARE.
-import os, sys, getopt
+import os, sys, getopt, re
+import dateutil.parser
def format_latex(text):
- return text.replace('{', '').replace('}', '').replace('\\', '')
+ # Get rid of matching dollar signs
+ text = re.sub(r'\$([^\$]*)\$', r'\1', text)
+
+ # Replace text
+ subst = {
+ '\\"a': 'ä',
+ '\\"o': 'ö',
+ '\\"u': 'u',
+ '\\mathcal': '',
+ '{': '',
+ '}': '',
+ '\\': '',
+ '~': ' ',
+ '---': 'â',
+ '--': 'â',
+ }
+
+ for a, b in subst.items():
+ text = text.replace(a, b)
+
+ return text
def format_field_span(type, value):
return "" + format_latex(value) + ""
def format_field(bibentry, field, pre='', post=''):
if field in bibentry.fields:
- return format_field_span(field, pre + bibentry.fields[field] + post)
+ if bibentry.fields[field] != "":
+ return format_field_span(field, pre + bibentry.fields[field] + post)
return ""
def format_author(a):
- return ' '.join(' '.join(p) for p in (a.first(), a.middle(), a.prelast(), a.last(), a.lineage()) if p)
+ return ' '.join(' '.join(p) for p in (a.first_names, a.middle_names, a.prelast_names, a.last_names, a.lineage_names) if p)
def format_authors(entry):
return ", ".join([format_author(a) for a in entry.persons['author']])
@@ -62,8 +84,9 @@ def format_details_article(entry):
format_field(entry, 'number', pre='(', post=')'))
line.append(format_field(entry, 'month', post=' ') + \
format_field(entry, 'year'))
- line = filter(lambda l: l != "", line)
+ line.append(format_field(entry, 'note'))
+ line = filter(lambda l: l != "", line)
return [where, ", ".join(line)]
def format_details_inproceedings(entry):
@@ -74,6 +97,24 @@ def format_details_inproceedings(entry):
line.append(format_field(entry, 'address'))
line.append(format_field(entry, 'month', post=' ') + \
format_field(entry, 'year'))
+ line.append(format_field(entry, 'isbn', pre='ISBN '))
+ line.append(format_field(entry, 'note'))
+
+ line = filter(lambda l: l != "", line)
+ return [where, ", ".join(line)]
+
+def format_details_incollection(entry):
+ where = format_field(entry, 'booktitle')
+
+ line = []
+ line.append(format_field(entry, 'publisher'))
+ line.append(format_field(entry, 'pages', pre='pp. '))
+ line.append(format_field(entry, 'address'))
+ line.append(format_field(entry, 'month', post=' ') + \
+ format_field(entry, 'year'))
+ line.append(format_field(entry, 'isbn', pre='ISBN '))
+ line.append(format_field(entry, 'note'))
+
line = filter(lambda l: l != "", line)
return [where, ", ".join(line)]
@@ -82,6 +123,8 @@ def format_details_thesis(entry):
line.append(format_field(entry, 'school'))
line.append(format_field(entry, 'month', post=' ') + \
format_field(entry, 'year'))
+ line.append(format_field(entry, 'note'))
+
line = filter(lambda l: l != "", line)
return [", ".join(line)]
@@ -91,16 +134,30 @@ def format_details_book(entry):
line.append(format_field(entry, 'isbn', pre='ISBN '))
line.append(format_field(entry, 'month', post=' ') + \
format_field(entry, 'year'))
+ line.append(format_field(entry, 'note'))
+
+ line = filter(lambda l: l != "", line)
+ return [", ".join(line)]
+
+def format_details_patent(entry):
+ line = []
+ line.append(format_field(entry, 'number', pre='Pat. '))
+ line.append(format_field(entry, 'month', post=' ') + \
+ format_field(entry, 'year'))
+ line.append(format_field(entry, 'note'))
+
line = filter(lambda l: l != "", line)
return [", ".join(line)]
def format_links(entry):
+ doi = format_field(entry, 'doi', pre='[DOI]')
webpdf = format_field(entry, 'webpdf', pre='[PDF]')
weblink = format_field(entry, 'weblink', pre='[link]')
+ url = format_field(entry, 'url', pre='[url]')
webslides = format_field(entry, 'webslides', pre='[slides]')
weberrata = format_field(entry, 'weberrata', pre='[errata]')
- return " ".join([webpdf, weblink, webslides, weberrata])
+ return " ".join([doi, webpdf, weblink, url, webslides, weberrata])
def format_entry(entry):
lines = []
@@ -111,8 +168,12 @@ def format_entry(entry):
lines.extend(format_details_article(entry))
elif entry.type=='inproceedings':
lines.extend(format_details_inproceedings(entry))
+ elif entry.type=='incollection':
+ lines.extend(format_details_incollection(entry))
elif entry.type=='book':
lines.extend(format_details_book(entry))
+ elif entry.type=='patent':
+ lines.extend(format_details_patent(entry))
elif entry.type in ['mastersthesis', 'phdthesis']:
lines.extend(format_details_thesis(entry))
else:
@@ -125,19 +186,38 @@ def format_entry(entry):
return "
\n".join(lines)
-def entryCompareDate(p1, p2):
- k1, e1 = p1
- k2, e2 = p2
+def entryDateSortKey(p):
+ k, e = p
+ if 'date' in e.fields:
+ return e.fields['date']
- def toStr(e):
- month = { 'jan' : '01', 'feb' : '02', 'mar' : '03', \
- 'apr' : '04', 'may' : '05', 'jun' : '06', \
- 'jul' : '07', 'aug' : '08', 'sep' : '09', \
- 'oct' : '10', 'nov' : '11', 'dec' : '12'}
- return e.fields['year'] + "-" + month[e.fields['month'].lower()[0:3]]
+ month2num = { 'jan' : '01', 'feb' : '02', 'mar' : '03', \
+ 'apr' : '04', 'may' : '05', 'jun' : '06', \
+ 'jul' : '07', 'aug' : '08', 'sep' : '09', \
+ 'oct' : '10', 'nov' : '11', 'dec' : '12'}
- return cmp(toStr(e1), toStr(e2))
+ if not 'month' in e.fields:
+ return e.fields['year']
+
+ month = e.fields['month'].lower()[0:3]
+ if month in month2num:
+ month = month2num[month]
+ else:
+ month = ""
+
+ return e.fields['year'] + "-" + month
+
+
+def entryGetYear(e):
+ if 'year' in e.fields:
+ return e.fields['year']
+
+ if 'date' in e.fields:
+ dt = dateutil.parser.isoparse(e.fields['date'])
+ return str(dt.year)
+
+ return None
def usage():
@@ -186,24 +266,23 @@ if __name__ == "__main__":
bib_data = parser.parse_file(bibfile)
entries = bib_data.entries
- years = list(set([ b.fields['year'] for b in entries.values() ]))
+ years = list(set([entryGetYear(e) for e in entries.values()]))
years.sort(reverse=True)
for year in years:
print("