Created
March 5, 2010 21:04
-
-
Save wyattdanger/323149 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from re import sub | |
from django import template | |
register = template.Library() | |
@register.filter(name='cleanhtml') | |
def cleanhtml(value): | |
"""Removes all attributes from all HTML tags, but keeps <img> tags intact. | |
Ideal for cleaning WYSIWYG-generated mess.""" | |
return sub('(<\w+(\ssrc=[\'"][\w:/\.-]+[\'"])?)[^>(/>)]*(>)', r'\1\2', str(value)) | |
# Example usage: {{ article.content|cleanhtml|safe }} | |
# Issues: form elements such as inputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment