diff options
author | 2018-04-09 14:53:56 +0100 | |
---|---|---|
committer | 2018-04-09 14:53:56 +0100 | |
commit | 0dc85de66d84d51bff4260f3fefcc8b2251d12e6 (patch) | |
tree | e12352a8ff3a961a36b1bbaa3d980a0c79d4d92b /pysite/rst | |
parent | [Wiki] Fix editor (diff) |
Abstract rst away and add icon directive
Diffstat (limited to 'pysite/rst')
-rw-r--r-- | pysite/rst/__init__.py | 14 | ||||
-rw-r--r-- | pysite/rst/directives/__init__.py | 1 | ||||
-rw-r--r-- | pysite/rst/roles.py | 46 |
3 files changed, 61 insertions, 0 deletions
diff --git a/pysite/rst/__init__.py b/pysite/rst/__init__.py new file mode 100644 index 00000000..0e5f6ffe --- /dev/null +++ b/pysite/rst/__init__.py @@ -0,0 +1,14 @@ +# coding=utf-8 +from docutils.core import publish_parts +from docutils.parsers.rst.roles import register_canonical_role + +from pysite.rst.roles import icon_role + + +def render(rst: str): + return publish_parts( + source=rst, writer_name="html5", settings_overrides={"halt_level": 2} + )["html_body"] + + +register_canonical_role("icon", icon_role) diff --git a/pysite/rst/directives/__init__.py b/pysite/rst/directives/__init__.py new file mode 100644 index 00000000..9bad5790 --- /dev/null +++ b/pysite/rst/directives/__init__.py @@ -0,0 +1 @@ +# coding=utf-8 diff --git a/pysite/rst/roles.py b/pysite/rst/roles.py new file mode 100644 index 00000000..bff00533 --- /dev/null +++ b/pysite/rst/roles.py @@ -0,0 +1,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], [] |