<s> """<STR_LIT>""" <EOL> import markdown <EOL> class CodeHilite : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , src = None , linenos = False , css_class = "<STR_LIT>" , <EOL> lang = None , style = '<STR_LIT:default>' , noclasses = False , tab_length = <NUM_LIT:4> ) : <EOL> self . src = src <EOL> self . lang = lang <EOL> self . linenos = linenos <EOL> self . css_class = css_class <EOL> self . style = style <EOL> self . noclasses = noclasses <EOL> self . tab_length = tab_length <EOL> def hilite ( self ) : <EOL> """<STR_LIT>""" <EOL> self . src = self . src . strip ( '<STR_LIT:\n>' ) <EOL> if self . lang == None : <EOL> self . _getLang ( ) <EOL> try : <EOL> from pygments import highlight <EOL> from pygments . lexers import get_lexer_by_name , guess_lexer , TextLexer <EOL> from pygments . formatters import HtmlFormatter <EOL> except ImportError : <EOL> txt = self . _escape ( self . src ) <EOL> if self . linenos : <EOL> txt = self . _number ( txt ) <EOL> else : <EOL> txt = '<STR_LIT>' % ( self . css_class , txt ) <EOL> return txt <EOL> else : <EOL> try : <EOL> lexer = get_lexer_by_name ( self . lang ) <EOL> except ValueError : <EOL> try : <EOL> lexer = guess_lexer ( self . src ) <EOL> except ValueError : <EOL> lexer = TextLexer ( ) <EOL> formatter = HtmlFormatter ( linenos = self . linenos , <EOL> cssclass = self . css_class , <EOL> style = self . style , <EOL> noclasses = self . noclasses ) <EOL> return highlight ( self . src , lexer , formatter ) <EOL> def _escape ( self , txt ) : <EOL> """<STR_LIT>""" <EOL> txt = txt . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) <EOL> txt = txt . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) <EOL> txt = txt . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) <EOL> txt = txt . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> return txt <EOL> def _number ( self , txt ) : <EOL> """<STR_LIT>""" <EOL> txt = txt . replace ( '<STR_LIT:\t>' , '<STR_LIT:U+0020>' * self . tab_length ) <EOL> txt = txt . replace ( "<STR_LIT:U+0020>" * <NUM_LIT:4> , "<STR_LIT>" ) <EOL> txt = txt . replace ( "<STR_LIT:U+0020>" * <NUM_LIT:3> , "<STR_LIT>" ) <EOL> txt = txt . replace ( "<STR_LIT:U+0020>" * <NUM_LIT:2> , "<STR_LIT>" ) <EOL> lines = txt . splitlines ( ) <EOL> txt = '<STR_LIT>' <EOL> for line in lines : <EOL> txt += '<STR_LIT>' % line <EOL> txt += '<STR_LIT>' <EOL> return txt <EOL> def _getLang ( self ) : <EOL> """<STR_LIT>""" <EOL> import re <EOL> lines = self . src . split ( "<STR_LIT:\n>" ) <EOL> fl = lines . pop ( <NUM_LIT:0> ) <EOL> c = re . compile ( r'''<STR_LIT>''' , re . VERBOSE ) <EOL> m = c . search ( fl ) <EOL> if m : <EOL> try : <EOL> self . lang = m . group ( '<STR_LIT>' ) . lower ( ) <EOL> except IndexError : <EOL> self . lang = None <EOL> if m . group ( '<STR_LIT:path>' ) : <EOL> lines . insert ( <NUM_LIT:0> , fl ) <EOL> if m . group ( '<STR_LIT>' ) : <EOL> self . linenos = True <EOL> else : <EOL> lines . insert ( <NUM_LIT:0> , fl ) <EOL> self . src = "<STR_LIT:\n>" . join ( lines ) . strip ( "<STR_LIT:\n>" ) <EOL> class HiliteTreeprocessor ( markdown . treeprocessors . Treeprocessor ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , root ) : <EOL> """<STR_LIT>""" <EOL> blocks = root . getiterator ( '<STR_LIT>' ) <EOL> for block in blocks : <EOL> children = block . getchildren ( ) <EOL> if len ( children ) == <NUM_LIT:1> and children [ <NUM_LIT:0> ] . tag == '<STR_LIT:code>' : <EOL> code = CodeHilite ( children [ <NUM_LIT:0> ] . text , <EOL> linenos = self . config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> css_class = self . config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> style = self . config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> noclasses = self . config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> tab_length = self . markdown . tab_length ) <EOL> placeholder = self . markdown . htmlStash . store ( code . hilite ( ) , <EOL> safe = True ) <EOL> block . clear ( ) <EOL> block . tag = '<STR_LIT:p>' <EOL> block . text = placeholder <EOL> class CodeHiliteExtension ( markdown . Extension ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , configs ) : <EOL> self . config = { <EOL> '<STR_LIT>' : [ False , "<STR_LIT>" ] , <EOL> '<STR_LIT>' : [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:default>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ False , '<STR_LIT>' ] <EOL> } <EOL> for key , value in configs : <EOL> self . setConfig ( key , value ) <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> """<STR_LIT>""" <EOL> hiliter = HiliteTreeprocessor ( md ) <EOL> hiliter . config = self . config <EOL> md . treeprocessors . add ( "<STR_LIT>" , hiliter , "<STR_LIT>" ) <EOL> md . registerExtension ( self ) <EOL> def makeExtension ( configs = { } ) : <EOL> return CodeHiliteExtension ( configs = configs ) </s>
