bornhack-website/src/utils/templatetags/commonmark.py

28 lines
881 B
Python
Raw Normal View History

2019-01-22 07:57:24 +00:00
import commonmark, bleach
from django import template
2016-08-15 07:16:07 +00:00
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def trustedcommonmark(value):
2019-01-22 07:57:24 +00:00
"""Returns HTML given some commonmark Markdown. Also allows real HTML, so do not use this with untrusted input."""
parser = commonmark.Parser()
renderer = commonmark.HtmlRenderer()
ast = parser.parse(value)
return mark_safe(renderer.render(ast))
@register.filter
@stringfilter
def untrustedcommonmark(value):
2019-01-22 07:57:24 +00:00
"""Returns HTML given some commonmark Markdown. Cleans actual HTML from input using bleach, suitable for use with untrusted input."""
parser = commonmark.Parser()
renderer = commonmark.HtmlRenderer()
2016-08-15 07:16:07 +00:00
ast = parser.parse(bleach.clean(value))
return mark_safe(renderer.render(ast))