Code

Ticket #2910: 2910-2.diff

File 2910-2.diff, 1.9 KB (added by alex@…, 7 years ago)

fix for stdout+mod_wsgi problem, please review wrt. unicode changes

Line 
1Index: django/contrib/markup/templatetags/markup.py
2===================================================================
3--- django/contrib/markup/templatetags/markup.py        (revision 6442)
4+++ django/contrib/markup/templatetags/markup.py        (working copy)
5@@ -30,15 +30,41 @@
6     else:
7         return force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8'))
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 force_unicode(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 force_unicode(markdown.markdown(smart_str(value)))
41+        extensions = [e for e in arg.split(",") if e]
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 force_unicode(markdown.markdown(value, extensions, safe_mode=safe_mode))
48 
49 def restructuredtext(value):
50     try: