Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #7: Resolved unicode decoding exception in simple_str_filte… #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions spitfire/runtime/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ def simple_str_filter(value):
"""Return a string if the input type is something primitive."""
if isinstance(value, (str, unicode, int, long, float,
runtime.UndefinedPlaceholder)):
# fixme: why do force this conversion here?
# do we want to be unicode or str?
return str(value)
# Convert Unicode to string if necessary
if isinstance(value, unicode):
return value.encode('utf-8')
else:
return str(value)
else:
return ''

Expand Down
21 changes: 15 additions & 6 deletions spitfire/text.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# Copyright 2007 The Spitfire Authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# test.py

import re
import string
import unicodedata

normal_characters = string.lowercase + string.uppercase
normal_characters = string.ascii_lowercase + string.ascii_uppercase
mangled_character_names = [
'LATIN SMALL LETTER A WITH RING ABOVE',
'LATIN SMALL LETTER THORN',
Expand Down Expand Up @@ -73,8 +70,20 @@ def i18n_mangled_message(msg):
return ''.join([char_map.get(c, c) for c in msg])


whitespace_regex = re.compile('\s+', re.UNICODE)
whitespace_regex = re.compile(r'\s+', re.UNICODE)


def normalize_whitespace(text):
return whitespace_regex.sub(' ', text)


# Example usage:
if __name__ == "__main__":
ascii_message = "Hello, world!"
unicode_message = i18n_mangled_message(ascii_message)
print("Original message:", ascii_message)
print("Mangled message:", unicode_message)
text_with_whitespace = " This is a test with whitespace "
normalized_text = normalize_whitespace(text_with_whitespace)
print("Original text:", text_with_whitespace)
print("Normalized text:", normalized_text)