<s> from django . template import Lexer , Parser , tag_re , NodeList , VariableNode , TemplateSyntaxError <EOL> from django . utils . encoding import force_unicode <EOL> from django . utils . html import escape <EOL> from django . utils . safestring import SafeData , EscapeData <EOL> class DebugLexer ( Lexer ) : <EOL> def __init__ ( self , template_string , origin ) : <EOL> super ( DebugLexer , self ) . __init__ ( template_string , origin ) <EOL> def tokenize ( self ) : <EOL> "<STR_LIT>" <EOL> result , upto = [ ] , <NUM_LIT:0> <EOL> for match in tag_re . finditer ( self . template_string ) : <EOL> start , end = match . span ( ) <EOL> if start > upto : <EOL> result . append ( self . create_token ( self . template_string [ upto : start ] , ( upto , start ) , False ) ) <EOL> upto = start <EOL> result . append ( self . create_token ( self . template_string [ start : end ] , ( start , end ) , True ) ) <EOL> upto = end <EOL> last_bit = self . template_string [ upto : ] <EOL> if last_bit : <EOL> result . append ( self . create_token ( last_bit , ( upto , upto + len ( last_bit ) ) , False ) ) <EOL> return result <EOL> def create_token ( self , token_string , source , in_tag ) : <EOL> token = super ( DebugLexer , self ) . create_token ( token_string , in_tag ) <EOL> token . source = self . origin , source <EOL> return token <EOL> class DebugParser ( Parser ) : <EOL> def __init__ ( self , lexer ) : <EOL> super ( DebugParser , self ) . __init__ ( lexer ) <EOL> self . command_stack = [ ] <EOL> def enter_command ( self , command , token ) : <EOL> self . command_stack . append ( ( command , token . source ) ) <EOL> def exit_command ( self ) : <EOL> self . command_stack . pop ( ) <EOL> def error ( self , token , msg ) : <EOL> return self . source_error ( token . source , msg ) <EOL> def source_error ( self , source , msg ) : <EOL> e = TemplateSyntaxError ( msg ) <EOL> e . source = source <EOL> return e <EOL> def create_nodelist ( self ) : <EOL> return DebugNodeList ( ) <EOL> def create_variable_node ( self , contents ) : <EOL> return DebugVariableNode ( contents ) <EOL> def extend_nodelist ( self , nodelist , node , token ) : <EOL> node . source = token . source <EOL> super ( DebugParser , self ) . extend_nodelist ( nodelist , node , token ) <EOL> def unclosed_block_tag ( self , parse_until ) : <EOL> command , source = self . command_stack . pop ( ) <EOL> msg = "<STR_LIT>" % ( command , '<STR_LIT:U+002CU+0020>' . join ( parse_until ) ) <EOL> raise self . source_error ( source , msg ) <EOL> def compile_function_error ( self , token , e ) : <EOL> if not hasattr ( e , '<STR_LIT:source>' ) : <EOL> e . source = token . source <EOL> class DebugNodeList ( NodeList ) : <EOL> def render_node ( self , node , context ) : <EOL> try : <EOL> result = node . render ( context ) <EOL> except TemplateSyntaxError , e : <EOL> if not hasattr ( e , '<STR_LIT:source>' ) : <EOL> e . source = node . source <EOL> raise <EOL> except Exception , e : <EOL> from sys import exc_info <EOL> wrapped = TemplateSyntaxError ( u'<STR_LIT>' % force_unicode ( e , errors = '<STR_LIT:replace>' ) ) <EOL> wrapped . source = node . source <EOL> wrapped . exc_info = exc_info ( ) <EOL> raise wrapped <EOL> return result <EOL> class DebugVariableNode ( VariableNode ) : <EOL> def render ( self , context ) : <EOL> try : <EOL> output = force_unicode ( self . filter_expression . resolve ( context ) ) <EOL> except TemplateSyntaxError , e : <EOL> if not hasattr ( e , '<STR_LIT:source>' ) : <EOL> e . source = self . source <EOL> raise <EOL> except UnicodeDecodeError : <EOL> return '<STR_LIT>' <EOL> if ( context . autoescape and not isinstance ( output , SafeData ) ) or isinstance ( output , EscapeData ) : <EOL> return escape ( output ) <EOL> else : <EOL> return output </s>
