Created
March 12, 2012 22:36
-
-
Save tkaemming/2025129 to your computer and use it in GitHub Desktop.
template tag to strip basic unicode control characters (unprintables, usually entered as artifacts of bad encoding/decoding from iso-8859 subsets like windows-1252) from template output
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
import re | |
from django import template | |
from django.template.defaultfilters import stringfilter | |
register = template.Library() | |
CONTROL_CHARACTERS = re.compile(r'([\x00-\x1f\x7f-\x9f])*') | |
@register.filter | |
@stringfilter | |
def strip_control_characters(value): | |
"""Strips Unicode control characters from input.""" | |
return CONTROL_CHARACTERS.sub('', value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment