<s> from __future__ import unicode_literals <EOL> from . abc import Writer <EOL> import cgi <EOL> class HtmlWriter ( Writer ) : <EOL> def __init__ ( self ) : <EOL> self . _fragments = [ ] <EOL> def text ( self , text ) : <EOL> self . _fragments . append ( _escape_html ( text ) ) <EOL> def start ( self , name , attributes = None ) : <EOL> attribute_string = _generate_attribute_string ( attributes ) <EOL> self . _fragments . append ( "<STR_LIT>" . format ( name , attribute_string ) ) <EOL> def end ( self , name ) : <EOL> self . _fragments . append ( "<STR_LIT>" . format ( name ) ) <EOL> def self_closing ( self , name , attributes = None ) : <EOL> attribute_string = _generate_attribute_string ( attributes ) <EOL> self . _fragments . append ( "<STR_LIT>" . format ( name , attribute_string ) ) <EOL> def append ( self , html ) : <EOL> self . _fragments . append ( html ) <EOL> def as_string ( self ) : <EOL> return "<STR_LIT>" . join ( self . _fragments ) <EOL> def _escape_html ( text ) : <EOL> return cgi . escape ( text , quote = True ) <EOL> def _generate_attribute_string ( attributes ) : <EOL> if attributes is None : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" . join ( <EOL> '<STR_LIT>' . format ( key , _escape_html ( attributes [ key ] ) ) <EOL> for key in sorted ( attributes ) <EOL> ) </s>
