<s> """<STR_LIT>""" <EOL> from pyth import document <EOL> from pyth . format import PythWriter <EOL> from cStringIO import StringIO <EOL> class RSTWriter ( PythWriter ) : <EOL> @ classmethod <EOL> def write ( klass , document , target = None ) : <EOL> if target is None : <EOL> target = StringIO ( ) <EOL> writer = RSTWriter ( document , target ) <EOL> return writer . go ( ) <EOL> def __init__ ( self , doc , target ) : <EOL> self . document = doc <EOL> self . target = target <EOL> self . indent = - <NUM_LIT:1> <EOL> self . paragraphDispatch = { document . List : self . list , <EOL> document . Paragraph : self . paragraph } <EOL> def go ( self ) : <EOL> for ( i , paragraph ) in enumerate ( self . document . content ) : <EOL> handler = self . paragraphDispatch [ paragraph . __class__ ] <EOL> handler ( paragraph ) <EOL> self . target . write ( "<STR_LIT:\n>" ) <EOL> self . target . seek ( - <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> self . target . truncate ( ) <EOL> return self . target <EOL> def text ( self , text ) : <EOL> """<STR_LIT>""" <EOL> ret = u"<STR_LIT>" . join ( text . content ) <EOL> if '<STR_LIT:url>' in text . properties : <EOL> return u"<STR_LIT>" % ret <EOL> if '<STR_LIT>' in text . properties : <EOL> return u"<STR_LIT>" % ret <EOL> if '<STR_LIT>' in text . properties : <EOL> return u"<STR_LIT>" % ret <EOL> if '<STR_LIT>' in text . properties : <EOL> return ur "<STR_LIT>" % ret <EOL> if '<STR_LIT>' in text . properties : <EOL> return ur "<STR_LIT>" % ret <EOL> return ret <EOL> def paragraph ( self , paragraph , prefix = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> content = [ ] <EOL> for text in paragraph . content : <EOL> content . append ( self . text ( text ) ) <EOL> content = u"<STR_LIT>" . join ( content ) . encode ( "<STR_LIT:utf-8>" ) <EOL> for line in content . split ( "<STR_LIT:\n>" ) : <EOL> self . target . write ( "<STR_LIT:U+0020>" * self . indent ) <EOL> self . target . write ( prefix ) <EOL> self . target . write ( line ) <EOL> self . target . write ( "<STR_LIT:\n>" ) <EOL> if prefix : <EOL> prefix = "<STR_LIT:U+0020>" <EOL> if any ( '<STR_LIT:url>' in text . properties for text in paragraph . content ) : <EOL> self . target . write ( "<STR_LIT:\n>" ) <EOL> for text in paragraph . content : <EOL> if '<STR_LIT:url>' in text . properties : <EOL> string = u"<STR_LIT>" . join ( text . content ) <EOL> url = text . properties [ '<STR_LIT:url>' ] <EOL> self . target . write ( "<STR_LIT>" % ( string , url ) ) <EOL> def list ( self , list , prefix = None ) : <EOL> """<STR_LIT>""" <EOL> self . indent += <NUM_LIT:1> <EOL> for ( i , entry ) in enumerate ( list . content ) : <EOL> for ( j , paragraph ) in enumerate ( entry . content ) : <EOL> prefix = "<STR_LIT>" if j == <NUM_LIT:0> else "<STR_LIT:U+0020>" <EOL> handler = self . paragraphDispatch [ paragraph . __class__ ] <EOL> handler ( paragraph , prefix ) <EOL> self . target . write ( "<STR_LIT:\n>" ) <EOL> self . indent -= <NUM_LIT:1> </s>
