blob: 02652ef73c312c9263a3394350c428a1bcbc09fb [file] [log] [blame]
Georg Brandl1f7fffb2010-10-15 15:57:45 +00001"""
2General functions for HTML manipulation.
3"""
4
5
6_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
7_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
8 ord('"'): '&quot;', ord('\''): '&#x27;'}
9
10# NB: this is a candidate for a bytes/string polymorphic interface
11
12def escape(s, quote=True):
13 """
14 Replace special characters "&", "<" and ">" to HTML-safe sequences.
15 If the optional flag quote is true (the default), the quotation mark
Senthil Kumarand71bbf92011-09-13 07:14:13 +080016 characters, both double quote (") and single quote (') characters are also
17 translated.
Georg Brandl1f7fffb2010-10-15 15:57:45 +000018 """
19 if quote:
20 return s.translate(_escape_map_full)
21 return s.translate(_escape_map)