Code

Ticket #2910: 2910.diff

File 2910.diff, 1.9 KB (added by ubernostrum, 7 years ago)

Alternate version of other patches

Line 
1Index: django/contrib/markup/templatetags/markup.py
2===================================================================
3--- django/contrib/markup/templatetags/markup.py        (revision 4664)
4+++ django/contrib/markup/templatetags/markup.py        (working copy)
5@@ -29,15 +29,41 @@
6     else:
7         return textile.textile(value, encoding=settings.DEFAULT_CHARSET, output=settings.DEFAULT_CHARSET)
8 
9-def markdown(value):
10+def markdown(value, arg=''):
11+    """
12+    Runs Markdown over a given value, optionally using various
13+    extensions python-markdown supports.
14+   
15+    Syntax::
16+   
17+        {{ value|markdown:"extension1_name,extension2_name..." }}
18+   
19+    To enable safe mode, which strips raw HTML and only returns HTML
20+    generated by actual Markdown syntax, pass "safe" as the first
21+    extension in the list.
22+   
23+    If the version of Markdown in use does not support extensions,
24+    they will be silently ignored.
25+   
26+    """
27     try:
28         import markdown
29     except ImportError:
30         if settings.DEBUG:
31-            raise template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed."
32-        return value
33+            raise template.TemplateSyntaxError('Error in markdown filter: Markdown could not be imported.')
34+        else:
35+            # Try to salvage this; whatever we do, we shouldn't
36+            # return potentially unsafe HTML.
37+            from django.utils.html import escape, linebreaks
38+            return linebreaks(escape(value))
39     else:
40-        return markdown.markdown(value)
41+        extensions=arg.split(",")
42+        if len(extensions) > 0 and extensions[0] == "safe":
43+            extensions = extensions[1:]
44+            safe_mode = True
45+        else:
46+            safe_mode = False
47+        return markdown.markdown(value, extensions, safe_mode=safe_mode)
48 
49 def restructuredtext(value):
50     try: