Skip to content

Commit 7492439

Browse files
committed
Fixed django#18041 -- Removed support for Markdown versions < 2.1, following the 1.5 deprecation timeline. Thanks Ramiro Morales for the review.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17909 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent a9187e5 commit 7492439

File tree

3 files changed

+20
-36
lines changed

3 files changed

+20
-36
lines changed

django/contrib/markup/templatetags/markup.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* reStructuredText, which requires docutils from http://docutils.sf.net/
1212
"""
1313

14-
import warnings
15-
1614
from django import template
1715
from django.conf import settings
1816
from django.utils.encoding import smart_str, force_unicode
@@ -56,35 +54,21 @@ def markdown(value, arg=''):
5654
raise template.TemplateSyntaxError("Error in 'markdown' filter: The Python markdown library isn't installed.")
5755
return force_unicode(value)
5856
else:
59-
# markdown.version was first added in 1.6b. The only version of markdown
60-
# to fully support extensions before 1.6b was the shortlived 1.6a.
61-
if hasattr(markdown, 'version'):
57+
markdown_vers = getattr(markdown, "version_info", 0)
58+
if markdown_vers < (2, 1):
59+
if settings.DEBUG:
60+
raise template.TemplateSyntaxError(
61+
"Error in 'markdown' filter: Django does not support versions of the Python markdown library < 2.1.")
62+
return force_unicode(value)
63+
else:
6264
extensions = [e for e in arg.split(",") if e]
63-
if len(extensions) > 0 and extensions[0] == "safe":
65+
if extensions and extensions[0] == "safe":
6466
extensions = extensions[1:]
65-
safe_mode = True
67+
return mark_safe(markdown.markdown(
68+
force_unicode(value), extensions, safe_mode=True, enable_attributes=False))
6669
else:
67-
safe_mode = False
68-
python_markdown_deprecation = "The use of Python-Markdown "
69-
"< 2.1 in Django is deprecated; please update to the current version"
70-
# Unicode support only in markdown v1.7 or above. Version_info
71-
# exist only in markdown v1.6.2rc-2 or above.
72-
markdown_vers = getattr(markdown, "version_info", None)
73-
if markdown_vers < (1,7):
74-
warnings.warn(python_markdown_deprecation, DeprecationWarning)
75-
return mark_safe(force_unicode(markdown.markdown(smart_str(value), extensions, safe_mode=safe_mode)))
76-
else:
77-
if markdown_vers >= (2,1):
78-
if safe_mode:
79-
return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode, enable_attributes=False))
80-
else:
81-
return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode))
82-
else:
83-
warnings.warn(python_markdown_deprecation, DeprecationWarning)
84-
return mark_safe(markdown.markdown(force_unicode(value), extensions, safe_mode=safe_mode))
85-
else:
86-
warnings.warn(python_markdown_deprecation, DeprecationWarning)
87-
return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
70+
return mark_safe(markdown.markdown(
71+
force_unicode(value), extensions, safe_mode=False))
8872

8973
@register.filter(is_safe=True)
9074
def restructuredtext(value):

django/contrib/markup/tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ class Templates(unittest.TestCase):
3737
3838
.. _link: http://www.example.com/"""
3939

40-
@unittest.skipUnless(textile, 'texttile not installed')
40+
@unittest.skipUnless(textile, 'textile not installed')
4141
def test_textile(self):
4242
t = Template("{% load markup %}{{ textile_content|textile }}")
4343
rendered = t.render(Context({'textile_content':self.textile_content})).strip()
4444
self.assertEqual(rendered.replace('\t', ''), """<p>Paragraph 1</p>
4545
4646
<p>Paragraph 2 with &#8220;quotes&#8221; and <code>code</code></p>""")
4747

48-
@unittest.skipIf(textile, 'texttile is installed')
48+
@unittest.skipIf(textile, 'textile is installed')
4949
def test_no_textile(self):
5050
t = Template("{% load markup %}{{ textile_content|textile }}")
5151
rendered = t.render(Context({'textile_content':self.textile_content})).strip()
5252
self.assertEqual(rendered, escape(self.textile_content))
5353

54-
@unittest.skipUnless(markdown, 'markdown not installed')
54+
@unittest.skipUnless(markdown and markdown_version >= (2,1), 'markdown >= 2.1 not installed')
5555
def test_markdown(self):
5656
t = Template("{% load markup %}{{ markdown_content|markdown }}")
5757
rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()

docs/ref/contrib/markup.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Django provides template filters that implement the following markup
99
languages:
1010

1111
* ``textile`` -- implements `Textile`_ -- requires `PyTextile`_
12-
* ``markdown`` -- implements `Markdown`_ -- requires `Python-markdown`_
12+
* ``markdown`` -- implements `Markdown`_ -- requires `Python-markdown`_ (>=2.1)
1313
* ``restructuredtext`` -- implements `reST (reStructured Text)`_
1414
-- requires `doc-utils`_
1515

@@ -53,13 +53,13 @@ Markdown
5353

5454
The Python Markdown library supports options named "safe_mode" and
5555
"enable_attributes". Both relate to the security of the output. To enable both
56-
options in tandem, the markdown filter supports the "safe" argument.
56+
options in tandem, the markdown filter supports the "safe" argument::
5757

5858
{{ markdown_content_var|markdown:"safe" }}
5959

6060
.. warning::
6161

6262
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.
63+
optional disabling of attributes. This is a security flaw. Therefore,
64+
``django.contrib.markup`` has dropped support for versions of
65+
Python-Markdown < 2.1 in Django 1.5.

0 commit comments

Comments
 (0)