2
Copied from Django 1.4 alpha
4
Set of "markup" template filters for Django. These filters transform plain text
5
markup syntaxes to HTML; currently there is support for:
7
* Textile, which requires the PyTextile library available at
8
http://loopcore.com/python-textile/
10
* Markdown, which requires the Python-markdown library from
11
http://www.freewisdom.org/projects/python-markdown
13
* reStructuredText, which requires docutils from http://docutils.sf.net/
16
from django import template
17
from django.conf import settings
18
from django.utils.encoding import smart_str, force_unicode
19
from django.utils.safestring import mark_safe
21
register = template.Library()
29
raise template.TemplateSyntaxError("Error in {% textile %} filter: The Python textile library isn't installed.")
30
return force_unicode(value)
32
return mark_safe(force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8')))
35
def markdown(value, arg=''):
37
Runs Markdown over a given value, optionally using various
38
extensions python-markdown supports.
42
{{ value|markdown:"extension1_name,extension2_name..." }}
44
To enable safe mode, which strips raw HTML and only returns HTML
45
generated by actual Markdown syntax, pass "safe" as the first
46
extension in the list.
48
If the version of Markdown in use does not support extensions,
49
they will be silently ignored.
56
raise template.TemplateSyntaxError("Error in {% markdown %} filter: The Python markdown library isn't installed.")
57
return force_unicode(value)
59
# markdown.version was first added in 1.6b. The only version of markdown
60
# to fully support extensions before 1.6b was the shortlived 1.6a.
61
if hasattr(markdown, 'version'):
62
extensions = [e for e in arg.split(",") if e]
63
if len(extensions) > 0 and extensions[0] == "safe":
64
extensions = extensions[1:]
69
# Unicode support only in markdown v1.7 or above. Version_info
70
# exist only in markdown v1.6.2rc-2 or above.
71
if getattr(markdown, "version_info", None) < (1,7):
72
return mark_safe(force_unicode(markdown.markdown(smart_str(value), extensions, safe_mode=safe_mode)))
74
return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode))
76
return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
79
def restructuredtext(value):
81
from docutils.core import publish_parts
84
raise template.TemplateSyntaxError("Error in {% restructuredtext %} filter: The Python docutils library isn't installed.")
85
return force_unicode(value)
87
docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
88
parts = publish_parts(source=smart_str(value), writer_name="html4css1", settings_overrides=docutils_settings)
89
return mark_safe(force_unicode(parts["fragment"]))