Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created March 5, 2010 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyattdanger/323149 to your computer and use it in GitHub Desktop.
Save wyattdanger/323149 to your computer and use it in GitHub Desktop.
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