]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - sommitrealweird/generic/templatetags/xhtml11rst.py
3bc212a4c79f30b7347a6260e8d6cc7ff0966052
[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_str, force_unicode
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_unicode(value)
16     else:
17         docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
18         parts = publish_parts(source=smart_str(value), writer=docutils_xhtml11.Writer(), settings_overrides=docutils_settings)
19         return mark_safe(force_unicode(parts["fragment"]))
20 restructuredtext.is_safe = True
21
22 register.filter(restructuredtext)