<s> """<STR_LIT>""" <EOL> from django import template <EOL> from django . template import Template , Context , Node , Variable , TemplateSyntaxError <EOL> from django . template . defaultfilters import stringfilter <EOL> from django . utils . safestring import mark_safe <EOL> try : <EOL> from pygments import highlight as pyghighlight <EOL> from pygments . lexers import get_lexer_by_name <EOL> from pygments . formatters import HtmlFormatter <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> register = template . Library ( ) <EOL> @ register . filter <EOL> @ stringfilter <EOL> def parse_template ( value ) : <EOL> return mark_safe ( Template ( value ) . render ( Context ( ) ) ) <EOL> parse_template . is_safe = True <EOL> class CodeNode ( Node ) : <EOL> def __init__ ( self , language , nodelist , name = '<STR_LIT>' ) : <EOL> self . language = Variable ( language ) <EOL> self . nodelist = nodelist <EOL> if name : <EOL> self . name = Variable ( name ) <EOL> else : <EOL> self . name = None <EOL> def render ( self , context ) : <EOL> code = self . nodelist . render ( context ) . strip ( ) <EOL> lexer = get_lexer_by_name ( self . language . resolve ( context ) ) <EOL> formatter = HtmlFormatter ( linenos = False ) <EOL> html = "<STR_LIT>" <EOL> if self . name : <EOL> name = self . name . resolve ( context ) <EOL> html = '<STR_LIT>' % ( name ) <EOL> return html + pyghighlight ( code , lexer , formatter ) <EOL> @ register . tag <EOL> def highlight ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> bits = token . split_contents ( ) [ <NUM_LIT:1> : ] <EOL> if len ( bits ) < <NUM_LIT:1> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" ) <EOL> return CodeNode ( bits [ <NUM_LIT:0> ] , nodelist , * bits [ <NUM_LIT:1> : ] ) </s>
