<s> from docutils . core import publish_parts <EOL> from docutils import nodes <EOL> from docutils . parsers . rst import directives <EOL> import re <EOL> import threading <EOL> g_data = threading . local ( ) <EOL> g_data . g_style = { } <EOL> class highlight_block ( nodes . General , nodes . Text ) : pass <EOL> from docutils . writers . html4css1 import Writer , HTMLTranslator <EOL> class SimpleWrite ( Writer ) : <EOL> def __init__ ( self ) : <EOL> Writer . __init__ ( self ) <EOL> self . translator_class = SimpleHTMLTranslator <EOL> class SimpleHTMLTranslator ( HTMLTranslator ) : <EOL> def visit_highlight_block ( self , node ) : <EOL> self . body . append ( node . astext ( ) ) <EOL> def depart_highlight_block ( self , node ) : <EOL> pass <EOL> def r_space ( m ) : <EOL> return len ( m . group ( ) ) * '<STR_LIT>' <EOL> re_space = re . compile ( r'<STR_LIT>' , re . MULTILINE ) <EOL> def code ( name , arguments , options , content , lineno , <EOL> content_offset , block_text , state , state_machine ) : <EOL> global g_data <EOL> if len ( arguments ) > <NUM_LIT:0> : <EOL> lang = arguments [ <NUM_LIT:0> ] <EOL> else : <EOL> lang = '<STR_LIT>' <EOL> style , text = highlight ( '<STR_LIT:\n>' . join ( content ) , lang ) <EOL> text = re_space . sub ( r_space , text ) <EOL> g_data . g_style [ lang ] = style <EOL> return [ highlight_block ( text ) ] <EOL> code . content = <NUM_LIT:1> <EOL> code . arguments = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> directives . register_directive ( '<STR_LIT:code>' , code ) <EOL> def to_html ( text , level = <NUM_LIT:1> , part = '<STR_LIT>' ) : <EOL> global g_data <EOL> g_data . g_style = { } <EOL> source = text <EOL> parts = publish_parts ( source , writer = SimpleWrite ( ) , settings_overrides = { '<STR_LIT>' : level } ) <EOL> if g_data . g_style : <EOL> style = '<STR_LIT>' + '<STR_LIT:\n>' . join ( g_data . g_style . values ( ) ) + '<STR_LIT>' <EOL> else : <EOL> style = '<STR_LIT>' <EOL> return style + '<STR_LIT:\n>' + parts [ part ] <EOL> def parts ( text , level = <NUM_LIT:1> , part = '<STR_LIT>' ) : <EOL> global g_data <EOL> g_data . g_style = { } <EOL> source = text <EOL> parts = publish_parts ( source , writer = SimpleWrite ( ) , settings_overrides = { '<STR_LIT>' : level } ) <EOL> if g_data . g_style : <EOL> style = '<STR_LIT>' + '<STR_LIT:\n>' . join ( g_data . g_style . values ( ) ) + '<STR_LIT>' <EOL> else : <EOL> style = '<STR_LIT>' <EOL> return style + '<STR_LIT:\n>' + parts [ part ] , parts <EOL> def highlight ( code , lang ) : <EOL> from pygments import highlight <EOL> from pygments . lexers import get_lexer_by_name , guess_lexer , PythonLexer <EOL> from pygments . formatters import HtmlFormatter <EOL> try : <EOL> lexer = get_lexer_by_name ( lang ) <EOL> except : <EOL> try : <EOL> lexer = guess_lexer ( code ) <EOL> lang = lexer . aliases [ <NUM_LIT:0> ] <EOL> except : <EOL> lexer = PythonLexer ( ) <EOL> lang = '<STR_LIT>' <EOL> lang = lang . replace ( '<STR_LIT:+>' , '<STR_LIT:_>' ) <EOL> return HtmlFormatter ( ) . get_style_defs ( '<STR_LIT>' + lang ) , highlight ( code , lexer , HtmlFormatter ( cssclass = '<STR_LIT>' + lang ) ) </s>
