lemoncurry/lemoncurry/templatetags/bleach.py

22 lines
528 B
Python

from django import template
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from bleach.sanitizer import Cleaner, ALLOWED_TAGS
from bleach.linkifier import LinkifyFilter
tags = ['code', 'p']
tags.extend(ALLOWED_TAGS)
attributes = {
'a': ('href', 'title', 'class')
}
register = template.Library()
cleaner = Cleaner(tags=tags, attributes=attributes, filters=(LinkifyFilter,))
@register.filter
@stringfilter
def bleach(html):
return mark_safe(cleaner.clean(html))