<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> from . import Extension <EOL> from . . treeprocessors import Treeprocessor <EOL> import warnings <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> pygments = True <EOL> except ImportError : <EOL> pygments = False <EOL> class CodeHilite ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , src = None , linenums = None , guess_lang = True , <EOL> css_class = "<STR_LIT>" , lang = None , style = '<STR_LIT:default>' , <EOL> noclasses = False , tab_length = <NUM_LIT:4> ) : <EOL> self . src = src <EOL> self . lang = lang <EOL> self . linenums = linenums <EOL> self . guess_lang = guess_lang <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 is None : <EOL> self . _getLang ( ) <EOL> if pygments : <EOL> try : <EOL> lexer = get_lexer_by_name ( self . lang ) <EOL> except ValueError : <EOL> try : <EOL> if self . guess_lang : <EOL> lexer = guess_lexer ( self . src ) <EOL> else : <EOL> lexer = TextLexer ( ) <EOL> except ValueError : <EOL> lexer = TextLexer ( ) <EOL> formatter = HtmlFormatter ( linenos = self . linenums , <EOL> cssclass = self . css_class , <EOL> style = self . style , <EOL> noclasses = self . noclasses ) <EOL> return highlight ( self . src , lexer , formatter ) <EOL> else : <EOL> txt = self . src . 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> classes = [ ] <EOL> if self . lang : <EOL> classes . append ( '<STR_LIT>' % self . lang ) <EOL> if self . linenums : <EOL> classes . append ( '<STR_LIT>' ) <EOL> class_str = '<STR_LIT>' <EOL> if classes : <EOL> class_str = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( classes ) <EOL> return '<STR_LIT>' % ( self . css_class , class_str , 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 self . linenums is None and m . group ( '<STR_LIT>' ) : <EOL> self . linenums = 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 ( 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> linenums = self . config [ '<STR_LIT>' ] , <EOL> guess_lang = self . config [ '<STR_LIT>' ] , <EOL> css_class = self . config [ '<STR_LIT>' ] , <EOL> style = self . config [ '<STR_LIT>' ] , <EOL> noclasses = self . config [ '<STR_LIT>' ] , <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 ( Extension ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , configs ) : <EOL> self . config = { <EOL> '<STR_LIT>' : [ None , "<STR_LIT>" ] , <EOL> '<STR_LIT>' : [ False , "<STR_LIT>" ] , <EOL> '<STR_LIT>' : [ True , "<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> if value == '<STR_LIT:True>' : value = True <EOL> if value == '<STR_LIT:False>' : value = False <EOL> if value == '<STR_LIT:None>' : value = None <EOL> if key == '<STR_LIT>' : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , PendingDeprecationWarning ) <EOL> if value : <EOL> self . setConfig ( '<STR_LIT>' , True ) <EOL> self . setConfig ( key , value ) <EOL> def extendMarkdown ( self , md , md_globals ) : <EOL> """<STR_LIT>""" <EOL> hiliter = HiliteTreeprocessor ( md ) <EOL> hiliter . config = self . getConfigs ( ) <EOL> md . treeprocessors . add ( "<STR_LIT>" , hiliter , "<STR_LIT>" ) <EOL> md . registerExtension ( self ) <EOL> def makeExtension ( configs = { } ) : <EOL> return CodeHiliteExtension ( configs = configs ) </s>
