From f48eda6ee2e23d3ee5165bdd69b5758f799a7e43 Mon Sep 17 00:00:00 2001 From: Jeff Dairiki Date: Mon, 2 May 2022 15:47:17 -0700 Subject: [PATCH] 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.) --- .../lektor_markdown_link_classes.py | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/markdown-link-classes/lektor_markdown_link_classes.py b/packages/markdown-link-classes/lektor_markdown_link_classes.py index ecd97d9c..1fe0f8dc 100644 --- a/packages/markdown-link-classes/lektor_markdown_link_classes.py +++ b/packages/markdown-link-classes/lektor_markdown_link_classes.py @@ -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"{text if text else link}" + + +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 '%s' % (' '.join(attr), text) config.renderer_mixins.append(LinkClassesMixin)