Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #2910 -- Added support for markdown extensions to the markdown …

…markup

filter (try saying that ten times quickly!). Thanks, Waylan Limberg.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6834 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4ac8ba18559ab352e06a553df5ef49244b72413c 1 parent 9075ac4
@malcolmt malcolmt authored
Showing with 29 additions and 2 deletions.
  1. +29 −2 django/contrib/markup/templatetags/markup.py
View
31 django/contrib/markup/templatetags/markup.py
@@ -32,7 +32,23 @@ def textile(value):
return mark_safe(force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8')))
textile.is_safe = True
-def markdown(value):
+def markdown(value, arg=''):
+ """
+ Runs Markdown over a given value, optionally using various
+ extensions python-markdown supports.
+
+ Syntax::
+
+ {{ value|markdown:"extension1_name,extension2_name..." }}
+
+ To enable safe mode, which strips raw HTML and only returns HTML
+ generated by actual Markdown syntax, pass "safe" as the first
+ extension in the list.
+
+ If the version of Markdown in use does not support extensions,
+ they will be silently ignored.
+
+ """
try:
import markdown
except ImportError:
@@ -40,7 +56,18 @@ def markdown(value):
raise template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed."
return force_unicode(value)
else:
- return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
+ # markdown.version was first added in 1.6b. The only version of markdown
+ # to fully support extensions before 1.6b was the shortlived 1.6a.
+ if hasattr(markdown, 'version'):
+ extensions = [e for e in arg.split(",") if e]
+ if len(extensions) > 0 and extensions[0] == "safe":
+ extensions = extensions[1:]
+ safe_mode = True
+ else:
+ safe_mode = False
+ return mark_safe(force_unicode(markdown.markdown(smart_str(value), extensions, safe_mode=safe_mode)))
+ else:
+ return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
markdown.is_safe = True
def restructuredtext(value):
Please sign in to comment.
Something went wrong with that request. Please try again.