Ticket #2910: 2910-3.diff

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

Address older versions of markdown and unicode support.

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

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