X-Git-Url: https://git.sthu.org/?p=shutils.git;a=blobdiff_plain;f=bib2html.py;h=64286f25ed25a3c513164c6481194e67f8280be2;hp=5db0d075d32d4ba6b75cbe1ddc7ba66a51cc822c;hb=HEAD;hpb=aed683d1d5066a1ecfa529973bb887b08dfcb893 diff --git a/bib2html.py b/bib2html.py index 5db0d07..7557cce 100755 --- a/bib2html.py +++ b/bib2html.py @@ -32,12 +32,31 @@ __license__ = "MIT" import os, sys, getopt, re +import dateutil.parser def format_latex(text): # Get rid of matching dollar signs text = re.sub(r'\$([^\$]*)\$', r'\1', text) - return text.replace('\mathcal', '').replace('{', '').replace('}', '').replace('\\', '') + + # 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) + "" @@ -84,6 +103,21 @@ def format_details_inproceedings(entry): 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)] + def format_details_thesis(entry): line = [] line.append(format_field(entry, 'school')) @@ -105,14 +139,25 @@ def format_details_book(entry): 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([doi, webpdf, weblink, webslides, weberrata]) + return " ".join([doi, webpdf, weblink, url, webslides, weberrata]) def format_entry(entry): lines = [] @@ -123,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: @@ -140,6 +189,9 @@ def format_entry(entry): def entryDateSortKey(p): k, e = p + if 'date' in e.fields: + return e.fields['date'] + month2num = { 'jan' : '01', 'feb' : '02', 'mar' : '03', \ 'apr' : '04', 'may' : '05', 'jun' : '06', \ 'jul' : '07', 'aug' : '08', 'sep' : '09', \ @@ -157,6 +209,17 @@ def entryDateSortKey(p): 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(): """Print usage text of this program""" @@ -203,7 +266,7 @@ 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: @@ -214,14 +277,12 @@ if __name__ == "__main__": iteritems.sort(key=entryDateSortKey, reverse=True) for key, entry in iteritems: - if entry.fields['year'] != year: + if entryGetYear(entry) != year: continue print("
") - print("[" + key + "]
") - + print("[{}]
{}
".format(key, key, entry.type)) e = format_entry(entry) print(e) print("
\n") -