<s> """<STR_LIT>""" <EOL> import re <EOL> from genshi . core import TEXT <EOL> from genshi . template . base import BadDirectiveError , Template , TemplateSyntaxError , EXEC , INCLUDE , SUB <EOL> from genshi . template . eval import Suite <EOL> from genshi . template . directives import * <EOL> from genshi . template . directives import Directive <EOL> from genshi . template . interpolation import interpolate <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> __docformat__ = '<STR_LIT>' <EOL> class NewTextTemplate ( Template ) : <EOL> r"""<STR_LIT>""" <EOL> directives = [ ( '<STR_LIT>' , DefDirective ) , <EOL> ( '<STR_LIT>' , WhenDirective ) , <EOL> ( '<STR_LIT>' , OtherwiseDirective ) , <EOL> ( '<STR_LIT>' , ForDirective ) , <EOL> ( '<STR_LIT>' , IfDirective ) , <EOL> ( '<STR_LIT>' , ChooseDirective ) , <EOL> ( '<STR_LIT>' , WithDirective ) ] <EOL> serializer = '<STR_LIT:text>' <EOL> _DIRECTIVE_RE = r'<STR_LIT>' <EOL> _ESCAPE_RE = r'<STR_LIT>' <EOL> def __init__ ( self , source , filepath = None , filename = None , loader = None , <EOL> encoding = None , lookup = '<STR_LIT:strict>' , allow_exec = False , <EOL> delims = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> self . delimiters = delims <EOL> Template . __init__ ( self , source , filepath = filepath , filename = filename , <EOL> loader = loader , encoding = encoding , lookup = lookup ) <EOL> def _get_delims ( self ) : <EOL> return self . _delims <EOL> def _set_delims ( self , delims ) : <EOL> if len ( delims ) != <NUM_LIT:4> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _delims = delims <EOL> self . _directive_re = re . compile ( self . _DIRECTIVE_RE % tuple ( <EOL> [ re . escape ( d ) for d in delims ] <EOL> ) , re . DOTALL ) <EOL> self . _escape_re = re . compile ( self . _ESCAPE_RE % tuple ( <EOL> [ re . escape ( d ) for d in delims [ : : <NUM_LIT:2> ] ] <EOL> ) ) <EOL> delimiters = property ( _get_delims , _set_delims , """<STR_LIT>""" ) <EOL> def _parse ( self , source , encoding ) : <EOL> """<STR_LIT>""" <EOL> stream = [ ] <EOL> dirmap = { } <EOL> depth = <NUM_LIT:0> <EOL> source = source . read ( ) <EOL> if not isinstance ( source , unicode ) : <EOL> source = source . decode ( encoding or '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> offset = <NUM_LIT:0> <EOL> lineno = <NUM_LIT:1> <EOL> _escape_sub = self . _escape_re . sub <EOL> def _escape_repl ( mo ) : <EOL> groups = [ g for g in mo . groups ( ) if g ] <EOL> if not groups : <EOL> return '<STR_LIT>' <EOL> return groups [ <NUM_LIT:0> ] <EOL> for idx , mo in enumerate ( self . _directive_re . finditer ( source ) ) : <EOL> start , end = mo . span ( <NUM_LIT:1> ) <EOL> if start > offset : <EOL> text = _escape_sub ( _escape_repl , source [ offset : start ] ) <EOL> for kind , data , pos in interpolate ( text , self . filepath , lineno , <EOL> lookup = self . lookup ) : <EOL> stream . append ( ( kind , data , pos ) ) <EOL> lineno += len ( text . splitlines ( ) ) <EOL> lineno += len ( source [ start : end ] . splitlines ( ) ) <EOL> command , value = mo . group ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> if command == '<STR_LIT>' : <EOL> pos = ( self . filename , lineno , <NUM_LIT:0> ) <EOL> value = list ( interpolate ( value , self . filepath , lineno , <NUM_LIT:0> , <EOL> lookup = self . lookup ) ) <EOL> if len ( value ) == <NUM_LIT:1> and value [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] is TEXT : <EOL> value = value [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> stream . append ( ( INCLUDE , ( value , None , [ ] ) , pos ) ) <EOL> elif command == '<STR_LIT>' : <EOL> if not self . allow_exec : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' , <EOL> self . filepath , lineno ) <EOL> try : <EOL> suite = Suite ( value , self . filepath , lineno , <EOL> lookup = self . lookup ) <EOL> except SyntaxError , err : <EOL> raise TemplateSyntaxError ( err , self . filepath , <EOL> lineno + ( err . lineno or <NUM_LIT:1> ) - <NUM_LIT:1> ) <EOL> pos = ( self . filename , lineno , <NUM_LIT:0> ) <EOL> stream . append ( ( EXEC , suite , pos ) ) <EOL> elif command == '<STR_LIT:end>' : <EOL> depth -= <NUM_LIT:1> <EOL> if depth in dirmap : <EOL> directive , start_offset = dirmap . pop ( depth ) <EOL> substream = stream [ start_offset : ] <EOL> stream [ start_offset : ] = [ ( SUB , ( [ directive ] , substream ) , <EOL> ( self . filepath , lineno , <NUM_LIT:0> ) ) ] <EOL> elif command : <EOL> cls = self . get_directive ( command ) <EOL> if cls is None : <EOL> raise BadDirectiveError ( command ) <EOL> directive = <NUM_LIT:0> , cls , value , None , ( self . filepath , lineno , <NUM_LIT:0> ) <EOL> dirmap [ depth ] = ( directive , len ( stream ) ) <EOL> depth += <NUM_LIT:1> <EOL> offset = end <EOL> if offset < len ( source ) : <EOL> text = _escape_sub ( _escape_repl , source [ offset : ] ) <EOL> for kind , data , pos in interpolate ( text , self . filepath , lineno , <EOL> lookup = self . lookup ) : <EOL> stream . append ( ( kind , data , pos ) ) <EOL> return stream <EOL> class OldTextTemplate ( Template ) : <EOL> """<STR_LIT>""" <EOL> directives = [ ( '<STR_LIT>' , DefDirective ) , <EOL> ( '<STR_LIT>' , WhenDirective ) , <EOL> ( '<STR_LIT>' , OtherwiseDirective ) , <EOL> ( '<STR_LIT>' , ForDirective ) , <EOL> ( '<STR_LIT>' , IfDirective ) , <EOL> ( '<STR_LIT>' , ChooseDirective ) , <EOL> ( '<STR_LIT>' , WithDirective ) ] <EOL> serializer = '<STR_LIT:text>' <EOL> _DIRECTIVE_RE = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' , <EOL> re . MULTILINE ) <EOL> def _parse ( self , source , encoding ) : <EOL> """<STR_LIT>""" <EOL> stream = [ ] <EOL> dirmap = { } <EOL> depth = <NUM_LIT:0> <EOL> source = source . read ( ) <EOL> if not isinstance ( source , unicode ) : <EOL> source = source . decode ( encoding or '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> offset = <NUM_LIT:0> <EOL> lineno = <NUM_LIT:1> <EOL> for idx , mo in enumerate ( self . _DIRECTIVE_RE . finditer ( source ) ) : <EOL> start , end = mo . span ( ) <EOL> if start > offset : <EOL> text = source [ offset : start ] <EOL> for kind , data , pos in interpolate ( text , self . filepath , lineno , <EOL> lookup = self . lookup ) : <EOL> stream . append ( ( kind , data , pos ) ) <EOL> lineno += len ( text . splitlines ( ) ) <EOL> text = source [ start : end ] . lstrip ( ) [ <NUM_LIT:1> : ] <EOL> lineno += len ( text . splitlines ( ) ) <EOL> directive = text . split ( None , <NUM_LIT:1> ) <EOL> if len ( directive ) > <NUM_LIT:1> : <EOL> command , value = directive <EOL> else : <EOL> command , value = directive [ <NUM_LIT:0> ] , None <EOL> if command == '<STR_LIT:end>' : <EOL> depth -= <NUM_LIT:1> <EOL> if depth in dirmap : <EOL> directive , start_offset = dirmap . pop ( depth ) <EOL> substream = stream [ start_offset : ] <EOL> stream [ start_offset : ] = [ ( SUB , ( [ directive ] , substream ) , <EOL> ( self . filepath , lineno , <NUM_LIT:0> ) ) ] <EOL> elif command == '<STR_LIT>' : <EOL> pos = ( self . filename , lineno , <NUM_LIT:0> ) <EOL> stream . append ( ( INCLUDE , ( value . strip ( ) , None , [ ] ) , pos ) ) <EOL> elif command != '<STR_LIT:#>' : <EOL> cls = self . get_directive ( command ) <EOL> if cls is None : <EOL> raise BadDirectiveError ( command ) <EOL> directive = <NUM_LIT:0> , cls , value , None , ( self . filepath , lineno , <NUM_LIT:0> ) <EOL> dirmap [ depth ] = ( directive , len ( stream ) ) <EOL> depth += <NUM_LIT:1> <EOL> offset = end <EOL> if offset < len ( source ) : <EOL> text = source [ offset : ] . replace ( '<STR_LIT>' , '<STR_LIT:#>' ) <EOL> for kind , data , pos in interpolate ( text , self . filepath , lineno , <EOL> lookup = self . lookup ) : <EOL> stream . append ( ( kind , data , pos ) ) <EOL> return stream <EOL> TextTemplate = OldTextTemplate </s>
