<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from docutils import nodes <EOL> from docutils . parsers . rst import directives , Directive <EOL> from hashlib import sha1 <EOL> from flask import url_for , make_response , request , abort <EOL> try : <EOL> from pygments import highlight <EOL> from pygments . lexers import get_lexer_by_name , TextLexer <EOL> from pygments . formatters import HtmlFormatter <EOL> except ImportError : <EOL> import sys <EOL> sys . exit ( "<STR_LIT>" ) <EOL> formatter = HtmlFormatter ( ) <EOL> class Pygments ( Directive ) : <EOL> required_arguments = <NUM_LIT:1> <EOL> optional_arguments = <NUM_LIT:0> <EOL> final_argument_whitespace = True <EOL> has_content = True <EOL> def run ( self ) : <EOL> self . assert_has_content ( ) <EOL> try : <EOL> lexer = get_lexer_by_name ( self . arguments [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> lexer = TextLexer ( ) <EOL> parsed = highlight ( u'<STR_LIT:\n>' . join ( self . content ) , lexer , formatter ) <EOL> return [ nodes . raw ( '<STR_LIT>' , parsed , format = '<STR_LIT:html>' ) ] <EOL> def setup ( app , cfg ) : <EOL> global formatter <EOL> formatter = HtmlFormatter ( style = cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> directives . register_directive ( '<STR_LIT>' , Pygments ) <EOL> directives . register_directive ( '<STR_LIT>' , Pygments ) <EOL> @ app . route ( cfg . get ( '<STR_LIT>' , "<STR_LIT>" ) ) <EOL> def pygments_css ( ) : <EOL> etag = sha1 ( str ( formatter . style ) ) . hexdigest ( ) <EOL> if request . headers . get ( '<STR_LIT>' ) == etag : <EOL> return "<STR_LIT>" , <NUM_LIT> <EOL> else : <EOL> res = make_response ( formatter . get_style_defs ( ) ) <EOL> res . mimetype = "<STR_LIT>" <EOL> res . headers [ '<STR_LIT>' ] = etag <EOL> return res <EOL> @ app . before_request <EOL> def inject_pygments_css ( ) : <EOL> if url_for ( '<STR_LIT>' ) not in app . config [ '<STR_LIT>' ] : <EOL> app . config [ '<STR_LIT>' ] . append ( url_for ( '<STR_LIT>' ) ) </s>
