+from django import template
+from django.conf import settings
+from django.utils.encoding import smart_str, force_unicode
+from django.utils.safestring import mark_safe
+from generic import docutils_xhtml11
+
+register = template.Library()
+
+def restructuredtext(value):
+ try:
+ from docutils.core import publish_parts
+ except ImportError:
+ if settings.DEBUG:
+ raise template.TemplateSyntaxError, "Error in {% restructuredtext %} filter: The Python docutils library isn't installed."
+ return force_unicode(value)
+ else:
+ docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
+ parts = publish_parts(source=smart_str(value), writer=docutils_xhtml11.Writer(), settings_overrides=docutils_settings)
+ return mark_safe(force_unicode(parts["fragment"]))
+restructuredtext.is_safe = True
+
+register.filter(restructuredtext)