Skip to content

Commit

Permalink
Support custom extension set in filters
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Feb 26, 2014
1 parent 3d0e0e0 commit 9568f64
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Changelog
@@ -1,3 +1,7 @@
2014-02-26 horneds

* Support custom extensions set in filters

2014-02-25 horneds dfeinzeig

* WARNING: `DJANGO_MARKDOWN_STYLE` settings renamed to `MARKDOWN_STYLE`
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 24 additions & 14 deletions django_markdown/templatetags/django_markdown.py
@@ -1,32 +1,42 @@
""" Support 'markdown' filter. """
import markdown as markdown_module
from django import template
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe

from ..settings import MARKDOWN_EXTENSIONS
from ..utils import markdown as _markdown, MARKDOWN_EXTENSIONS


register = template.Library()


@register.filter(is_safe=True)
def markdown(value):
""" Render markdown.
def markdown(value, arg=None):
""" Render markdown over a given value, optionally using varios extensions.
:returns: A rendered string
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
Syntax: ::
{{value|markdown}}
{{value|markdown:"tables,codehilite"}}
:returns: A rendered markdown
"""
return mark_safe(markdown_module.markdown(
force_text(value), extensions=MARKDOWN_EXTENSIONS, safe_mode=False))
extensions = (arg and arg.split(',')) or MARKDOWN_EXTENSIONS
return _markdown(value, extensions=extensions, safe=False)


@register.filter(is_safe=True)
def markdown_safe(value):
""" Safe Rendering markdown.
def markdown_safe(value, arg=None):
""" Render markdown over a given value, optionally using varios extensions.
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
Enables safe mode, which strips raw HTML and only returns HTML generated
by markdown.
:returns: A rendered string.
:returns: A rendered markdown.
"""
return mark_safe(markdown_module.markdown(
force_text(value), extensions=MARKDOWN_EXTENSIONS, safe_mode=True))
extensions = (arg and arg.split(',')) or MARKDOWN_EXTENSIONS
return _markdown(value, extensions=extensions, safe=True)
15 changes: 10 additions & 5 deletions django_markdown/tests.py
Expand Up @@ -4,19 +4,24 @@
class DjangoMarkdownTestCase(TestCase):

def test_base(self):
self.assertTrue(True)
from django_markdown.utils import markdown

self.assertEqual(markdown('**test**'), '<p><strong>test</strong></p>')

def test_filters(self):
from django_markdown.templatetags.django_markdown import markdown

self.assertEqual(markdown('| header |\n| ---- |\n| data |', 'tables'), '<table>\n<thead>\n<tr>\n<th>header</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>data</td>\n</tr>\n</tbody>\n</table>')

def test_preview_view(self):
response = self.client.get('/markdown/preview/')
self.assertContains(response, 'No content posted')
self.assertContains(response, 'preview.css')

response = self.client.get('/markdown/preview/', data=dict(
data="# header \n *test*"
))
data="# header \n *test*"))
self.assertContains(response, '<h1>header</h1>')

response = self.client.post('/markdown/preview/', data=dict(
data="# header \n *test*"
))
data="# header \n *test*"))
self.assertContains(response, '<h1>header</h1>')
18 changes: 18 additions & 0 deletions django_markdown/utils.py
@@ -0,0 +1,18 @@
""" Markdown utils. """
import markdown as markdown_module
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe

from .settings import MARKDOWN_EXTENSIONS


def markdown(value, extensions=MARKDOWN_EXTENSIONS, safe=False):
""" Render markdown over a given value, optionally using varios extensions.
Default extensions could be defined which MARKDOWN_EXTENSIONS option.
:returns: A rendered markdown
"""
return mark_safe(markdown_module.markdown(
force_text(value), extensions=extensions, safe_mode=safe))
2 changes: 1 addition & 1 deletion pylama.ini
Expand Up @@ -9,7 +9,7 @@ lint_ignore=C0110,W0401
lint_ignore=C0110

[*/tests.py]
lint_ignore=C0110
lint_ignore=C0110,C0111,E501,C0301

[*/settings.py]
lint_ignore=E501,C0301
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -26,7 +26,7 @@ def read(fname):

META_DATA = dict(
name=PROJECT,
VERSION=VERSION,
version=VERSION,
description=read('DESCRIPTION'),
long_description=read('README.rst'),
license=LICENSE,
Expand Down

0 comments on commit 9568f64

Please sign in to comment.