Created
June 26, 2009 21:04
-
-
Save rtyler/136737 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
| import Cheetah.Filters | |
| class UnicodeHarder(Cheetah.Filters.Filter): | |
| def filter(self, val, | |
| encoding='utf8', | |
| str=str, | |
| **kw): | |
| """ Try our best to unicode our strings """ | |
| if not val: | |
| return u'' | |
| if isinstance(val, unicode): | |
| return val | |
| try: | |
| return val.decode('utf-8', 'strict') | |
| except UnicodeDecodeError: | |
| try: | |
| return val.decode('latin-1', 'strict') | |
| except UnicodeDecodeError: | |
| return val.decode('ascii', 'ignore') | |
| except AttributeError: | |
| return unicode(val) | |
| return val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment