|
[Webware-checkins] r2785 - Webware/trunk/WebUtils
From: <updates@we...> - 2005-07-24 17:02
|
Author: echuck
Date: Sun Jul 24 11:01:51 2005
New Revision: 2785
Modified:
Webware/trunk/WebUtils/Funcs.py
Log:
Enhanced htmlEncode() to:
- ask its argument to convert to html, if the argument supports that
- convert its argument to a string, if necessary
Modified: Webware/trunk/WebUtils/Funcs.py
==============================================================================
--- Webware/trunk/WebUtils/Funcs.py (original)
+++ Webware/trunk/WebUtils/Funcs.py Sun Jul 24 11:01:51 2005
@@ -13,6 +13,8 @@
import string
+htmlForNone = '-' # used by htmlEncode.
+
htmlCodes = [
['&', '&'],
['<', '<'],
@@ -25,7 +27,19 @@
htmlCodesReversed.reverse()
-def htmlEncode(s, codes=htmlCodes):
+def htmlEncode(what, codes=htmlCodes):
+ if what is None:
+ return htmlForNone
+ if hasattr(what, 'html'):
+ # allow objects to specify their own translation to html via a method, property or attribute
+ ht = what.html
+ if callable(ht):
+ ht = ht()
+ return ht
+ what = str(what)
+ return htmlEncodeStr(what, codes)
+
+def htmlEncodeStr(s, codes=htmlCodes):
""" Returns the HTML encoded version of the given string. This is useful to display a plain ASCII text string on a web page."""
for code in codes:
s = string.replace(s, code[0], code[1])
@@ -98,11 +112,11 @@
"""
if not path:
return
-
+
initialslash = path[0] == '/'
lastslash = path[-1] == '/'
comps = string.split(path, '/')
-
+
newcomps = []
for comp in comps:
if comp in ('','.'):
|
| Thread | Author | Date |
|---|---|---|
| [Webware-checkins] r2785 - Webware/trunk/WebUtils | <updates@we...> |