lektor-website/packages/markdown-link-classes/lektor_markdown_link_classe...

36 lines
1.1 KiB
Python
Raw Normal View History

2015-12-19 14:52:17 +01:00
import re
from lektor.pluginsystem import Plugin
from markupsafe import escape
_class_re = re.compile(r'\s+:([a-zA-Z0-9_-]+)')
def split_classes(text):
classes = []
def _handle_match(match):
classes.append(match.group(1))
return ''
text = _class_re.sub(_handle_match, text).replace('\\:', ':')
return text, classes
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):
2022-04-30 06:08:18 +02:00
def link(renderer, link, text, title):
2015-12-19 14:52:17 +01:00
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)