diff options
| author | Marko Tibold | 2011-12-21 01:10:10 +0100 |
|---|---|---|
| committer | Marko Tibold | 2011-12-21 01:10:10 +0100 |
| commit | 8162303bc7f0ea389d1cdf9e9eb5d5677cb5e3bf (patch) | |
| tree | 42c9de25894aef1172bd43c5062b89160d59df02 /djangorestframework/compat.py | |
| parent | 443694ebe6195547a9078b7d2586c652676e43e7 (diff) | |
| parent | 90ddec03b7e6c3236d44eafe1a3c17db7a662780 (diff) | |
| download | django-rest-framework-8162303bc7f0ea389d1cdf9e9eb5d5677cb5e3bf.tar.bz2 | |
Merge branch 'alazaro-master'
Diffstat (limited to 'djangorestframework/compat.py')
| -rw-r--r-- | djangorestframework/compat.py | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/djangorestframework/compat.py b/djangorestframework/compat.py index cae8c7b7..ab0d80f3 100644 --- a/djangorestframework/compat.py +++ b/djangorestframework/compat.py @@ -374,21 +374,22 @@ else: # Markdown is optional try: import markdown - import re - + class CustomSetextHeaderProcessor(markdown.blockprocessors.BlockProcessor): """ - Override `markdown`'s :class:`SetextHeaderProcessor`, so that ==== headers are <h2> and ---- headers are <h3>. - + 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. @@ -401,21 +402,25 @@ try: 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 <h2>. + Simple wrapper around :func:`markdown.markdown` to set the base level + of '#' style headers to <h2>. """ 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, + 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) + md.parser.blockprocessors['setextheader'] = CustomSetextHeaderProcessor(md.parser) + else: + md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode) return md.convert(text) except ImportError: @@ -426,3 +431,4 @@ try: import yaml except ImportError: yaml = None + |
