Ticket #2910: 2910.diff

File 2910.diff, 1.9 KB (added by James Bennett, 18 years ago)

Alternate version of other patches

  • django/contrib/markup/templatetags/markup.py

     
    2929    else:
    3030        return textile.textile(value, encoding=settings.DEFAULT_CHARSET, output=settings.DEFAULT_CHARSET)
    3131
    32 def markdown(value):
     32def markdown(value, arg=''):
     33    """
     34    Runs Markdown over a given value, optionally using various
     35    extensions python-markdown supports.
     36   
     37    Syntax::
     38   
     39        {{ value|markdown:"extension1_name,extension2_name..." }}
     40   
     41    To enable safe mode, which strips raw HTML and only returns HTML
     42    generated by actual Markdown syntax, pass "safe" as the first
     43    extension in the list.
     44   
     45    If the version of Markdown in use does not support extensions,
     46    they will be silently ignored.
     47   
     48    """
    3349    try:
    3450        import markdown
    3551    except ImportError:
    3652        if settings.DEBUG:
    37             raise template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed."
    38         return value
     53            raise template.TemplateSyntaxError('Error in markdown filter: Markdown could not be imported.')
     54        else:
     55            # Try to salvage this; whatever we do, we shouldn't
     56            # return potentially unsafe HTML.
     57            from django.utils.html import escape, linebreaks
     58            return linebreaks(escape(value))
    3959    else:
    40         return markdown.markdown(value)
     60        extensions=arg.split(",")
     61        if len(extensions) > 0 and extensions[0] == "safe":
     62            extensions = extensions[1:]
     63            safe_mode = True
     64        else:
     65            safe_mode = False
     66        return markdown.markdown(value, extensions, safe_mode=safe_mode)
    4167
    4268def restructuredtext(value):
    4369    try:
Back to Top