From 4a60575132b2221e9abca432bbef57611b629a97 Mon Sep 17 00:00:00 2001
From: alazaro
Date: Wed, 14 Dec 2011 23:36:13 +0100
Subject: Fixed test broken by markdown update
Removed unused code. No longer needed with markdown 2.1.0.
---
djangorestframework/compat.py | 37 +++-----------------------------
djangorestframework/tests/description.py | 6 +++---
2 files changed, 6 insertions(+), 37 deletions(-)
diff --git a/djangorestframework/compat.py b/djangorestframework/compat.py
index cae8c7b7..b7eedf85 100644
--- a/djangorestframework/compat.py
+++ b/djangorestframework/compat.py
@@ -374,48 +374,17 @@ else:
# Markdown is optional
try:
import markdown
- import re
-
- class CustomSetextHeaderProcessor(markdown.blockprocessors.BlockProcessor):
- """
- Override `markdown`'s :class:`SetextHeaderProcessor`, so that ==== headers are
and ---- headers are .
-
- We use for the resource name.
- """
-
- # Detect Setext-style header. Must be first 2 lines of block.
- RE = re.compile(r'^.*?\n[=-]{3,}', re.MULTILINE)
- def test(self, parent, block):
- return bool(self.RE.match(block))
-
- def run(self, parent, blocks):
- lines = blocks.pop(0).split('\n')
- # Determine level. ``=`` is 1 and ``-`` is 2.
- if lines[1].startswith('='):
- level = 2
- else:
- level = 3
- h = markdown.etree.SubElement(parent, 'h%d' % level)
- h.text = lines[0].strip()
- if len(lines) > 2:
- # Block contains additional lines. Add to master blocks for later.
- blocks.insert(0, '\n'.join(lines[2:]))
-
def apply_markdown(text):
"""
- Simple wrapper around :func:`markdown.markdown` to apply our :class:`CustomSetextHeaderProcessor`,
- and also set the base level of '#' style headers to .
+ Simple wrapper around :func:`markdown.markdown` to set the base level
+ of '#' style headers to .
"""
extensions = ['headerid(level=2)']
safe_mode = False,
- output_format = markdown.DEFAULT_OUTPUT_FORMAT
- md = markdown.Markdown(extensions=markdown.load_extensions(extensions),
- safe_mode=safe_mode,
- output_format=output_format)
- md.parser.blockprocessors['setextheader'] = CustomSetextHeaderProcessor(md.parser)
+ md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode)
return md.convert(text)
except ImportError:
diff --git a/djangorestframework/tests/description.py b/djangorestframework/tests/description.py
index 1ce29112..17bb4330 100644
--- a/djangorestframework/tests/description.py
+++ b/djangorestframework/tests/description.py
@@ -20,16 +20,16 @@ indented
# hash style header #"""
# If markdown is installed we also test it's working (and that our wrapped forces '=' to h2 and '-' to h3)
-MARKED_DOWN = """an example docstring
+MARKED_DOWN = """an example docstring
-another header
+
code block
indented
-"""
+"""
class TestViewNamesAndDescriptions(TestCase):
--
cgit v1.2.3
From 90ddec03b7e6c3236d44eafe1a3c17db7a662780 Mon Sep 17 00:00:00 2001
From: Marko Tibold
Date: Wed, 21 Dec 2011 01:06:24 +0100
Subject: Fixes #94
Modified alazaro's commit sot that both markdown < 2.1 and >= 2.1 are
supported
The test checks if either matches the old or the new style.
---
djangorestframework/compat.py | 41 ++++++++++++++++++++++++++++++--
djangorestframework/tests/description.py | 22 ++++++++++++++---
2 files changed, 58 insertions(+), 5 deletions(-)
diff --git a/djangorestframework/compat.py b/djangorestframework/compat.py
index b7eedf85..ab0d80f3 100644
--- a/djangorestframework/compat.py
+++ b/djangorestframework/compat.py
@@ -374,7 +374,35 @@ else:
# Markdown is optional
try:
import markdown
-
+
+ class CustomSetextHeaderProcessor(markdown.blockprocessors.BlockProcessor):
+ """
+ Class for markdown < 2.1
+
+ Override `markdown`'s :class:`SetextHeaderProcessor`, so that ==== headers are and ---- heade
+
+ We use for the resource name.
+ """
+ import re
+ # Detect Setext-style header. Must be first 2 lines of block.
+ RE = re.compile(r'^.*?\n[=-]{3,}', re.MULTILINE)
+
+ def test(self, parent, block):
+ return bool(self.RE.match(block))
+
+ def run(self, parent, blocks):
+ lines = blocks.pop(0).split('\n')
+ # Determine level. ``=`` is 1 and ``-`` is 2.
+ if lines[1].startswith('='):
+ level = 2
+ else:
+ level = 3
+ h = markdown.etree.SubElement(parent, 'h%d' % level)
+ h.text = lines[0].strip()
+ if len(lines) > 2:
+ # Block contains additional lines. Add to master blocks for later.
+ blocks.insert(0, '\n'.join(lines[2:]))
+
def apply_markdown(text):
"""
Simple wrapper around :func:`markdown.markdown` to set the base level
@@ -384,7 +412,15 @@ try:
extensions = ['headerid(level=2)']
safe_mode = False,
- md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode)
+ if markdown.version < (2, 1):
+ output_format = markdown.DEFAULT_OUTPUT_FORMAT
+
+ md = markdown.Markdown(extensions=markdown.load_extensions(extensions),
+ safe_mode=safe_mode,
+ output_format=output_format)
+ md.parser.blockprocessors['setextheader'] = CustomSetextHeaderProcessor(md.parser)
+ else:
+ md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode)
return md.convert(text)
except ImportError:
@@ -395,3 +431,4 @@ try:
import yaml
except ImportError:
yaml = None
+
diff --git a/djangorestframework/tests/description.py b/djangorestframework/tests/description.py
index 17bb4330..c0424c24 100644
--- a/djangorestframework/tests/description.py
+++ b/djangorestframework/tests/description.py
@@ -19,8 +19,22 @@ indented
# hash style header #"""
-# If markdown is installed we also test it's working (and that our wrapped forces '=' to h2 and '-' to h3)
-MARKED_DOWN = """an example docstring
+# If markdown is installed we also test it's working
+# (and that our wrapped forces '=' to h2 and '-' to h3)
+
+# We support markdown < 2.1 and markdown >= 2.1
+MARKED_DOWN_lt_21 = """an example docstring
+
+another header
+code block
+
+
indented
+"""
+
+MARKED_DOWN_gte_21 = """an example docstring
- list
- list
@@ -92,4 +106,6 @@ class TestViewNamesAndDescriptions(TestCase):
def test_markdown(self):
"""Ensure markdown to HTML works as expected"""
if apply_markdown:
- self.assertEquals(apply_markdown(DESCRIPTION), MARKED_DOWN)
+ gte_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_gte_21
+ lt_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_lt_21
+ self.assertTrue(gte_21_match or lt_21_match)
--
cgit v1.2.3