#NEWLINE# def _register_pygments_rst_directive( ) : #NEWLINE# #INDENT# from docutils import nodes #NEWLINE# from docutils.parsers.rst import directives #NEWLINE# from pygments import highlight #NEWLINE# from pygments.lexers import get_lexer_by_name , TextLexer #NEWLINE# from pygments.formatters import HtmlFormatter #NEWLINE# DEFAULT = HtmlFormatter ( ) #NEWLINE# VARIANTS = { #NEWLINE# #INDENT# 'linenos' : HtmlFormatter ( linenos = True ) , #NEWLINE# #UNINDENT# } #NEWLINE# def pygments_directive( name , arguments , options , content , lineno , content_offset , block_text , state , state_machine ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# lexer = get_lexer_by_name ( arguments [ 0 ] ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# lexer = TextLexer ( ) #NEWLINE# #UNINDENT# formatter = ( ( options and VARIANTS [ options . keys ( ) [ 0 ] ] ) or DEFAULT ) #NEWLINE# parsed = highlight ( '\n' . join ( content ) , lexer , formatter ) #NEWLINE# return [ nodes . raw ( '' , parsed , format = 'html' ) ] #NEWLINE# #UNINDENT# pygments_directive . arguments = ( 1 , 0 , 1 ) #NEWLINE# pygments_directive . content = 1 #NEWLINE# directives . register_directive ( 'code' , pygments_directive )