|
From: <sub...@co...> - 2007-05-08 22:33:35
|
Author: ianb
Date: 2007-05-08 16:33:34 -0600 (Tue, 08 May 2007)
New Revision: 2661
Modified:
FormEncode/trunk/docs/news.txt
FormEncode/trunk/formencode/htmlfill.py
Log:
Added encoding parameter to htmlfill.htmlrender
Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt 2007-05-08 15:07:11 UTC (rev 2660)
+++ FormEncode/trunk/docs/news.txt 2007-05-08 22:33:34 UTC (rev 2661)
@@ -3,6 +3,12 @@
.. contents::
+svn trunk
+---------
+
+* Added encoding parameter to htmlfill, which will handle cases when mixed
+ str and unicode objects are used (turning all str objects into unicode)
+
0.7.1
-----
Modified: FormEncode/trunk/formencode/htmlfill.py
===================================================================
--- FormEncode/trunk/formencode/htmlfill.py 2007-05-08 15:07:11 UTC (rev 2660)
+++ FormEncode/trunk/formencode/htmlfill.py 2007-05-08 22:33:34 UTC (rev 2661)
@@ -14,7 +14,7 @@
def render(form, defaults=None, errors=None, use_all_keys=False,
error_formatters=None, add_attributes=None,
auto_insert_errors=True, auto_error_formatter=None,
- text_as_default=False, listener=None):
+ text_as_default=False, listener=None, encoding=None):
"""
Render the ``form`` (which should be a string) given the defaults
and errors. Defaults are the values that go in the input fields
@@ -52,6 +52,9 @@
``listener`` can be an object that watches fields pass; the only
one currently is in ``htmlfill_schemabuilder.SchemaBuilder``
+
+ ``encoding`` specifies an encoding to assume when mixing str and
+ unicode text in the template.
"""
if defaults is None:
defaults = {}
@@ -64,7 +67,7 @@
add_attributes=add_attributes,
auto_error_formatter=auto_error_formatter,
text_as_default=text_as_default,
- listener=listener,
+ listener=listener, encoding=encoding,
)
p.feed(form)
p.close()
@@ -166,7 +169,7 @@
error_formatters=None, error_class='error',
add_attributes=None, listener=None,
auto_error_formatter=None,
- text_as_default=False):
+ text_as_default=False, encoding=None):
HTMLParser.HTMLParser.__init__(self)
self._content = []
self.source = None
@@ -193,6 +196,7 @@
self.listener = listener
self.auto_error_formatter = auto_error_formatter
self.text_as_default = text_as_default
+ self.encoding = encoding
def feed(self, data):
self.data_is_str = isinstance(data, str)
@@ -231,6 +235,13 @@
assert False, (
"These errors were not used in the form: %s" %
', '.join(error_text))
+ if self.encoding is not None:
+ new_content = []
+ for item in self._content:
+ if isinstance(item, str):
+ item = item.decode(self.encoding)
+ new_content.append(item)
+ self._content = new_content
try:
self._text = ''.join([
t for t in self._content if not isinstance(t, tuple)])
|