| 33 | | def markdown(value): |
| | 33 | def 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 | """ |
| 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)) |
| 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))) |