diff options
| author | Tom Christie | 2011-04-29 14:32:56 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-04-29 14:32:56 +0100 |
| commit | b358fbdbe9cbd4ce644c4b2c7b9b4cec0811e14e (patch) | |
| tree | 601e5576995809b74f914cafcee8a7a8cd9c6937 /djangorestframework/compat.py | |
| parent | 93aa065fa92f64472a3ee80564020a81776be742 (diff) | |
| download | django-rest-framework-b358fbdbe9cbd4ce644c4b2c7b9b4cec0811e14e.tar.bz2 | |
More refactoring - move various less core stuff into utils etc
Diffstat (limited to 'djangorestframework/compat.py')
| -rw-r--r-- | djangorestframework/compat.py | 66 |
1 files changed, 59 insertions, 7 deletions
diff --git a/djangorestframework/compat.py b/djangorestframework/compat.py index 22b57186..98fbbb62 100644 --- a/djangorestframework/compat.py +++ b/djangorestframework/compat.py @@ -1,9 +1,24 @@ """Compatability module to provide support for backwards compatability with older versions of django/python""" +# cStringIO only if it's available +try: + import cStringIO as StringIO +except ImportError: + import StringIO + + +# parse_qs +try: + # python >= ? + from urlparse import parse_qs +except ImportError: + # python <= ? + from cgi import parse_qs + + # django.test.client.RequestFactory (Django >= 1.3) try: from django.test.client import RequestFactory - except ImportError: from django.test import Client from django.core.handlers.wsgi import WSGIRequest @@ -49,7 +64,7 @@ except ImportError: # django.views.generic.View (Django >= 1.3) try: from django.views.generic import View -except: +except ImportError: from django import http from django.utils.functional import update_wrapper # from django.utils.log import getLogger @@ -127,10 +142,47 @@ except: #) return http.HttpResponseNotAllowed(allowed_methods) -# parse_qs + try: - # python >= ? - from urlparse import parse_qs + import markdown + import re + + class CustomSetextHeaderProcessor(markdown.blockprocessors.BlockProcessor): + """Override markdown's SetextHeaderProcessor, so that ==== headers are <h2> and ---- headers are <h3>. + + We use <h1> 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 markdown.markdown to apply our CustomSetextHeaderProcessor, + and also 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, + output_format=output_format) + md.parser.blockprocessors['setextheader'] = CustomSetextHeaderProcessor(md.parser) + return md.convert(text) + except ImportError: - # python <= ? - from cgi import parse_qs
\ No newline at end of file + apply_markdown = None
\ No newline at end of file |
