"""WebUtils.Funcs
This module provides some basic functions that are useful
in HTML and web development.
You can safely import * from WebUtils.Funcs if you like.
TO DO
* Document the 'codes' arg of htmlEncode/Decode.
"""
htmlForNone = '-' # used by htmlEncode.
htmlCodes = [
['&', '&'],
['<', '<'],
['>', '>'],
['"', '"'],
# ['\n', '
'],
]
htmlCodesReversed = htmlCodes[:]
htmlCodesReversed.reverse()
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):
"""Return 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 = s.replace(code[0], code[1])
return s
def htmlDecode(s, codes=htmlCodesReversed):
"""Return the ASCII decoded version of the given HTML string.
This does NOT remove normal HTML tags like
. It is the inverse of htmlEncode(). """ for code in codes: s = s.replace(code[1], code[0]) return s _urlEncode = {} for i in range(256): c = chr(i) _urlEncode[c] = c == ' ' and '+' \ or i < 128 and (c.isalnum() or c in '_.-/') and c \ or '%%%02X' % i def urlEncode(s): """Return the encoded version of the given string. The resulting string is safe for using as a URL. Identical to urllib.quote_plus(s) in Python 2.4, but faster for older Python versions. """ return ''.join(map(_urlEncode.get, s)) _urlDecode = {} for i in range(256): _urlDecode['%02x' % i] = _urlDecode['%02X' % i] = chr(i) try: UnicodeDecodeError except NameError: # Python < 2.3 class UnicodeDecodeError(Exception): pass def urlDecode(s): """Return the decoded version of the given string. Note that invalid URLs will not throw exceptions. For example, incorrect % codings will be ignored. Identical to urllib.unquote_plus(s) in Python 2.4, but faster and more exact for older Python versions. """ s = s.replace('+', ' ').split('%') for i in xrange(1, len(s)): t = s[i] try: s[i] = _urlDecode[t[:2]] + t[2:] except KeyError: s[i] = '%' + t except UnicodeDecodeError: s[i] = unichr(int(t[:2], 16)) + t[2:] return ''.join(s) def htmlForDict(dict, addSpace=None, filterValueCallBack=None, maxValueLength=None): """Return an HTML string with a
| %s | ' '%s |