2010-10-15 12:57:45 -03:00
|
|
|
"""
|
|
|
|
General functions for HTML manipulation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# NB: this is a candidate for a bytes/string polymorphic interface
|
|
|
|
|
|
|
|
def escape(s, quote=True):
|
|
|
|
"""
|
|
|
|
Replace special characters "&", "<" and ">" to HTML-safe sequences.
|
|
|
|
If the optional flag quote is true (the default), the quotation mark
|
2011-09-12 20:14:13 -03:00
|
|
|
characters, both double quote (") and single quote (') characters are also
|
|
|
|
translated.
|
2010-10-15 12:57:45 -03:00
|
|
|
"""
|
2013-07-07 06:11:24 -03:00
|
|
|
s = s.replace("&", "&") # Must be done first!
|
|
|
|
s = s.replace("<", "<")
|
|
|
|
s = s.replace(">", ">")
|
2010-10-15 12:57:45 -03:00
|
|
|
if quote:
|
2013-07-07 06:11:24 -03:00
|
|
|
s = s.replace('"', """)
|
|
|
|
s = s.replace('\'', "'")
|
|
|
|
return s
|