<s> import re , urllib , htmlentitydefs , codecs <EOL> from StringIO import StringIO <EOL> from mako import util <EOL> xml_escapes = { <EOL> '<STR_LIT:&>' : '<STR_LIT>' , <EOL> '<STR_LIT:>>' : '<STR_LIT>' , <EOL> '<STR_LIT:<>' : '<STR_LIT>' , <EOL> '<STR_LIT:">' : '<STR_LIT>' , <EOL> "<STR_LIT:'>" : '<STR_LIT>' <EOL> } <EOL> def legacy_html_escape ( string ) : <EOL> """<STR_LIT>""" <EOL> return re . sub ( r'<STR_LIT>' , lambda m : xml_escapes [ m . group ( ) ] , string ) <EOL> try : <EOL> import markupsafe <EOL> html_escape = markupsafe . escape <EOL> except ImportError : <EOL> html_escape = legacy_html_escape <EOL> def xml_escape ( string ) : <EOL> return re . sub ( r'<STR_LIT>' , lambda m : xml_escapes [ m . group ( ) ] , string ) <EOL> def url_escape ( string ) : <EOL> string = string . encode ( "<STR_LIT:utf8>" ) <EOL> return urllib . quote_plus ( string ) <EOL> def url_unescape ( string ) : <EOL> text = urllib . unquote_plus ( string ) <EOL> if not is_ascii_str ( text ) : <EOL> text = text . decode ( "<STR_LIT:utf8>" ) <EOL> return text <EOL> def trim ( string ) : <EOL> return string . strip ( ) <EOL> class Decode ( object ) : <EOL> def __getattr__ ( self , key ) : <EOL> def decode ( x ) : <EOL> if isinstance ( x , unicode ) : <EOL> return x <EOL> elif not isinstance ( x , str ) : <EOL> return unicode ( str ( x ) , encoding = key ) <EOL> else : <EOL> return unicode ( x , encoding = key ) <EOL> return decode <EOL> decode = Decode ( ) <EOL> _ASCII_re = re . compile ( r'<STR_LIT>' ) <EOL> def is_ascii_str ( text ) : <EOL> return isinstance ( text , str ) and _ASCII_re . match ( text ) <EOL> class XMLEntityEscaper ( object ) : <EOL> def __init__ ( self , codepoint2name , name2codepoint ) : <EOL> self . codepoint2entity = dict ( [ ( c , u'<STR_LIT>' % n ) <EOL> for c , n in codepoint2name . iteritems ( ) ] ) <EOL> self . name2codepoint = name2codepoint <EOL> def escape_entities ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return unicode ( text ) . translate ( self . codepoint2entity ) <EOL> def __escape ( self , m ) : <EOL> codepoint = ord ( m . group ( ) ) <EOL> try : <EOL> return self . codepoint2entity [ codepoint ] <EOL> except ( KeyError , IndexError ) : <EOL> return '<STR_LIT>' % codepoint <EOL> __escapable = re . compile ( r'<STR_LIT>' ) <EOL> def escape ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return self . __escapable . sub ( self . __escape , unicode ( text ) <EOL> ) . encode ( '<STR_LIT:ascii>' ) <EOL> __characterrefs = re . compile ( r'''<STR_LIT>''' , <EOL> re . X | re . UNICODE ) <EOL> def __unescape ( self , m ) : <EOL> dval , hval , name = m . groups ( ) <EOL> if dval : <EOL> codepoint = int ( dval ) <EOL> elif hval : <EOL> codepoint = int ( hval , <NUM_LIT:16> ) <EOL> else : <EOL> codepoint = self . name2codepoint . get ( name , <NUM_LIT> ) <EOL> if codepoint < <NUM_LIT> : <EOL> return chr ( codepoint ) <EOL> return unichr ( codepoint ) <EOL> def unescape ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return self . __characterrefs . sub ( self . __unescape , text ) <EOL> _html_entities_escaper = XMLEntityEscaper ( htmlentitydefs . codepoint2name , <EOL> htmlentitydefs . name2codepoint ) <EOL> html_entities_escape = _html_entities_escaper . escape_entities <EOL> html_entities_unescape = _html_entities_escaper . unescape <EOL> def htmlentityreplace_errors ( ex ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( ex , UnicodeEncodeError ) : <EOL> bad_text = ex . object [ ex . start : ex . end ] <EOL> text = _html_entities_escaper . escape ( bad_text ) <EOL> return ( unicode ( text ) , ex . end ) <EOL> raise ex <EOL> codecs . register_error ( '<STR_LIT>' , htmlentityreplace_errors ) <EOL> DEFAULT_ESCAPES = { <EOL> '<STR_LIT:x>' : '<STR_LIT>' , <EOL> '<STR_LIT:h>' : '<STR_LIT>' , <EOL> '<STR_LIT:u>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:str>' : '<STR_LIT:str>' , <EOL> '<STR_LIT:n>' : '<STR_LIT:n>' <EOL> } <EOL> if util . py3k : <EOL> DEFAULT_ESCAPES . update ( { <EOL> '<STR_LIT>' : '<STR_LIT:str>' <EOL> } ) <EOL> NON_UNICODE_ESCAPES = DEFAULT_ESCAPES . copy ( ) <EOL> NON_UNICODE_ESCAPES [ '<STR_LIT:h>' ] = '<STR_LIT>' </s>
