Ticket #2910: 2910-4.diff

File 2910-4.diff, 2.1 KB (added by Waylan Limberg, 17 years ago)

Patch updated to support the new auto-escaping

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

    === modified file 'django/contrib/markup/templatetags/markup.py'
     
    3232        return mark_safe(force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8')))
    3333textile.is_safe = True
    3434
    35 def markdown(value):
     35def markdown(value, arg=''):
     36    """
     37    Runs Markdown over a given value, optionally using various
     38    extensions python-markdown supports.
     39           
     40    Syntax::
     41                   
     42        {{ value|markdown:"extension1_name,extension2_name..." }}
     43                               
     44    To enable safe mode, which strips raw HTML and only returns HTML
     45    generated by actual Markdown syntax, pass "safe" as the first
     46    extension in the list.
     47                                               
     48    If the version of Markdown in use does not support extensions,
     49    they will be silently ignored.
     50                                                           
     51    """
    3652    try:
    3753        import markdown
    3854    except ImportError:
     
    4056            raise template.TemplateSyntaxError, "Error in {% markdown %} filter: The Python markdown library isn't installed."
    4157        return force_unicode(value)
    4258    else:
    43         return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
     59        # markdown.version was first added in 1.6b. The only version of markdown
     60        # to fully support extensions before 1.6b was the shortlived 1.6a.
     61        if hasattr(markdown, 'version'):
     62            extensions = [e for e in arg.split(",") if e]
     63            if len(extensions) > 0 and extensions[0] == "safe":
     64                extensions = extensions[1:]
     65                safe_mode = True
     66            else:
     67                safe_mode = False
     68            return mark_safe(force_unicode(markdown.markdown(smart_str(value), extensions, safe_mode=safe_mode)))
     69        else:
     70            return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
    4471markdown.is_safe = True
    4572
    4673def restructuredtext(value):
Back to Top