<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import StringIO <EOL> from pygments . formatter import Formatter <EOL> from pygments . token import Token , Text , STANDARD_TYPES <EOL> from pygments . util import get_bool_opt , get_int_opt , get_list_opt , bytes <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def escape_html ( text ) : <EOL> """<STR_LIT>""" <EOL> return text . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) . replace ( "<STR_LIT:'>" , '<STR_LIT>' ) <EOL> def get_random_id ( ) : <EOL> """<STR_LIT>""" <EOL> from random import random <EOL> from time import time <EOL> try : <EOL> from hashlib import sha1 as sha <EOL> except ImportError : <EOL> import sha <EOL> sha = sha . new <EOL> return sha ( '<STR_LIT>' % ( random ( ) , time ( ) ) ) . hexdigest ( ) <EOL> def _get_ttype_class ( ttype ) : <EOL> fname = STANDARD_TYPES . get ( ttype ) <EOL> if fname : <EOL> return fname <EOL> aname = '<STR_LIT>' <EOL> while fname is None : <EOL> aname = '<STR_LIT:->' + ttype [ - <NUM_LIT:1> ] + aname <EOL> ttype = ttype . parent <EOL> fname = STANDARD_TYPES . get ( ttype ) <EOL> return fname + aname <EOL> CSSFILE_TEMPLATE = '''<STR_LIT>''' <EOL> DOC_HEADER = '''<STR_LIT>''' + CSSFILE_TEMPLATE + '''<STR_LIT>''' <EOL> DOC_HEADER_EXTERNALCSS = '''<STR_LIT>''' <EOL> DOC_FOOTER = '''<STR_LIT>''' <EOL> class HtmlFormatter ( Formatter ) : <EOL> r"""<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> aliases = [ '<STR_LIT:html>' ] <EOL> filenames = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , ** options ) : <EOL> Formatter . __init__ ( self , ** options ) <EOL> self . title = self . _decodeifneeded ( self . title ) <EOL> self . nowrap = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> self . noclasses = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> self . classprefix = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . cssclass = self . _decodeifneeded ( options . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . cssstyles = self . _decodeifneeded ( options . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . prestyles = self . _decodeifneeded ( options . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . cssfile = self . _decodeifneeded ( options . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . noclobber_cssfile = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> linenos = options . get ( '<STR_LIT>' , False ) <EOL> if linenos == '<STR_LIT>' : <EOL> self . linenos = <NUM_LIT:2> <EOL> elif linenos : <EOL> self . linenos = <NUM_LIT:1> <EOL> else : <EOL> self . linenos = <NUM_LIT:0> <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 . linenospecial = abs ( get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> self . nobackground = get_bool_opt ( options , '<STR_LIT>' , False ) <EOL> self . lineseparator = options . get ( '<STR_LIT>' , '<STR_LIT:\n>' ) <EOL> self . lineanchors = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . anchorlinenos = options . get ( '<STR_LIT>' , False ) <EOL> self . hl_lines = set ( ) <EOL> for lineno in get_list_opt ( options , '<STR_LIT>' , [ ] ) : <EOL> try : <EOL> self . hl_lines . add ( int ( lineno ) ) <EOL> except ValueError : <EOL> pass <EOL> self . _class_cache = { } <EOL> self . _create_stylesheet ( ) <EOL> def _get_css_class ( self , ttype ) : <EOL> """<STR_LIT>""" <EOL> if ttype in self . _class_cache : <EOL> return self . _class_cache [ ttype ] <EOL> return self . classprefix + _get_ttype_class ( ttype ) <EOL> def _create_stylesheet ( self ) : <EOL> t2c = self . ttype2class = { Token : '<STR_LIT>' } <EOL> c2s = self . class2style = { } <EOL> cp = self . classprefix <EOL> for ttype , ndef in self . style : <EOL> name = cp + _get_ttype_class ( ttype ) <EOL> style = '<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> style += '<STR_LIT>' % ndef [ '<STR_LIT>' ] <EOL> if ndef [ '<STR_LIT>' ] : <EOL> style += '<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> style += '<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> style += '<STR_LIT>' <EOL> if ndef [ '<STR_LIT>' ] : <EOL> style += '<STR_LIT>' % ndef [ '<STR_LIT>' ] <EOL> if ndef [ '<STR_LIT>' ] : <EOL> style += '<STR_LIT>' % ndef [ '<STR_LIT>' ] <EOL> if style : <EOL> t2c [ ttype ] = name <EOL> c2s [ name ] = ( style [ : - <NUM_LIT:2> ] , ttype , len ( ttype ) ) <EOL> def get_style_defs ( self , arg = None ) : <EOL> """<STR_LIT>""" <EOL> if arg is None : <EOL> arg = ( '<STR_LIT>' in self . options and '<STR_LIT:.>' + self . cssclass or '<STR_LIT>' ) <EOL> if isinstance ( arg , basestring ) : <EOL> args = [ arg ] <EOL> else : <EOL> args = list ( arg ) <EOL> def prefix ( cls ) : <EOL> if cls : <EOL> cls = '<STR_LIT:.>' + cls <EOL> tmp = [ ] <EOL> for arg in args : <EOL> tmp . append ( ( arg and arg + '<STR_LIT:U+0020>' or '<STR_LIT>' ) + cls ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( tmp ) <EOL> styles = [ ( level , ttype , cls , style ) <EOL> for cls , ( style , ttype , level ) in self . class2style . iteritems ( ) <EOL> if cls and style ] <EOL> styles . sort ( ) <EOL> lines = [ '<STR_LIT>' % ( prefix ( cls ) , style , repr ( ttype ) [ <NUM_LIT:6> : ] ) <EOL> for ( level , ttype , cls , style ) in styles ] <EOL> if arg and not self . nobackground and self . style . background_color is not None : <EOL> text_style = '<STR_LIT>' <EOL> if Text in self . ttype2class : <EOL> text_style = '<STR_LIT:U+0020>' + self . class2style [ self . ttype2class [ Text ] ] [ <NUM_LIT:0> ] <EOL> lines . insert ( <NUM_LIT:0> , '<STR_LIT>' % <EOL> ( prefix ( '<STR_LIT>' ) , self . style . background_color , text_style ) ) <EOL> if self . style . highlight_color is not None : <EOL> lines . insert ( <NUM_LIT:0> , '<STR_LIT>' % <EOL> ( prefix ( '<STR_LIT>' ) , self . style . highlight_color ) ) <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> def _decodeifneeded ( self , value ) : <EOL> if isinstance ( value , bytes ) : <EOL> if self . encoding : <EOL> return value . decode ( self . encoding ) <EOL> return value . decode ( ) <EOL> return value <EOL> def _wrap_full ( self , inner , outfile ) : <EOL> if self . cssfile : <EOL> if os . path . isabs ( self . cssfile ) : <EOL> cssfilename = self . cssfile <EOL> else : <EOL> try : <EOL> filename = outfile . name <EOL> if not filename or filename [ <NUM_LIT:0> ] == '<STR_LIT:<>' : <EOL> raise AttributeError <EOL> cssfilename = os . path . join ( os . path . dirname ( filename ) , <EOL> self . cssfile ) <EOL> except AttributeError : <EOL> print >> sys . stderr , '<STR_LIT>' '<STR_LIT>' <EOL> cssfilename = self . cssfile <EOL> try : <EOL> if not os . path . exists ( cssfilename ) or not self . noclobber_cssfile : <EOL> cf = open ( cssfilename , "<STR_LIT:w>" ) <EOL> cf . write ( CSSFILE_TEMPLATE % <EOL> { '<STR_LIT>' : self . get_style_defs ( '<STR_LIT:body>' ) } ) <EOL> cf . close ( ) <EOL> except IOError , err : <EOL> err . strerror = '<STR_LIT>' + err . strerror <EOL> raise <EOL> yield <NUM_LIT:0> , ( DOC_HEADER_EXTERNALCSS % <EOL> dict ( title = self . title , <EOL> cssfile = self . cssfile , <EOL> encoding = self . encoding ) ) <EOL> else : <EOL> yield <NUM_LIT:0> , ( DOC_HEADER % <EOL> dict ( title = self . title , <EOL> styledefs = self . get_style_defs ( '<STR_LIT:body>' ) , <EOL> encoding = self . encoding ) ) <EOL> for t , line in inner : <EOL> yield t , line <EOL> yield <NUM_LIT:0> , DOC_FOOTER <EOL> def _wrap_tablelinenos ( self , inner ) : <EOL> dummyoutfile = StringIO . StringIO ( ) <EOL> lncount = <NUM_LIT:0> <EOL> for t , line in inner : <EOL> if t : <EOL> lncount += <NUM_LIT:1> <EOL> dummyoutfile . write ( line ) <EOL> fl = self . linenostart <EOL> mw = len ( str ( lncount + fl - <NUM_LIT:1> ) ) <EOL> sp = self . linenospecial <EOL> st = self . linenostep <EOL> la = self . lineanchors <EOL> aln = self . anchorlinenos <EOL> if sp : <EOL> lines = [ ] <EOL> for i in range ( fl , fl + lncount ) : <EOL> if i % st == <NUM_LIT:0> : <EOL> if i % sp == <NUM_LIT:0> : <EOL> if aln : <EOL> lines . append ( '<STR_LIT>' % <EOL> ( la , i , mw , i ) ) <EOL> else : <EOL> lines . append ( '<STR_LIT>' % ( mw , i ) ) <EOL> else : <EOL> if aln : <EOL> lines . append ( '<STR_LIT>' % ( la , i , mw , i ) ) <EOL> else : <EOL> lines . append ( '<STR_LIT>' % ( mw , i ) ) <EOL> else : <EOL> lines . append ( '<STR_LIT>' ) <EOL> ls = '<STR_LIT:\n>' . join ( lines ) <EOL> else : <EOL> lines = [ ] <EOL> for i in range ( fl , fl + lncount ) : <EOL> if i % st == <NUM_LIT:0> : <EOL> if aln : <EOL> lines . append ( '<STR_LIT>' % ( la , i , mw , i ) ) <EOL> else : <EOL> lines . append ( '<STR_LIT>' % ( mw , i ) ) <EOL> else : <EOL> lines . append ( '<STR_LIT>' ) <EOL> ls = '<STR_LIT:\n>' . join ( lines ) <EOL> yield <NUM_LIT:0> , ( '<STR_LIT>' % self . cssclass + <EOL> '<STR_LIT>' + <EOL> ls + '<STR_LIT>' ) <EOL> yield <NUM_LIT:0> , dummyoutfile . getvalue ( ) <EOL> yield <NUM_LIT:0> , '<STR_LIT>' <EOL> def _wrap_inlinelinenos ( self , inner ) : <EOL> lines = list ( inner ) <EOL> sp = self . linenospecial <EOL> st = self . linenostep <EOL> num = self . linenostart <EOL> mw = len ( str ( len ( lines ) + num - <NUM_LIT:1> ) ) <EOL> if sp : <EOL> for t , line in lines : <EOL> yield <NUM_LIT:1> , '<STR_LIT>' % ( <EOL> num % sp == <NUM_LIT:0> and '<STR_LIT>' or '<STR_LIT>' , mw , <EOL> ( num % st and '<STR_LIT:U+0020>' or num ) ) + line <EOL> num += <NUM_LIT:1> <EOL> else : <EOL> for t , line in lines : <EOL> yield <NUM_LIT:1> , '<STR_LIT>' % ( <EOL> mw , ( num % st and '<STR_LIT:U+0020>' or num ) ) + line <EOL> num += <NUM_LIT:1> <EOL> def _wrap_lineanchors ( self , inner ) : <EOL> s = self . lineanchors <EOL> i = <NUM_LIT:0> <EOL> for t , line in inner : <EOL> if t : <EOL> i += <NUM_LIT:1> <EOL> yield <NUM_LIT:1> , '<STR_LIT>' % ( s , i ) + line <EOL> else : <EOL> yield <NUM_LIT:0> , line <EOL> def _wrap_div ( self , inner ) : <EOL> style = [ ] <EOL> if ( self . noclasses and not self . nobackground and <EOL> self . style . background_color is not None ) : <EOL> style . append ( '<STR_LIT>' % ( self . style . background_color , ) ) <EOL> if self . cssstyles : <EOL> style . append ( self . cssstyles ) <EOL> style = '<STR_LIT>' . join ( style ) <EOL> yield <NUM_LIT:0> , ( '<STR_LIT>' + ( self . cssclass and '<STR_LIT>' % self . cssclass ) <EOL> + ( style and ( '<STR_LIT>' % style ) ) + '<STR_LIT:>>' ) <EOL> for tup in inner : <EOL> yield tup <EOL> yield <NUM_LIT:0> , '<STR_LIT>' <EOL> def _wrap_pre ( self , inner ) : <EOL> style = [ ] <EOL> if self . prestyles : <EOL> style . append ( self . prestyles ) <EOL> if self . noclasses : <EOL> style . append ( '<STR_LIT>' ) <EOL> style = '<STR_LIT>' . join ( style ) <EOL> yield <NUM_LIT:0> , ( '<STR_LIT>' + ( style and '<STR_LIT>' % style ) + '<STR_LIT:>>' ) <EOL> for tup in inner : <EOL> yield tup <EOL> yield <NUM_LIT:0> , '<STR_LIT>' <EOL> def _format_lines ( self , tokensource ) : <EOL> """<STR_LIT>""" <EOL> nocls = self . noclasses <EOL> lsep = self . lineseparator <EOL> getcls = self . ttype2class . get <EOL> c2s = self . class2style <EOL> lspan = '<STR_LIT>' <EOL> line = '<STR_LIT>' <EOL> for ttype , value in tokensource : <EOL> if nocls : <EOL> cclass = getcls ( ttype ) <EOL> while cclass is None : <EOL> ttype = ttype . parent <EOL> cclass = getcls ( ttype ) <EOL> cspan = cclass and '<STR_LIT>' % c2s [ cclass ] [ <NUM_LIT:0> ] or '<STR_LIT>' <EOL> else : <EOL> cls = self . _get_css_class ( ttype ) <EOL> cspan = cls and '<STR_LIT>' % cls or '<STR_LIT>' <EOL> parts = escape_html ( value ) . split ( '<STR_LIT:\n>' ) <EOL> for part in parts [ : - <NUM_LIT:1> ] : <EOL> if line : <EOL> if lspan != cspan : <EOL> line += ( lspan and '<STR_LIT>' ) + cspan + part + ( cspan and '<STR_LIT>' ) + lsep <EOL> else : <EOL> line += part + ( lspan and '<STR_LIT>' ) + lsep <EOL> yield <NUM_LIT:1> , line <EOL> line = '<STR_LIT>' <EOL> elif part : <EOL> yield <NUM_LIT:1> , cspan + part + ( cspan and '<STR_LIT>' ) + lsep <EOL> else : <EOL> yield <NUM_LIT:1> , lsep <EOL> if line and parts [ - <NUM_LIT:1> ] : <EOL> if lspan != cspan : <EOL> line += ( lspan and '<STR_LIT>' ) + cspan + parts [ - <NUM_LIT:1> ] <EOL> lspan = cspan <EOL> else : <EOL> line += parts [ - <NUM_LIT:1> ] <EOL> elif parts [ - <NUM_LIT:1> ] : <EOL> line = cspan + parts [ - <NUM_LIT:1> ] <EOL> lspan = cspan <EOL> if line : <EOL> yield <NUM_LIT:1> , line + ( lspan and '<STR_LIT>' ) + lsep <EOL> def _highlight_lines ( self , tokensource ) : <EOL> """<STR_LIT>""" <EOL> hls = self . hl_lines <EOL> for i , ( t , value ) in enumerate ( tokensource ) : <EOL> if t != <NUM_LIT:1> : <EOL> yield t , value <EOL> if i + <NUM_LIT:1> in hls : <EOL> if self . noclasses : <EOL> style = '<STR_LIT>' <EOL> if self . style . highlight_color is not None : <EOL> style = ( '<STR_LIT>' % <EOL> ( self . style . highlight_color , ) ) <EOL> yield <NUM_LIT:1> , '<STR_LIT>' % ( style , value ) <EOL> else : <EOL> yield <NUM_LIT:1> , '<STR_LIT>' % value <EOL> else : <EOL> yield <NUM_LIT:1> , value <EOL> def wrap ( self , source , outfile ) : <EOL> """<STR_LIT>""" <EOL> return self . _wrap_div ( self . _wrap_pre ( source ) ) <EOL> def format_unencoded ( self , tokensource , outfile ) : <EOL> """<STR_LIT>""" <EOL> source = self . _format_lines ( tokensource ) <EOL> if self . hl_lines : <EOL> source = self . _highlight_lines ( source ) <EOL> if not self . nowrap : <EOL> if self . linenos == <NUM_LIT:2> : <EOL> source = self . _wrap_inlinelinenos ( source ) <EOL> if self . lineanchors : <EOL> source = self . _wrap_lineanchors ( source ) <EOL> source = self . wrap ( source , outfile ) <EOL> if self . linenos == <NUM_LIT:1> : <EOL> source = self . _wrap_tablelinenos ( source ) <EOL> if self . full : <EOL> source = self . _wrap_full ( source , outfile ) <EOL> for t , piece in source : <EOL> outfile . write ( piece ) </s>
