aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/rst/roles.py
blob: d83f07f903e63ef1fbb07b60c7bee2eae65c11eb (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from docutils import nodes
from docutils.parsers.rst.roles import set_classes
from docutils.parsers.rst.states import Inliner
from flask import url_for
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 fa-fw {weight} fa-{parts[1]}"></i>"""

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


def url_for_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("URL specification must be in the form <page.name>/<text>", line=lineno)
        prb = inliner.problematic(text, rawtext, msg)

        return [prb], [msg]

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

        return [prb], [msg]
    else:
        try:
            url = url_for(parts[0])
            name = parts[1]

            html = f"""<a href="{url}">{name}</a>"""

            node = nodes.raw(html, html, format="html", **options)
            return [node], []
        except Exception as e:
            msg = inliner.reporter.error(str(e), line=lineno)
            prb = inliner.problematic(text, rawtext, msg)

            return [prb], [msg]


def page_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.rsplit("/", 1)]
    else:
        msg = inliner.reporter.error("Page specification must be in the form <page_slug>/<text>", line=lineno)
        prb = inliner.problematic(text, rawtext, msg)

        return [prb], [msg]

    try:
        url = url_for("wiki.page", page=parts[0])
        name = parts[1]

        html = f"""<a href="{url}">{name}</a>"""

        node = nodes.raw(html, html, format="html", **options)
        return [node], []
    except Exception as e:
        msg = inliner.reporter.error(str(e), line=lineno)
        prb = inliner.problematic(text, rawtext, msg)

        return [prb], [msg]


def fira_code_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)

    html = f"""<span class="fira-code">{text}</span>"""
    node = nodes.raw(html, html, format="html", **options)

    return [node], []