aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/rst
diff options
context:
space:
mode:
Diffstat (limited to 'pysite/rst')
-rw-r--r--pysite/rst/__init__.py3
-rw-r--r--pysite/rst/roles.py36
2 files changed, 38 insertions, 1 deletions
diff --git a/pysite/rst/__init__.py b/pysite/rst/__init__.py
index 815ad058..e0fc973e 100644
--- a/pysite/rst/__init__.py
+++ b/pysite/rst/__init__.py
@@ -2,7 +2,7 @@
from docutils.core import publish_parts
from docutils.parsers.rst.roles import register_canonical_role
-from pysite.rst.roles import icon_role, url_for_role
+from pysite.rst.roles import icon_role, page_role, url_for_role
def render(rst: str):
@@ -12,4 +12,5 @@ def render(rst: str):
register_canonical_role("icon", icon_role)
+register_canonical_role("page", page_role)
register_canonical_role("url_for", url_for_role)
diff --git a/pysite/rst/roles.py b/pysite/rst/roles.py
index 414832df..3d0155e0 100644
--- a/pysite/rst/roles.py
+++ b/pysite/rst/roles.py
@@ -81,3 +81,39 @@ def url_for_role(_role: str, rawtext: str, text: str, lineno: int, inliner: Inli
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.split("/")]
+ 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]
+
+ if len(parts) != 2:
+ 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]
+ else:
+ 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]