3
################################################################################
8
_escape_re = re.compile(eval(r'u"[&<>\"]|[\u0080-\uffff]+"'))
10
################################################################################
12
def _escape_sub(match):
14
entity_code = ord(match.group(0))
20
named_entitiy = htmlentitydefs.codepoint2name.get(entity_code)
21
if named_entitiy: return '&%s;' % named_entitiy
22
else: return '&#%d;' % entity_code
24
################################################################################
26
def escape(uni, codec=None):
27
if codec: uni = uni.decode(codec)
28
return _escape_re.sub(_escape_sub, uni)
32
__all__ = ['escape', 'ehtml']
34
################################################################################
36
if __name__ == '__main__':
38
'“Gross national happiness is more important”', 'utf-8'
41
print ehtml('&&', 'utf-8')
43
################################################################################
b'\\ No newline at end of file'