#NEWLINE# def _parse( reader , template , in_block = None , in_loop = None , begin_tag = BEGIN_TAG , end_tag = END_TAG , debug = False , see = None ) : #NEWLINE# #INDENT# body = _ChunkList ( [ ] ) #NEWLINE# _len_b = len ( begin_tag ) #NEWLINE# _len_e = len ( end_tag ) #NEWLINE# comment_end = ( '##%s' % end_tag ) #NEWLINE# _len_comment = len ( comment_end ) #NEWLINE# filename = template . filename #NEWLINE# if ( reader . find ( '#uliweb-template-tag:' ) > ( - 1 ) ) : #NEWLINE# #INDENT# pos = reader . find ( '\n' , 21 ) #NEWLINE# tag_head = reader . consume ( 26 ) [ 21 : ] #NEWLINE# ( begin_tag , end_tag ) = [ x . strip ( ) for x in tag_head . split ( ',' ) ] [ : 2 ] #NEWLINE# _len_b = len ( begin_tag ) #NEWLINE# _len_e = len ( end_tag ) #NEWLINE# comment_end = ( '##%s' % end_tag ) #NEWLINE# _len_comment = len ( comment_end ) #NEWLINE# #UNINDENT# while True : #NEWLINE# #INDENT# curly = 0 #NEWLINE# while True : #NEWLINE# #INDENT# curly = reader . find ( begin_tag , curly ) #NEWLINE# if ( ( curly == ( - 1 ) ) or ( ( curly + _len_b ) == reader . remaining ( ) ) ) : #NEWLINE# #INDENT# if in_block : #NEWLINE# #INDENT# raise ParseError ( ( 'Missing %s end %s block for %s on line %s:%d' % ( begin_tag , end_tag , in_block , filename , reader . line ) ) ) #NEWLINE# #UNINDENT# body . chunks . append ( _Text ( reader . consume ( ) , reader . line ) ) #NEWLINE# return body #NEWLINE# #UNINDENT# break #NEWLINE# #UNINDENT# if ( curly > 0 ) : #NEWLINE# #INDENT# cons = reader . consume ( curly ) #NEWLINE# body . chunks . append ( _Text ( cons , reader . line ) ) #NEWLINE# #UNINDENT# start_brace = reader . consume ( _len_b ) #NEWLINE# line = reader . line #NEWLINE# if ( reader [ : 2 ] == '##' ) : #NEWLINE# #INDENT# end = reader . find ( comment_end ) #NEWLINE# if ( end == ( - 1 ) ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Missing end expression #} on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# contents = reader . consume ( end ) . strip ( ) #NEWLINE# reader . consume ( _len_comment ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( reader [ 0 ] == '#' ) : #NEWLINE# #INDENT# end = reader . find ( end_tag ) #NEWLINE# if ( end == ( - 1 ) ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Missing end expression #} on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# contents = reader . consume ( end ) . strip ( ) #NEWLINE# reader . consume ( _len_b ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( reader [ 0 ] == '=' ) : #NEWLINE# #INDENT# reader . consume ( 1 ) #NEWLINE# end = reader . find ( end_tag ) #NEWLINE# if ( end == ( - 1 ) ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Missing end expression %s on line %s:%d' % ( end_tag , filename , line ) ) ) #NEWLINE# #UNINDENT# contents = reader . consume ( end ) . strip ( ) #NEWLINE# reader . consume ( _len_e ) #NEWLINE# if ( not contents ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Empty expression on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# body . chunks . append ( _Expression ( ( 'escape(%s)' % contents ) , line ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( reader [ : 2 ] == '<<' ) : #NEWLINE# #INDENT# reader . consume ( 2 ) #NEWLINE# end = reader . find ( end_tag ) #NEWLINE# if ( end == ( - 1 ) ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Missing end expression %s on line %s:%d' % ( end_tag , filename , line ) ) ) #NEWLINE# #UNINDENT# contents = reader . consume ( end ) . strip ( ) #NEWLINE# reader . consume ( _len_e ) #NEWLINE# if ( not contents ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Empty expression on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# body . chunks . append ( _Expression ( contents , line ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# assert ( start_brace == begin_tag ) , start_brace #NEWLINE# end = reader . find ( end_tag ) #NEWLINE# if ( end == ( - 1 ) ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Missing end block %s on line %s:%d' % ( end_tag , filename , line ) ) ) #NEWLINE# #UNINDENT# contents = reader . consume ( end ) . strip ( ) #NEWLINE# reader . consume ( _len_e ) #NEWLINE# if ( not contents ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Empty block tag (%s %s) on line %s:%d' % ( begin_tag , end_tag , filename , line ) ) ) #NEWLINE# #UNINDENT# ( operator , space , suffix ) = contents . partition ( ' ' ) #NEWLINE# suffix = suffix . strip ( ) #NEWLINE# if ( operator == 'super' ) : #NEWLINE# #INDENT# warnings . simplefilter ( 'default' ) #NEWLINE# warnings . warn ( ( 'Super is not supported on line %s:%d.' % ( filename , line ) ) , DeprecationWarning ) #NEWLINE# continue #NEWLINE# #UNINDENT# x_operator = operator . rstrip ( ':' ) #NEWLINE# if ( template . multilines and ( x_operator in [ 'else' , 'elif' , 'except' , 'finally' , 'pass' , 'try' , 'if' , 'for' , 'while' , 'break' , 'continue' , 'def' ] ) ) : #NEWLINE# #INDENT# for ( i , x ) in enumerate ( contents . splitlines ( ) ) : #NEWLINE# #INDENT# txt = r_out_write . sub ( '_tt_write' , x ) #NEWLINE# txt = r_out_xml . sub ( 'out_write' , txt ) #NEWLINE# body . chunks . append ( _Statement ( txt , ( line + i ) ) ) #NEWLINE# #UNINDENT# continue #NEWLINE# #UNINDENT# intermediate_blocks = { #NEWLINE# #INDENT# 'else' : set ( [ 'if' , 'for' , 'while' , 'try' ] ) , #NEWLINE# 'elif' : set ( [ 'if' ] ) , #NEWLINE# 'except' : set ( [ 'try' ] ) , #NEWLINE# 'finally' : set ( [ 'try' ] ) , #NEWLINE# #UNINDENT# } #NEWLINE# allowed_parents = intermediate_blocks . get ( x_operator ) #NEWLINE# if ( allowed_parents is not None ) : #NEWLINE# #INDENT# if ( not in_block ) : #NEWLINE# #INDENT# raise ParseError ( ( '%s outside %s block on line %s:%d' % ( operator , allowed_parents , filename , line ) ) ) #NEWLINE# #UNINDENT# if ( in_block not in allowed_parents ) : #NEWLINE# #INDENT# raise ParseError ( ( '%s block cannot be attached to %s block on line %s:%d' % ( operator , in_block , filename , line ) ) ) #NEWLINE# #UNINDENT# body . chunks . append ( _IntermediateControlBlock ( contents , line ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator in ( 'end' , 'pass' ) ) : #NEWLINE# #INDENT# if ( not in_block ) : #NEWLINE# #INDENT# raise ParseError ( ( 'Extra %s end %s block on line %s:%d' % ( begin_tag , end_tag , filename , line ) ) ) #NEWLINE# #UNINDENT# return body #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator in ( 'extend' , 'extends' , 'include' , 'embed' , 'BEGIN_TAG' , 'END_TAG' , 'use' , 'link' , 'head' , 'head_link' ) ) : #NEWLINE# #INDENT# if ( operator in ( 'extend' , 'extends' ) ) : #NEWLINE# #INDENT# if template . skip_extern : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# suffix = suffix . strip ( '"' ) . strip ( "'" ) #NEWLINE# if ( not suffix ) : #NEWLINE# #INDENT# raise ParseError ( ( 'extends missing file path on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# block = _ExtendsBlock ( suffix ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'include' ) : #NEWLINE# #INDENT# if template . skip_extern : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# suffix = suffix . strip ( '"' ) . strip ( "'" ) #NEWLINE# if ( not suffix ) : #NEWLINE# #INDENT# raise ParseError ( ( 'include missing file path on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# block = _IncludeBlock ( suffix , reader , line , template ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'use' ) : #NEWLINE# #INDENT# block = _Use ( suffix , line , template ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'link' ) : #NEWLINE# #INDENT# block = _Link ( suffix , line , template ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'head' ) : #NEWLINE# #INDENT# block = _Head ( suffix , line , template ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'head_link' ) : #NEWLINE# #INDENT# block = _HeadLink ( suffix , line , template ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'embed' ) : #NEWLINE# #INDENT# block = _Expression ( suffix , line ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'BEGIN_TAG' ) : #NEWLINE# #INDENT# block = _Text ( begin_tag , line ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'END_TAG' ) : #NEWLINE# #INDENT# block = _Text ( end_tag , line ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# body . chunks . append ( block ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator in ( 'apply' , 'block' , 'try' , 'if' , 'for' , 'while' ) ) : #NEWLINE# #INDENT# if ( operator in ( 'for' , 'while' ) ) : #NEWLINE# #INDENT# block_body = _parse ( reader , template , operator , operator , begin_tag , end_tag , debug = debug , see = see ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'apply' ) : #NEWLINE# #INDENT# block_body = _parse ( reader , template , operator , None , begin_tag , end_tag , debug = debug , see = see ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# block_body = _parse ( reader , template , operator , in_loop , begin_tag , end_tag , debug = debug , see = see ) #NEWLINE# #UNINDENT# #UNINDENT# if ( operator == 'apply' ) : #NEWLINE# #INDENT# if ( not suffix ) : #NEWLINE# #INDENT# raise ParseError ( ( 'apply missing method name on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# block = _ApplyBlock ( suffix , line , block_body ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'block' ) : #NEWLINE# #INDENT# if ( not suffix ) : #NEWLINE# #INDENT# raise ParseError ( ( 'block missing name on line %s:%d' % ( filename , line ) ) ) #NEWLINE# #UNINDENT# block = _NamedBlock ( suffix , block_body , template , line ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# block = _ControlBlock ( contents , line , block_body ) #NEWLINE# #UNINDENT# #UNINDENT# body . chunks . append ( block ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'def' ) : #NEWLINE# #INDENT# block_body = _parse ( reader , template , operator , None , begin_tag , end_tag , debug = debug , see = see ) #NEWLINE# block = _ControlBlock ( contents , line , block_body ) #NEWLINE# body . chunks . append ( block ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator in ( 'break' , 'continue' ) ) : #NEWLINE# #INDENT# if ( not in_loop ) : #NEWLINE# #INDENT# raise ParseError ( ( '%s outside %s block on line %s:%d' % ( operator , set ( [ 'for' , 'while' ] ) , filename , line ) ) ) #NEWLINE# #UNINDENT# body . chunks . append ( _Statement ( contents , line ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator in __custom_nodes__ ) : #NEWLINE# #INDENT# if template . skip_extern : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# NodeCls = __custom_nodes__ [ operator ] #NEWLINE# if issubclass ( NodeCls , BaseNode ) : #NEWLINE# #INDENT# body . chunks . append ( NodeCls ( suffix , line ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if issubclass ( NodeCls , BaseBlockNode ) : #NEWLINE# #INDENT# block_body = _parse ( reader , template , operator , in_block , begin_tag , end_tag , debug = debug , see = see ) #NEWLINE# block = NodeCls ( suffix , line , block_body ) #NEWLINE# body . chunks . append ( block ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ParseError ( ( 'Not support this custom node type %s on line %s:%d' % ( NodeCls . __name__ , filename , line ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for ( i , x ) in enumerate ( contents . splitlines ( ) ) : #NEWLINE# #INDENT# txt = r_out_write . sub ( '_tt_write' , x ) #NEWLINE# txt = r_out_xml . sub ( 'out_write' , txt ) #NEWLINE# body . chunks . append ( _Statement ( txt , ( reader + i ) ) )