Changes to make it work with django 1.11 and python3
[sommitrealweird.git] / sommitrealweird / generic / templatetags / xhtml11rst.py
1 from django import template
2 from django.conf import settings
3 from django.utils.encoding import smart_bytes, force_text
4 from django.utils.safestring import mark_safe
5 from generic import docutils_xhtml11
6
7 register = template.Library()
8
9 def restructuredtext(value):
10     try:
11         from docutils.core import publish_parts
12     except ImportError:
13         if settings.DEBUG:
14             raise template.TemplateSyntaxError("Error in {% restructuredtext %} filter: The Python docutils library isn't installed.")
15         return force_text(value)
16     else:
17         docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
18         parts = publish_parts(source=smart_bytes(value), writer=docutils_xhtml11.Writer(), settings_overrides=docutils_settings)
19         return mark_safe(force_text(parts["fragment"]))
20 restructuredtext.is_safe = True
21
22 register.filter(restructuredtext)