chrisjsewell / ipypublish
1 3
import io
2 3
import re
3 3
import string
4

5 3
from six import string_types
6 3
import bibtexparser
7

8

9 3
def read_bibliography(path, raise_error=True):
10
    """ read a bibliography
11

12
    """
13 0
    bibdatabase = {}
14 0
    bibparser = bibtexparser.bparser.BibTexParser()
15 0
    try:
16 0
        if hasattr(path, "open"):
17 0
            with path.open(encoding="utf8") as bibtex_file:
18 0
                bibtex_data = bibtex_file.read()
19
        else:
20 0
            with io.open(path, encoding="utf8") as bibtex_file:
21 0
                bibtex_data = bibtex_file.read()
22 0
        bibtex_data = safe_str(bibtex_data)
23 0
        bibdatabase = bibparser.parse(bibtex_data).entries_dict
24 0
    except Exception as err:
25 0
        if raise_error:
26 0
            raise IOError("could not read bibliopath {}: {}".format(path, err))
27

28 0
    return bibdatabase
29

30

31 3
def process_bib_entry(
32
    cid, bibdatabase, bibnums, fallback_fmt="[{author_abbrev}, {year}]"
33
):
34
    """work out the best way to represent the bib entry """
35 0
    entry = bibdatabase[cid]
36 0
    if cid not in bibnums:
37 0
        bibnums[cid] = len(bibnums) + 1
38

39 0
    if "doi" in entry:
40 0
        return r'<a href="https://doi.org/{doi}">{text}</a>'.format(
41
            doi=entry["doi"], text=bibnums[cid]
42
        )
43 0
    elif "url" in entry:
44 0
        return r'<a href="{url}">{text}</a>'.format(url=entry["url"], text=bibnums[cid])
45 0
    elif "link" in entry:
46 0
        return r'<a href="{url}">{text}</a>'.format(
47
            url=entry["link"], text=bibnums[cid]
48
        )
49
    else:
50 0
        return bibnums[cid]
51
        # add_abbreviated_author(entry)
52
        # split_date(entry)
53
        # return DefaultFormatter().format(fallback_fmt, **entry)
54

55

56 3
def add_abbreviated_author(entry):
57
    # abbreviate a list of authors
58 0
    if "author" in entry:
59 0
        authors = re.split(", | and ", entry["author"])
60 0
        if len(authors) > 1:
61 0
            author_abbrev = authors[0] + " <em>et al</em>"
62
        else:
63 0
            author_abbrev = authors[0]
64 0
        entry["author_abbrev"] = author_abbrev
65

66

67 3
def split_date(entry):
68
    # split up date into year, month, day
69 0
    if "date" in entry:
70 0
        date = entry["date"].split("-")
71 0
        if len(date) == 3:
72 0
            entry["year"] = date[0]
73 0
            entry["month"] = date[1]
74 0
            entry["day"] = date[2]
75
        else:
76 0
            entry["year"] = date[0]
77

78

79 3
class DefaultFormatter(string.Formatter):
80 3
    def __init__(self, default=""):
81 0
        self.default = default
82

83 3
    def get_value(self, key, args, kwds):
84 0
        if isinstance(key, string_types):
85 0
            return kwds.get(key, self.default.format(key))
86
        else:
87 0
            string.Formatter.get_value(key, args, kwds)
88

89

90 3
def safe_str(obj):
91 0
    if hasattr(obj, "decode"):
92 0
        try:
93 0
            obj = obj.decode("utf-8")
94 0
        except UnicodeEncodeError:
95 0
            pass
96 0
    try:
97 0
        return str(obj)
98 0
    except UnicodeEncodeError:
99
        # python 2.7
100 0
        obj = re.sub(u"\u2013", "-", obj)  # en dash
101 0
        obj = re.sub(u"\u2014", "--", obj)  # em dash
102 0
        return obj.encode("ascii", "ignore").decode("ascii")
103 0
    return ""

Read our documentation on viewing source code .

Loading