diff --git a/django/contrib/markup/templatetags/markup.py b/django/contrib/markup/templatetags/markup.py
index 7cdc04c..66cb12b 100644
a
|
b
|
markup syntaxes to HTML; currently there is support for:
|
11 | 11 | * reStructuredText, which requires docutils from http://docutils.sf.net/ |
12 | 12 | """ |
13 | 13 | |
| 14 | import warnings |
| 15 | |
14 | 16 | from django import template |
15 | 17 | from django.conf import settings |
16 | 18 | from django.utils.encoding import smart_str, force_unicode |
… |
… |
def markdown(value, arg=''):
|
65 | 67 | |
66 | 68 | # Unicode support only in markdown v1.7 or above. Version_info |
67 | 69 | # exist only in markdown v1.6.2rc-2 or above. |
68 | | if getattr(markdown, "version_info", None) < (1,7): |
| 70 | markdown_vers = getattr(markdown, "version_info", None) |
| 71 | if markdown_vers < (1,7): |
69 | 72 | return mark_safe(force_unicode(markdown.markdown(smart_str(value), extensions, safe_mode=safe_mode))) |
70 | 73 | else: |
71 | | return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode)) |
| 74 | if markdown_vers >= (2,1): |
| 75 | if safe_mode: |
| 76 | return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode, enable_attributes=False)) |
| 77 | else: |
| 78 | return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode)) |
| 79 | else: |
| 80 | warnings.warn("Versions of markdown prior to 2.1 do not " |
| 81 | "support disabling of attributes, no " |
| 82 | "attributes have been removed and the result " |
| 83 | "is insecure.") |
| 84 | return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode)) |
72 | 85 | else: |
73 | 86 | return mark_safe(force_unicode(markdown.markdown(smart_str(value)))) |
74 | 87 | markdown.is_safe = True |
diff --git a/django/contrib/markup/tests.py b/django/contrib/markup/tests.py
index e97a7de..6903dd7 100644
a
|
b
|
Paragraph 2 with a link_
|
60 | 60 | pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""") |
61 | 61 | self.assertTrue(pattern.match(rendered)) |
62 | 62 | |
| 63 | @unittest.skipUnless(markdown, 'markdown no installed') |
| 64 | def test_markdown_attribute_disable(self): |
| 65 | t = Template("{% load markup %}{{ markdown_content|markdown:'safe' }}") |
| 66 | markdown_content = "{@onclick=alert('hi')}some paragraph" |
| 67 | rendered = t.render(Context({'markdown_content':markdown_content})).strip() |
| 68 | self.assertTrue('@' in rendered) |
| 69 | |
| 70 | @unittest.skipUnless(markdown, 'markdown no installed') |
| 71 | def test_markdown_attribute_enable(self): |
| 72 | t = Template("{% load markup %}{{ markdown_content|markdown }}") |
| 73 | markdown_content = "{@onclick=alert('hi')}some paragraph" |
| 74 | rendered = t.render(Context({'markdown_content':markdown_content})).strip() |
| 75 | self.assertFalse('@' in rendered) |
| 76 | |
63 | 77 | @unittest.skipIf(markdown, 'markdown is installed') |
64 | 78 | def test_no_markdown(self): |
65 | 79 | t = Template("{{ markdown_content|markdown }}") |
diff --git a/docs/ref/contrib/markup.txt b/docs/ref/contrib/markup.txt
index d5f07f5..a4480f6 100644
a
|
b
|
override the default writer settings. See the `restructuredtext writer
|
47 | 47 | settings`_ for details on what these settings are. |
48 | 48 | |
49 | 49 | .. _restructuredtext writer settings: http://docutils.sourceforge.net/docs/user/config.html#html4css1-writer |
| 50 | |
| 51 | Markdown |
| 52 | -------- |
| 53 | |
| 54 | The Python Markdown library supports options named "safe_mode" and |
| 55 | "enable_attributes". Both relate to the security of the output. To enable both |
| 56 | options in tandem, the markdown filter supports the "safe" argument. |
| 57 | |
| 58 | {{ markdown_content_var|markdown:"safe" }} |
| 59 | |
| 60 | .. warning:: |
| 61 | |
| 62 | Versions of the Python-Markdown library prior to 2.1 do not support the |
| 63 | optional disabling of attributes and by default they will be included in |
| 64 | any output from the markdown filter - a warning is issued if this is the |
| 65 | case. |