www-builder/linkify.py

46 lines
1.3 KiB
Python
Raw Normal View History

2021-06-28 17:30:52 +00:00
# Forked from https://github.com/daGrevis/mdx_linkify/blob/master/mdx_linkify/mdx_linkify.py
# Added linker_options.setdefault("callbacks", []) so that rel=nofollow isn't automatically added.
from bleach.linkifier import Linker
from markdown.postprocessors import Postprocessor
from markdown.extensions import Extension
class LinkifyExtension(Extension):
def __init__(self, **kwargs):
self.config = {
'linker_options': [{}, 'Options for bleach.linkifier.Linker'],
}
super(LinkifyExtension, self).__init__(**kwargs)
def extendMarkdown(self, md):
md.postprocessors.register(
LinkifyPostprocessor(
md,
self.getConfig('linker_options'),
),
"linkify",
50,
)
class LinkifyPostprocessor(Postprocessor):
def __init__(self, md, linker_options):
super(LinkifyPostprocessor, self).__init__(md)
linker_options.setdefault("skip_tags", ["code"])
linker_options.setdefault("callbacks", [])
self._linker_options = linker_options
def run(self, text):
linker = Linker(**self._linker_options)
return linker.linkify(text)
def makeExtension(*args, **kwargs):
return LinkifyExtension(*args, **kwargs)
makeExtension()