blob: 335d2148288fc15f8f24ce1d53387da772e2c452 [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
16 character (") is also translated.
17 """
18 if quote:
19 return s.translate(_escape_map_full)
20 return s.translate(_escape_map)