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

cvs@template-toolkit.org cvs@template-toolkit.org
Tue, 09 Nov 2004 13:25:11 +0000


cvs         04/11/09 13:25:10

  Modified:    lib/Template/Tag Closed.pm
  Log:
  * changed Template::Tag::Closed to be a subclass of Template::Tag for
    special cases (rather than the norm), implementing its own scan() method
  
  Revision  Changes    Path
  1.2       +55 -7     TT3/lib/Template/Tag/Closed.pm
  
  Index: Closed.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Tag/Closed.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Closed.pm	2004/11/08 18:47:57	1.1
  +++ Closed.pm	2004/11/09 13:25:10	1.2
  @@ -3,8 +3,8 @@
   # Template::Tag::Closed
   #
   # DESCRIPTION
  -#   Subclass of Template::Tag which additionally defines an explicit
  -#   end token marking the end of the tag.
  +#   Subclass of Template::Tag which scans ahead for the end token in 
  +#   advance.
   # 
   # AUTHOR
   #   Andy Wardley <abw@wardley.org>
  @@ -16,7 +16,7 @@
   #   modify it under the same terms as Perl itself.
   #
   # REVISION
  -#   $Id: Closed.pm,v 1.1 2004/11/08 18:47:57 abw Exp $
  +#   $Id: Closed.pm,v 1.2 2004/11/09 13:25:10 abw Exp $
   #
   #========================================================================
   
  @@ -28,7 +28,7 @@
   use vars qw( $VERSION $ERROR $DEBUG $TAG );
   use base qw( Template::Tag );
   
  -our $VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);
  +our $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
   our $DEBUG   = 0 unless defined $DEBUG;
   our $ERROR   = '';
   our $TAG     = {
  @@ -36,9 +36,57 @@
       end   => '%]',
   };
   
  -# alias scan() method directly to scan_closed()
  -*scan = \&Template::Tag::scan_closed;
   
  +#------------------------------------------------------------------------
  +# scan($textref, $handler, $match)
  +#
  +# Method to scan the text of a closed tag.
  +#------------------------------------------------------------------------
  +
  +sub scan {
  +    my ($self, $textref, $handler, $match) = @_;
  +
  +    $self->debug("scan_closed()\n") if $DEBUG;
  +
  +    # save match info locally to make protect re-entrancy
  +    local $self->{ match } = $match;
  +
  +    # compile regex to match end tag
  +    my $regex = $self->{ end_regex } ||= do {
  +        my $endtag = $self->{ end };
  +        return $self->tag_error('no end token defined for tag') 
  +            unless defined $endtag and length $endtag;
  +        $endtag = ref $endtag eq 'Regexp' ? $endtag : quotemeta($endtag);
  +        qr/ \G (.*?) ($endtag) /sx;
  +    };
  +    
  +    $self->debug("scanning for end with $regex\n") if $DEBUG;
  +
  +    # scan for closing tag and report error if not found
  +    return $self->error("no closing tag to match $match->{ start }")
  +        unless $$textref =~ /$regex/gc;
  +
  +    # TODO: should we set $match->{ offset } to 0, given that we're 
  +    # passing in a new text ref with different length?
  +
  +    my ($body, $end) = ($1, $2);
  +    $match->{ text  } = \$body;
  +    $match->{ end   } = $end;
  +    $match->{ lines } = 
  +          ( $match->{ start } =~ tr/\n// )
  +        + ( $body  =~ tr/\n// ) 
  +        + ( $end   =~ tr/\n// );
  +
  +    # call parse() method, catching errors thrown or reported via error()
  +    eval {
  +        $handler = $self->parse(\$body, $handler, $match);
  +    };
  +
  +    return $@ ? $self->error($@) : $handler;
  +}
  +
  +
  +
   
   1;
   __END__
  @@ -76,7 +124,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.1 $
  +$Revision: 1.2 $
   
   =head1 COPYRIGHT