<s> """<STR_LIT>""" <EOL> from pygments . formatter import Formatter <EOL> from pygments . token import Token , STANDARD_TYPES <EOL> from pygments . util import get_bool_opt , get_int_opt , StringIO <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def escape_tex ( text , commandprefix ) : <EOL> return text . replace ( '<STR_LIT:\\>' , '<STR_LIT:\x00>' ) . replace ( '<STR_LIT:{>' , '<STR_LIT>' ) . replace ( '<STR_LIT:}>' , '<STR_LIT>' ) . replace ( '<STR_LIT:\x00>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:_>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:&>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:<>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:>>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:#>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:%>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:$>' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:->' , r'<STR_LIT>' % commandprefix ) . replace ( "<STR_LIT:'>" , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT:">' , r'<STR_LIT>' % commandprefix ) . replace ( '<STR_LIT>' , r'<STR_LIT>' % commandprefix ) <EOL> DOC_TEMPLATE = r'''<STR_LIT>''' <EOL> STYLE_TEMPLATE = r'''<STR_LIT>''' <EOL> def _get_ttype_name ( ttype ) : <EOL> fname = STANDARD_TYPES . get ( ttype ) <EOL> if fname : <EOL> return fname <EOL> aname = '<STR_LIT>' <EOL> while fname is None : <EOL> aname = ttype [ - <NUM_LIT:1> ] + aname <EOL> ttype = ttype . parent <EOL> fname = STANDARD_TYPES . get ( ttype ) <EOL> return fname + aname <EOL> class LatexFormatter ( Formatter ) : <EOL> r"""<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> aliases = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> filenames = [ '<STR_LIT>' ] <EOL> def __init__ ( self , ** options ) : <EOL> Formatter . __init__ ( self , ** options ) <EOL> self . docclass = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . preamble = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . linenos = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> self . linenostart = abs ( get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> self . linenostep = abs ( get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> self . verboptions = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . nobackground = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> self . commandprefix = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . texcomments = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> self . mathescape = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> self . _create_stylesheet ( ) <EOL> def _create_stylesheet ( self ) : <EOL> t2n = self . ttype2name = { Token : '<STR_LIT>' } <EOL> c2d = self . cmd2def = { } <EOL> cp = self . commandprefix <EOL> def rgbcolor ( col ) : <EOL> if col : <EOL> return '<STR_LIT:U+002C>' . join ( [ '<STR_LIT>' % ( int ( col [ i ] + col [ i + <NUM_LIT:1> ] , <NUM_LIT:16> ) / <NUM_LIT> ) <EOL> for i in ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ) ] ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> for ttype , ndef in self . style : <EOL> name = _get_ttype_name ( ttype ) <EOL> cmndef = '<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += r'<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += r'<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += r'<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += r'<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += r'<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += r'<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += ( r'<STR_LIT>' % <EOL> rgbcolor ( ndef [ '<STR_LIT>' ] ) ) <EOL> if ndef [ '<STR_LIT>' ] : <EOL> cmndef += ( r'<STR_LIT>' <EOL> r'<STR_LIT>' % <EOL> ( rgbcolor ( ndef [ '<STR_LIT>' ] ) , <EOL> rgbcolor ( ndef [ '<STR_LIT>' ] ) ) ) <EOL> elif ndef [ '<STR_LIT>' ] : <EOL> cmndef += ( r'<STR_LIT>' <EOL> r'<STR_LIT>' % <EOL> rgbcolor ( ndef [ '<STR_LIT>' ] ) ) <EOL> if cmndef == '<STR_LIT>' : <EOL> continue <EOL> cmndef = cmndef . replace ( '<STR_LIT>' , cp ) <EOL> t2n [ ttype ] = name <EOL> c2d [ name ] = cmndef <EOL> def get_style_defs ( self , arg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cp = self . commandprefix <EOL> styles = [ ] <EOL> for name , definition in self . cmd2def . iteritems ( ) : <EOL> styles . append ( r'<STR_LIT>' % <EOL> ( cp , name , definition ) ) <EOL> return STYLE_TEMPLATE % { '<STR_LIT>' : self . commandprefix , <EOL> '<STR_LIT>' : '<STR_LIT:\n>' . join ( styles ) } <EOL> def format_unencoded ( self , tokensource , outfile ) : <EOL> t2n = self . ttype2name <EOL> cp = self . commandprefix <EOL> if self . full : <EOL> realoutfile = outfile <EOL> outfile = StringIO ( ) <EOL> outfile . write ( ur '<STR_LIT>' ) <EOL> if self . linenos : <EOL> start , step = self . linenostart , self . linenostep <EOL> outfile . write ( u'<STR_LIT>' + <EOL> ( start and u'<STR_LIT>' % start or u'<STR_LIT>' ) + <EOL> ( step and u'<STR_LIT>' % step or u'<STR_LIT>' ) ) <EOL> if self . mathescape or self . texcomments : <EOL> outfile . write ( ur '<STR_LIT>' ) <EOL> if self . verboptions : <EOL> outfile . write ( u'<STR_LIT:U+002C>' + self . verboptions ) <EOL> outfile . write ( u'<STR_LIT>' ) <EOL> for ttype , value in tokensource : <EOL> if ttype in Token . Comment : <EOL> if self . texcomments : <EOL> start = value [ <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> for i in xrange ( <NUM_LIT:1> , len ( value ) ) : <EOL> if start [ <NUM_LIT:0> ] != value [ i ] : <EOL> break <EOL> start += value [ i ] <EOL> value = value [ len ( start ) : ] <EOL> start = escape_tex ( start , self . commandprefix ) <EOL> value = start + value <EOL> elif self . mathescape : <EOL> parts = value . split ( '<STR_LIT:$>' ) <EOL> in_math = False <EOL> for i , part in enumerate ( parts ) : <EOL> if not in_math : <EOL> parts [ i ] = escape_tex ( part , self . commandprefix ) <EOL> in_math = not in_math <EOL> value = '<STR_LIT:$>' . join ( parts ) <EOL> else : <EOL> value = escape_tex ( value , self . commandprefix ) <EOL> else : <EOL> value = escape_tex ( value , self . commandprefix ) <EOL> styles = [ ] <EOL> while ttype is not Token : <EOL> try : <EOL> styles . append ( t2n [ ttype ] ) <EOL> except KeyError : <EOL> styles . append ( _get_ttype_name ( ttype ) ) <EOL> ttype = ttype . parent <EOL> styleval = '<STR_LIT:+>' . join ( reversed ( styles ) ) <EOL> if styleval : <EOL> spl = value . split ( '<STR_LIT:\n>' ) <EOL> for line in spl [ : - <NUM_LIT:1> ] : <EOL> if line : <EOL> outfile . write ( "<STR_LIT>" % ( cp , styleval , line ) ) <EOL> outfile . write ( '<STR_LIT:\n>' ) <EOL> if spl [ - <NUM_LIT:1> ] : <EOL> outfile . write ( "<STR_LIT>" % ( cp , styleval , spl [ - <NUM_LIT:1> ] ) ) <EOL> else : <EOL> outfile . write ( value ) <EOL> outfile . write ( u'<STR_LIT>' ) <EOL> if self . full : <EOL> realoutfile . write ( DOC_TEMPLATE % <EOL> dict ( docclass = self . docclass , <EOL> preamble = self . preamble , <EOL> title = self . title , <EOL> encoding = self . encoding or '<STR_LIT>' , <EOL> styledefs = self . get_style_defs ( ) , <EOL> code = outfile . getvalue ( ) ) ) </s>
