Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More generic "extensions" parameter for Markdown #406

Closed
wants to merge 9 commits into from
13 changes: 11 additions & 2 deletions zinnia/markups.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import warnings

from django.utils import six
from django.utils.encoding import force_text
from django.utils.encoding import force_bytes

Expand All @@ -30,6 +31,12 @@ def markdown(value, extensions=MARKDOWN_EXTENSIONS):
"""
Markdown processing with optionally using various extensions
that python-markdown supports.
`extensions` may be a string of comma-separated extension paths
or an iterable of either markdown.Extension instances or
extension paths.
Samples:
'markdown.extensions.nl2br,markdown.extension.toc'
['markdown.extensions.nl2br', MyExtension(mysetting="foo")]
"""
try:
import markdown
Expand All @@ -38,9 +45,11 @@ def markdown(value, extensions=MARKDOWN_EXTENSIONS):
RuntimeWarning)
return value

extensions = [e for e in extensions.split(',') if e]
if isinstance(extensions, six.string_types):
extensions = [e for e in extensions.split(',') if e]

return markdown.markdown(force_text(value),
extensions, safe_mode=False)
extensions=extensions, safe_mode=False)


def restructuredtext(value, settings=RESTRUCTUREDTEXT_SETTINGS):
Expand Down
15 changes: 15 additions & 0 deletions zinnia/tests/test_markups.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_markdown(self):

@skipUnless(is_lib_available('markdown'), 'Markdown is not available')
def test_markdown_extensions(self):
from markdown.extensions.toc import TocExtension
text = '[TOC]\n\n# Header 1\n\n## Header 2'
self.assertEqual(markdown(text).strip(),
'<p>[TOC]</p>\n<h1>Header 1</h1>'
Expand All @@ -40,6 +41,20 @@ def test_markdown_extensions(self):
'Header 2</a></li>\n</ul>\n</li>\n</ul>\n</div>'
'\n<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>')
self.assertEqual(markdown(text, extensions=['toc']).strip(),
'<div class="toc">\n<ul>\n<li><a href="#header-1">'
'Header 1</a><ul>\n<li><a href="#header-2">'
'Header 2</a></li>\n</ul>\n</li>\n</ul>\n</div>'
'\n<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>')
tocext = TocExtension(marker='--TOC--', permalink='PL')
self.assertEqual(markdown(text, extensions=[tocext]).strip(),
'<p>[TOC]</p>\n<h1 id="header-1">Header 1'
'<a class="headerlink" href="#header-1" '
'title="Permanent link">PL</a></h1>\n'
'<h2 id="header-2">Header 2'
'<a class="headerlink" href="#header-2" '
'title="Permanent link">PL</a></h2>')

@skipUnless(is_lib_available('docutils'), 'Docutils is not available')
def test_restructuredtext(self):
Expand Down