Ticket #2910: 2910-2.diff

File 2910-2.diff, 1.9 KB (added by alex@…, 7 years ago)

fix for stdout+mod_wsgi problem, please review wrt. unicode changes

  • 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        extensions = [e for e in arg.split(",") if e]
     62        if len(extensions) > 0 and extensions[0] == "safe":
     63            extensions = extensions[1:]
     64            safe_mode = True
     65        else:
     66            safe_mode = False
     67        return force_unicode(markdown.markdown(value, extensions, safe_mode=safe_mode))
    4268
    4369def restructuredtext(value):
    4470    try:
Back to Top