mirror of
https://github.com/Findus23/acronomy.git
synced 2024-09-19 15:33:45 +02:00
parent
fb8e2ea15e
commit
0b4fb6d909
2 changed files with 69 additions and 0 deletions
|
@ -7,6 +7,7 @@ def md_to_html(md: str) -> str:
|
||||||
output_format="html5",
|
output_format="html5",
|
||||||
extensions=[
|
extensions=[
|
||||||
"nl2br",
|
"nl2br",
|
||||||
|
"acros.utils.wikilinks"
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
return html
|
return html
|
||||||
|
|
68
acros/utils/wikilinks.py
Normal file
68
acros/utils/wikilinks.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue