aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/compat.py
diff options
context:
space:
mode:
authorMarko Tibold2011-12-21 01:06:24 +0100
committerMarko Tibold2011-12-21 01:06:24 +0100
commit90ddec03b7e6c3236d44eafe1a3c17db7a662780 (patch)
treec6ee9239f6afdee3eb697edece94dc923eb0c869 /djangorestframework/compat.py
parent1c8b40fb5f4744bc0bfa4448075eeed193ab950c (diff)
downloaddjango-rest-framework-90ddec03b7e6c3236d44eafe1a3c17db7a662780.tar.bz2
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.
Diffstat (limited to 'djangorestframework/compat.py')
-rw-r--r--djangorestframework/compat.py41
1 files changed, 39 insertions, 2 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 <h2> and ---- heade
+
+ We use <h1> 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
+