|
From: <sub...@co...> - 2005-12-07 21:06:59
|
Author: test
Date: 2005-12-07 21:06:36 +0000 (Wed, 07 Dec 2005)
New Revision: 1370
Modified:
FormEncode/trunk/docs/news.txt
FormEncode/trunk/formencode/htmlfill.py
Log:
Added a new formatter
Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt 2005-12-07 20:37:00 UTC (rev 1369)
+++ FormEncode/trunk/docs/news.txt 2005-12-07 21:06:36 UTC (rev 1370)
@@ -3,6 +3,15 @@
.. contents::
+SVN trunk
+---------
+
+* Added ``htmlfill.default_formatter_dict``, and you can poke new
+ formatters in there to effective register them.
+
+* Added an ``escapenl`` formatter (nl=newline) that escapes HTML and
+ turns newlines into ``<br>``.
+
0.4
---
Modified: FormEncode/trunk/formencode/htmlfill.py
===================================================================
--- FormEncode/trunk/formencode/htmlfill.py 2005-12-07 20:37:00 UTC (rev 1369)
+++ FormEncode/trunk/formencode/htmlfill.py 2005-12-07 21:06:36 UTC (rev 1370)
@@ -101,14 +101,32 @@
return cgi.escape(str(v), 1)
def default_formatter(error):
+ """
+ Formatter that escapes the error, wraps the error in a span with
+ class ``error-message``, and adds a ``<br>``
+ """
return '<span class="error-message">%s</span><br />\n' % html_quote(error)
def none_formatter(error):
+ """
+ Formatter that does nothing, no escaping HTML, nothin'
+ """
return error
def escape_formatter(error):
+ """
+ Formatter that escapes HTML, no more.
+ """
return html_quote(error)
+def escapenl_formatter(error):
+ """
+ Formatter that escapes HTML, and translates newlines to ``<br>``
+ """
+ error = html_quote(error)
+ error = error.replace('\n', '<br>\n')
+ return error
+
class FillingParser(HTMLParser.HTMLParser):
r"""
Fills HTML with default values, as in a form.
@@ -164,9 +182,7 @@
self.used_keys = {}
self.used_errors = {}
if error_formatters is None:
- self.error_formatters = {'default': default_formatter,
- 'none': none_formatter,
- 'escape': escape_formatter}
+ self.error_formatters = default_formatter_dict
else:
self.error_formatters = error_formatters
self.error_class = error_class
@@ -500,3 +516,9 @@
raise Exception(
"You must .close() a parser instance before getting "
"the text from it")
+
+# This can potentially be extended globally
+default_formatter_dict = {'default': default_formatter,
+ 'none': none_formatter,
+ 'escape': escape_formatter,
+ 'escapenl': escapenl_formatter}
|