2017-10-25 23:40:29 -04:00
|
|
|
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
|
|
|
|
|
2018-01-10 19:41:36 -05:00
|
|
|
tags = ['cite', 'code', 'p', 'pre', 'img', 'span']
|
2017-10-25 23:40:29 -04:00
|
|
|
tags.extend(ALLOWED_TAGS)
|
|
|
|
attributes = {
|
2017-10-29 04:35:57 -04:00
|
|
|
'a': ('href', 'title', 'class'),
|
|
|
|
'img': ('alt', 'src', 'title'),
|
2017-11-06 00:06:25 -05:00
|
|
|
'span': ('class',),
|
2017-10-25 23:40:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
cleaner = Cleaner(tags=tags, attributes=attributes, filters=(LinkifyFilter,))
|
|
|
|
|
|
|
|
|
|
|
|
@register.filter
|
|
|
|
@stringfilter
|
|
|
|
def bleach(html):
|
|
|
|
return mark_safe(cleaner.clean(html))
|