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
7 register = template.Library()
9 def restructuredtext(value):
11 from docutils.core import publish_parts
14 raise template.TemplateSyntaxError, "Error in {% restructuredtext %} filter: The Python docutils library isn't installed."
15 return force_unicode(value)
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
22 register.filter(restructuredtext)