How to use the bleach.linkifier.LinkifyFilter function in bleach

To help you get started, we’ve selected a few bleach examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mozilla / bleach / tests / test_linkify.py View on Github external
def test_linkify_filter(text, expected):
    cleaner = Cleaner(filters=[LinkifyFilter])
    assert cleaner.clean(text) == expected
github thomersch / openstreetmap-calendar / osmcal / templatetags / markdown.py View on Github external
import bleach
import markdown as md
from bleach.linkifier import LinkifyFilter
from django import template

allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'li', 'ol', 'p', 'pre', 'strong', 'ul']

register = template.Library()
cleaner = bleach.Cleaner(tags=allowed_tags, filters=[LinkifyFilter])


@register.filter(is_safe=True)
def markdown(value):
    if not value:
        return ""
    return cleaner.clean(md.markdown(value))


@register.tag()
def markdownify(parser, token):
    nodelist = parser.parse(('endmarkdownify', ))
    parser.delete_first_token()
    return Markdownify(nodelist)
github CenterForOpenScience / osf.io / addons / wiki / models.py View on Github external
def html(self, node):
        """The cleaned HTML of the page"""
        html_output = build_html_output(self.content, node=node)
        try:
            cleaner = Cleaner(
                tags=settings.WIKI_WHITELIST['tags'],
                attributes=settings.WIKI_WHITELIST['attributes'],
                styles=settings.WIKI_WHITELIST['styles'],
                filters=[partial(LinkifyFilter, callbacks=[nofollow, ])]
            )
            return cleaner.clean(html_output)
        except TypeError:
            logger.warning('Returning unlinkified content.')
            return render_content(self.content, node=node)
github hypothesis / h / h / util / markdown.py View on Github external
def _get_cleaner():
    global cleaner
    if cleaner is None:
        linkify_filter = partial(
            LinkifyFilter, callbacks=[_linkify_target_blank, _linkify_rel]
        )
        cleaner = bleach.Cleaner(
            tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, filters=[linkify_filter]
        )
    return cleaner
github mozilla / bleach / bleach / linkifier.py View on Github external
:returns: linkified text as unicode

        :raises TypeError: if ``text`` is not a text type

        """
        if not isinstance(text, six.string_types):
            raise TypeError('argument must be of text type')

        text = force_unicode(text)

        if not text:
            return ''

        dom = self.parser.parseFragment(text)
        filtered = LinkifyFilter(
            source=self.walker(dom),
            callbacks=self.callbacks,
            skip_tags=self.skip_tags,
            parse_email=self.parse_email,
            url_re=self.url_re,
            email_re=self.email_re,
        )
        return self.serializer.render(filtered)
github hasgeek / coaster / coaster / utils / text.py View on Github external
def sanitize_html(value, valid_tags=None, strip=True, linkify=False):
    """
    Strips unwanted markup out of HTML.
    """
    if valid_tags is None:
        valid_tags = VALID_TAGS
    if linkify:
        filters = [
            partial(LinkifyFilter, skip_tags=['pre'], callbacks=DEFAULT_CALLBACKS)
        ]
    else:
        filters = []
    cleaner = Cleaner(
        tags=list(valid_tags.keys()),
        attributes=valid_tags,
        filters=filters,
        strip=strip,
    )
    return Markup(cleaner.clean(value))