diff --git a/acros/utils/conversion.py b/acros/utils/conversion.py index 8304e68..d81dced 100644 --- a/acros/utils/conversion.py +++ b/acros/utils/conversion.py @@ -7,6 +7,7 @@ def md_to_html(md: str) -> str: output_format="html5", extensions=[ "nl2br", + "acros.utils.wikilinks" ] ) return html diff --git a/acros/utils/wikilinks.py b/acros/utils/wikilinks.py new file mode 100644 index 0000000..846c8a1 --- /dev/null +++ b/acros/utils/wikilinks.py @@ -0,0 +1,68 @@ +""" +Original code Copyright [Waylan Limberg](http://achinghead.com/). + +All changes Copyright The Python Markdown Project + +License: [BSD](https://opensource.org/licenses/bsd-license.php) + +------------ + +Custom modification by Lukas Winkler + +""" + +import xml.etree.ElementTree as etree + +from markdown import Extension +from markdown.inlinepatterns import InlineProcessor + +from acros.models import Acronym + + +def build_url(label, base, end): + return "".join([base, label, end]) + + +class WikiLinkExtension(Extension): + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def extendMarkdown(self, md): + self.md = md + + # append to end of inline patterns + WIKILINK_RE = r"\[\[([\w0-9_ -]+)\]\]" + wikilinkPattern = WikiLinksInlineProcessor(WIKILINK_RE, self.getConfigs()) + wikilinkPattern.md = md + md.inlinePatterns.register(wikilinkPattern, "acrowikilink", 75) + + +class WikiLinksInlineProcessor(InlineProcessor): + base_url = "/" + end_url = "/" + html_class = "wikilink" + + def __init__(self, pattern, config): + super().__init__(pattern) + + def handleMatch(self, m, data): + label = m.group(1).strip() + try: + acro = Acronym.objects.get(name=label) + except Acronym.DoesNotExist: + # TODO: Notify user of invalid acronym + return "", m.start(0), m.end(0) + url = f"/acronym/{acro.slug}" + a = etree.Element("a") + a.text = label + a.set("href", url) + a.set("title", acro.full_name) + a.set("data-toggle", "tooltip") + if self.html_class: + a.set("class", self.html_class) + return a, m.start(0), m.end(0) + + +def makeExtension(**kwargs): # pragma: no cover + return WikiLinkExtension(**kwargs)