[Templates-cvs] cvs commit: TT3/lib/Template/Tag Directive.pm

cvs@template-toolkit.org cvs@template-toolkit.org
Fri, 03 Dec 2004 13:38:38 +0000


cvs         04/12/03 13:38:38

  Modified:    lib/Template/Tag Directive.pm
  Log:
  * added sinister hack to allow the parser to hook back into the tag
    to handle nested tags
  
  Revision  Changes    Path
  1.12      +21 -12    TT3/lib/Template/Tag/Directive.pm
  
  Index: Directive.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Tag/Directive.pm,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Directive.pm	2004/12/01 11:12:13	1.11
  +++ Directive.pm	2004/12/03 13:38:37	1.12
  @@ -16,7 +16,7 @@
   #   modify it under the same terms as Perl itself.
   #
   # REVISION
  -#   $Id: Directive.pm,v 1.11 2004/12/01 11:12:13 abw Exp $
  +#   $Id: Directive.pm,v 1.12 2004/12/03 13:38:37 abw Exp $
   #
   # TODO
   #   * if we change the directives then we need to notify parser
  @@ -35,7 +35,7 @@
   use Template::Tag;
   use base qw( Template::Tag );
   
  -our $VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
  +our $VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/);
   our $DEBUG   = 0 unless defined $DEBUG;
   our $ERROR   = '';
   
  @@ -190,23 +190,26 @@
       $self->debug("init_parser()  end => $self->{ end }\n") if $DEBUG;
   
       my $parser = $self->{ parser } || $self->pkgvar( PARSER => $PARSER );
  -    my $endtag = $self->{ end };
  -    $endtag = ref $endtag eq 'Regexp' ? $endtag : quotemeta($endtag);
  -    $endtag = qr/ [-=+]? $endtag /x;
  -#    $endtag = qr/ ([-=+]?) ($endtag) /x;
  +    my $start  = $self->{ start };
  +    my $end    = $self->{ end };
  +    $start     = ref $start eq 'Regexp' ? $start : quotemeta($start);
  +    $end       = ref $end   eq 'Regexp' ? $end   : quotemeta($end);
  +    $end       = qr/ [-=+]? $end /x;    # add closing flags to end
   
       if (ref $parser && UNIVERSAL::can($parser, 'clone')) {
           # $parser is an object. we call its clone() method 
           # to allow it to construct new regexen to detect the end token
           return $self->{ parser } = $parser->clone({
  -            tag_end    => $endtag,
  +            tag_start  => $start,
  +            tag_end    => $end,
               directives => $self->{ directives },
           }) || $self->error('failed to clone parser: ', $parser->error());
       }
       elsif (! ref $parser) {
           # parser is a class name so we call the new() method
           return $self->{ parser } = $parser->new({
  -            tag_end    => $endtag,
  +            tag_start  => $start,
  +            tag_end    => $end,
               directives => $self->{ directives },
           }) || return $self->error('failed to create parser: ', $parser->error());
       }
  @@ -227,9 +230,15 @@
   
       # save match info locally to make protect re-entrancy
       local $self->{ match } = $match;
  +    local $match->{ tag     } = $self;
  +    local $match->{ parser  } = $parser;
  +    local $match->{ handler } = $handler;
       $match->{ lines  } = 0;
       $match->{ end    } = $endtag;
  -    $match->{ parser } = $parser;
  +
  +    # NOTE: this is a temporary hack to make nested tags work.
  +    # BEWARE! the potential circular reference between match and parser
  +    local $parser->{ match } = $match;
   
       # we must have an end tag defined
       return $self->error('no end token defined for tag') 
  @@ -480,12 +489,12 @@
   
       # see if it looks like an assignment
       if ($self->{ implicit_set } 
  -        && ($expr = $parser->parse_assign($textref, $match))) {
  +        && ($expr = $parser->parse_assign($textref))) {
           $self->debug("implicit SET\n") if $DEBUG;
           return $handler->expr([ set => $expr ]);
       }
       elsif ($self->{ implicit_get }
  -           && ($expr = $parser->parse_expression($textref, $match))) {
  +           && ($expr = $parser->parse_expression($textref))) {
           $self->debug("implicit GET\n") if $DEBUG;
           return $handler->expr([ get => $expr ]);
       }
  @@ -607,7 +616,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.11 $
  +$Revision: 1.12 $
   
   =head1 COPYRIGHT