mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 21:24:33 +03:00
Merge branch 'master' of https://github.com/alazaro/django-rest-framework into alazaro-master
This commit is contained in:
commit
1c8b40fb5f
|
@ -374,48 +374,17 @@ else:
|
||||||
# Markdown is optional
|
# Markdown is optional
|
||||||
try:
|
try:
|
||||||
import markdown
|
import markdown
|
||||||
import re
|
|
||||||
|
|
||||||
class CustomSetextHeaderProcessor(markdown.blockprocessors.BlockProcessor):
|
|
||||||
"""
|
|
||||||
Override `markdown`'s :class:`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):
|
def apply_markdown(text):
|
||||||
"""
|
"""
|
||||||
Simple wrapper around :func:`markdown.markdown` to apply our :class:`CustomSetextHeaderProcessor`,
|
Simple wrapper around :func:`markdown.markdown` to set the base level
|
||||||
and also set the base level of '#' style headers to <h2>.
|
of '#' style headers to <h2>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
extensions = ['headerid(level=2)']
|
extensions = ['headerid(level=2)']
|
||||||
safe_mode = False,
|
safe_mode = False,
|
||||||
output_format = markdown.DEFAULT_OUTPUT_FORMAT
|
|
||||||
|
|
||||||
md = markdown.Markdown(extensions=markdown.load_extensions(extensions),
|
md = markdown.Markdown(extensions=extensions, safe_mode=safe_mode)
|
||||||
safe_mode=safe_mode,
|
|
||||||
output_format=output_format)
|
|
||||||
md.parser.blockprocessors['setextheader'] = CustomSetextHeaderProcessor(md.parser)
|
|
||||||
return md.convert(text)
|
return md.convert(text)
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
|
@ -20,16 +20,16 @@ indented
|
||||||
# hash style header #"""
|
# hash style header #"""
|
||||||
|
|
||||||
# If markdown is installed we also test it's working (and that our wrapped forces '=' to h2 and '-' to h3)
|
# If markdown is installed we also test it's working (and that our wrapped forces '=' to h2 and '-' to h3)
|
||||||
MARKED_DOWN = """<h2>an example docstring</h2>
|
MARKED_DOWN = """<h2 id="an-example-docstring">an example docstring</h2>
|
||||||
<ul>
|
<ul>
|
||||||
<li>list</li>
|
<li>list</li>
|
||||||
<li>list</li>
|
<li>list</li>
|
||||||
</ul>
|
</ul>
|
||||||
<h3>another header</h3>
|
<h3 id="another-header">another header</h3>
|
||||||
<pre><code>code block
|
<pre><code>code block
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>indented</p>
|
<p>indented</p>
|
||||||
<h2 id="hash_style_header">hash style header</h2>"""
|
<h2 id="hash-style-header">hash style header</h2>"""
|
||||||
|
|
||||||
|
|
||||||
class TestViewNamesAndDescriptions(TestCase):
|
class TestViewNamesAndDescriptions(TestCase):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user