Restore compatibility with mistune==0.8.4

The latest release version of Lektor is 3.3.4.  It does not include
the support for `mistune>=2.0` that has made it into the `master`
branch of Lektor, thus it pins `mistune<2`.

This commit restores compatibility with older versions of
mistune.  (That compatibility was broken by 3fcd062.)
This commit is contained in:
Jeff Dairiki 2022-05-02 15:47:17 -07:00
parent 3fcd0624e9
commit f48eda6ee2
1 changed files with 23 additions and 12 deletions

View File

@ -1,6 +1,7 @@
import re
from lektor.pluginsystem import Plugin
from markupsafe import escape
from mistune import __version__ as mistune_version
_class_re = re.compile(r'\s+:([a-zA-Z0-9_-]+)')
@ -15,21 +16,31 @@ def split_classes(text):
return text, classes
def render_link(link, text, title=None):
text, classes = split_classes(text)
if link.startswith('javascript:'):
link = ''
attr = [f'href="{escape(link)}"']
if title:
attr.append(f'title="{escape(title)}"')
if classes:
attr.append(f'class="{" ".join(map(escape, classes))}"')
return f"<a {' '.join(attr)}>{text if text else link}</a>"
if mistune_version.startswith("0."):
class LinkClassesMixin(object):
def link(renderer, link, title, text):
return render_link(link, text, title)
else:
class LinkClassesMixin(object):
def link(renderer, link, text, title):
return render_link(link, text, title)
class MarkdownLinkClassesPlugin(Plugin):
name = 'Markdown Link Classes'
description = 'Adds the ability to add classes to links.'
def on_markdown_config(self, config, **extra):
class LinkClassesMixin(object):
def link(renderer, link, text, title):
text, classes = split_classes(text)
if link.startswith('javascript:'):
link = ''
attr = ['href="%s"' % escape(link)]
if title:
attr.append('title="%s"' % escape(title))
if classes:
attr.append('class="%s"' % ' '.join(
escape(x) for x in classes))
return '<a %s>%s</a>' % (' '.join(attr), text)
config.renderer_mixins.append(LinkClassesMixin)