diff options
| author | 2018-04-10 22:45:51 +0200 | |
|---|---|---|
| committer | 2018-04-10 22:45:51 +0200 | |
| commit | 5420551d340fc06146f2a8fdf7d69ade0195239d (patch) | |
| tree | 786750b177fbf348835c816b066ab9c7b6e45087 /pysite/rst/roles.py | |
| parent | Moved all redirects into a folder and added one for github. (diff) | |
| parent | Whoops, Flake8 (diff) | |
Merge branch 'master' of github.com:discord-python/site
Diffstat (limited to 'pysite/rst/roles.py')
| -rw-r--r-- | pysite/rst/roles.py | 36 | 
1 files changed, 36 insertions, 0 deletions
| diff --git a/pysite/rst/roles.py b/pysite/rst/roles.py index 414832df..10f14603 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] | 
