aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/markdownwrapper.py
diff options
context:
space:
mode:
authortom christie tom@tomchristie.com2011-02-19 10:26:27 +0000
committertom christie tom@tomchristie.com2011-02-19 10:26:27 +0000
commit805aa03ec1871f6a766d9052b348ddce9e9843c3 (patch)
tree8ab5b6a7396236aa45bbc61e8404cc77fc75a9c5 /djangorestframework/markdownwrapper.py
parentb749b950a1b4bede76b7e3900a6385779904902d (diff)
downloaddjango-rest-framework-805aa03ec1871f6a766d9052b348ddce9e9843c3.tar.bz2
Yowzers. Final big bunch of refactoring for 0.1 release. Now support Django 1.3's views, admin style api is all polished off, loads of tests, new test project for running the test. All sorts of goodness. Getting ready to push this out now.
Diffstat (limited to 'djangorestframework/markdownwrapper.py')
-rw-r--r--djangorestframework/markdownwrapper.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/djangorestframework/markdownwrapper.py b/djangorestframework/markdownwrapper.py
new file mode 100644
index 00000000..70512440
--- /dev/null
+++ b/djangorestframework/markdownwrapper.py
@@ -0,0 +1,51 @@
+"""If python-markdown is installed expose an apply_markdown(text) function,
+to convert markeddown text into html. Otherwise just set apply_markdown to None.
+
+See: http://www.freewisdom.org/projects/python-markdown/
+"""
+
+__all__ = ['apply_markdown']
+
+try:
+ 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:
+ apply_markdown = None \ No newline at end of file