Django

Code

Changeset 6834

Show
Ignore:
Timestamp:
12/02/07 09:49:10 (2 years ago)
Author:
mtredinnick
Message:

Fixed #2910 -- Added support for markdown extensions to the markdown markup
filter (try saying that ten times quickly!). Thanks, Waylan Limberg.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/markup/templatetags/markup.py

    r6671 r6834  
    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 
     
    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