aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/rst/roles.py
blob: bff00533a2bb606846d76f3b34aa52c5d4e87a31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# coding=utf-8
from docutils import nodes
from docutils.parsers.rst.roles import set_classes
from docutils.parsers.rst.states import Inliner
from jinja2 import escape


def icon_role(_role: str, rawtext: str, text: str, lineno: int, inliner: Inliner,
              options: dict = None, _content: dict = None):
    if options is None:
        options = {}

    set_classes(options)

    if "/" in text:
        parts = [escape(x) for x in text.split("/")]
    else:
        msg = inliner.reporter.error("Icon specification must be in the form <type>/<name>", line=lineno)
        prb = inliner.problematic(text, rawtext, msg)

        return [prb], [msg]

    if len(parts) != 2:
        msg = inliner.reporter.error("Icon specification must be in the form <type>/<name>", line=lineno)
        prb = inliner.problematic(text, rawtext, msg)

        return [prb], [msg]
    else:
        if parts[0] == "light":
            weight = "fal"
        elif parts[0] == "regular":
            weight = "far"
        elif parts[0] == "solid":
            weight = "fas"
        elif parts[0] == "branding":
            weight = "fab"
        else:
            msg = inliner.reporter.error("Icon type must be one of light, regular, solid or branding", line=lineno)
            prb = inliner.problematic(text, rawtext, msg)

            return [prb], [msg]

        html = f"""<i class="uk-icon {weight} fa-{parts[1]}"></i>"""

        node = nodes.raw(html, html, format="html", **options)
        return [node], []