Code

Ticket #2910: 2910-4.diff

File 2910-4.diff, 2.1 KB (added by wayla, 7 years ago)

Patch updated to support the new auto-escaping

Line 
1=== modified file 'django/contrib/markup/templatetags/markup.py'
2--- django/contrib/markup/templatetags/markup.py        2007-11-14 12:58:53 +0000
3+++ django/contrib/markup/templatetags/markup.py        2007-11-15 19:07:13 +0000
4@@ -32,7 +32,23 @@
5         return mark_safe(force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8')))
6 textile.is_safe = True
7 
8-def markdown(value):
9+def markdown(value, arg=''):
10+    """
11+    Runs Markdown over a given value, optionally using various
12+    extensions python-markdown supports.
13+           
14+    Syntax::
15+                   
16+        {{ value|markdown:"extension1_name,extension2_name..." }}
17+                               
18+    To enable safe mode, which strips raw HTML and only returns HTML
19+    generated by actual Markdown syntax, pass "safe" as the first
20+    extension in the list.
21+                                               
22+    If the version of Markdown in use does not support extensions,
23+    they will be silently ignored.
24+                                                           
25+    """
26     try:
27         import markdown
28     except ImportError:
29@@ -40,7 +56,18 @@
30             raise template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed."
31         return force_unicode(value)
32     else:
33-        return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
34+        # markdown.version was first added in 1.6b. The only version of markdown
35+        # to fully support extensions before 1.6b was the shortlived 1.6a.
36+        if hasattr(markdown, 'version'):
37+            extensions = [e for e in arg.split(",") if e]
38+            if len(extensions) > 0 and extensions[0] == "safe":
39+                extensions = extensions[1:]
40+                safe_mode = True
41+            else:
42+                safe_mode = False
43+            return mark_safe(force_unicode(markdown.markdown(smart_str(value), extensions, safe_mode=safe_mode)))
44+        else:
45+            return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
46 markdown.is_safe = True
47 
48 def restructuredtext(value):
49