From cd8372960d6224fb08a1092b2964e77ebdf488b9 Mon Sep 17 00:00:00 2001 From: Natim Date: Wed, 11 Apr 2012 16:23:47 +0200 Subject: [PATCH] When markdown is very old, markdown.blocktrans doesn't exists --- djangorestframework/compat.py | 59 +++++++++++++++++------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/djangorestframework/compat.py b/djangorestframework/compat.py index 1d84ee185..cb01ccec9 100644 --- a/djangorestframework/compat.py +++ b/djangorestframework/compat.py @@ -371,6 +371,34 @@ else: 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 @@ -381,35 +409,6 @@ try: safe_mode = False, if markdown.version_info < (2, 1): - 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:])) - - output_format = markdown.DEFAULT_OUTPUT_FORMAT md = markdown.Markdown(extensions=markdown.load_extensions(extensions), @@ -420,7 +419,7 @@ try: md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode) return md.convert(text) -except ImportError: +except (ImportError, AttributeError): apply_markdown = None # Yaml is optional