<s> """<STR_LIT>""" <EOL> from pyth import document <EOL> from pyth . format import PythWriter <EOL> from cStringIO import StringIO <EOL> _tagNames = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:u>' , <EOL> } <EOL> class XHTMLWriter ( PythWriter ) : <EOL> @ classmethod <EOL> def write ( klass , document , target = None , cssClasses = True , pretty = False ) : <EOL> if target is None : <EOL> target = StringIO ( ) <EOL> writer = XHTMLWriter ( document , target , cssClasses , pretty ) <EOL> final = writer . go ( ) <EOL> final . seek ( <NUM_LIT:0> ) <EOL> return final <EOL> def __init__ ( self , doc , target , cssClasses = True , pretty = False ) : <EOL> self . document = doc <EOL> self . target = target <EOL> self . cssClasses = cssClasses <EOL> self . pretty = pretty <EOL> self . paragraphDispatch = { <EOL> document . List : self . _list , <EOL> document . Paragraph : self . _paragraph <EOL> } <EOL> def go ( self ) : <EOL> self . listLevel = - <NUM_LIT:1> <EOL> tag = Tag ( "<STR_LIT>" ) <EOL> for element in self . document . content : <EOL> handler = self . paragraphDispatch [ element . __class__ ] <EOL> tag . content . extend ( handler ( element ) ) <EOL> tag . render ( self . target ) <EOL> return self . target <EOL> def _paragraph ( self , paragraph ) : <EOL> p = Tag ( "<STR_LIT:p>" ) <EOL> for text in paragraph . content : <EOL> p . content . append ( self . _text ( text ) ) <EOL> if self . pretty : <EOL> return [ _prettyBreak , p , _prettyBreak ] <EOL> else : <EOL> return [ p ] <EOL> def _list ( self , lst ) : <EOL> self . listLevel += <NUM_LIT:1> <EOL> ul = Tag ( "<STR_LIT>" ) <EOL> if self . cssClasses : <EOL> ul . attrs [ '<STR_LIT:class>' ] = '<STR_LIT>' % self . listLevel <EOL> for entry in lst . content : <EOL> li = Tag ( "<STR_LIT>" ) <EOL> for element in entry . content : <EOL> handler = self . paragraphDispatch [ element . __class__ ] <EOL> li . content . extend ( handler ( element ) ) <EOL> ul . content . append ( li ) <EOL> self . listLevel -= <NUM_LIT:1> <EOL> return [ ul ] <EOL> def _text ( self , text ) : <EOL> if '<STR_LIT:url>' in text . properties : <EOL> tag = Tag ( "<STR_LIT:a>" ) <EOL> tag . attrs [ '<STR_LIT>' ] = text . properties [ '<STR_LIT:url>' ] <EOL> else : <EOL> tag = Tag ( None ) <EOL> current = tag <EOL> for prop in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if prop in text . properties : <EOL> newTag = Tag ( _tagNames [ prop ] ) <EOL> current . content . append ( newTag ) <EOL> current = newTag <EOL> for prop in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if prop in text . properties : <EOL> if current . tag is None : <EOL> newTag = Tag ( "<STR_LIT>" ) <EOL> current . content . append ( newTag ) <EOL> current = newTag <EOL> current . attrs [ '<STR_LIT>' ] = "<STR_LIT>" % prop <EOL> current . content . append ( u"<STR_LIT>" . join ( text . content ) ) <EOL> return tag <EOL> _prettyBreak = object ( ) <EOL> class Tag ( object ) : <EOL> def __init__ ( self , tag , attrs = None , content = None ) : <EOL> self . tag = tag <EOL> self . attrs = attrs or { } <EOL> self . content = content or [ ] <EOL> def render ( self , target ) : <EOL> if self . tag is not None : <EOL> attrString = self . attrString ( ) <EOL> if attrString : <EOL> attrString = "<STR_LIT:U+0020>" + attrString <EOL> target . write ( '<STR_LIT>' % ( self . tag , attrString ) ) <EOL> for c in self . content : <EOL> if isinstance ( c , Tag ) : <EOL> c . render ( target ) <EOL> elif c is _prettyBreak : <EOL> target . write ( '<STR_LIT:\n>' ) <EOL> else : <EOL> target . write ( quoteText ( c ) . encode ( "<STR_LIT:utf-8>" ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) ) <EOL> if self . tag is not None : <EOL> target . write ( '<STR_LIT>' % self . tag ) <EOL> def attrString ( self ) : <EOL> return "<STR_LIT:U+0020>" . join ( <EOL> '<STR_LIT>' % ( k , quoteAttr ( v ) ) <EOL> for ( k , v ) in self . attrs . iteritems ( ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . tag , repr ( self . content ) ) <EOL> def quoteText ( text ) : <EOL> return text . replace ( <EOL> u"<STR_LIT:&>" , u"<STR_LIT>" ) . replace ( <EOL> u"<STR_LIT:<>" , u"<STR_LIT>" ) . replace ( <EOL> u"<STR_LIT:>>" , u"<STR_LIT>" ) <EOL> def quoteAttr ( text ) : <EOL> return quoteText ( text ) . replace ( <EOL> u'<STR_LIT:">' , u"<STR_LIT>" ) . replace ( <EOL> u"<STR_LIT:'>" , u"<STR_LIT>" ) </s>
