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

cvs@template-toolkit.org cvs@template-toolkit.org
Fri, 19 Dec 2003 17:49:49 +0000


cvs         03/12/19 17:49:49

  Added:       lib/Template/TT3/Tag Closed.pm
  Log:
  Closed.pm
  
  Revision  Changes    Path
  1.1                  TT3/lib/Template/TT3/Tag/Closed.pm
  
  Index: Closed.pm
  ===================================================================
  #========================================================================
  #
  # Template::TT3::Tag::Closed
  #
  # DESCRIPTION
  #   Subclass of Template::TT3::Tag which additionally defines an explicit
  #   end token marking the end of the tag.
  # 
  # AUTHOR
  #   Andy Wardley <abw@wardley.org>
  #
  # COPYRIGHT
  #   Copyright (C) 1996-2003 Andy Wardley.  All Rights Reserved.
  #   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  #
  #   This module is free software; you can redistribute it and/or
  #   modify it under the same terms as Perl itself.
  #
  # REVISION
  #   $Id: Closed.pm,v 1.1 2003/12/19 17:49:49 abw Exp $
  #
  #========================================================================
  
  package Template::TT3::Tag::Closed;
  
  use strict;
  use warnings;
  use Template::TT3::Tag;
  use vars qw( $VERSION $DEBUG $ERROR $WARNING $TAG );
  use base qw( Template::TT3::Tag );
  
  $VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);
  $DEBUG   = 0 unless defined $DEBUG;
  $ERROR   = '';
  $TAG     = {
      name  => 'closed',
      start => '[%',
      end   => '%]',
  };
  
  
  
  #------------------------------------------------------------------------
  # scan($textref, $handler, $lineref, $start)
  #
  # Scan method for closed tags.  Uses the $self->{ end } token to locate
  # the end of the tag, defines a $self->{ match } record and then calls 
  # the parse() method for further processing.  Keeps track of lines 
  # consumed and updates $lineref accordingly.
  #------------------------------------------------------------------------
  
  sub scan {
      my ($self, $textref, $handler, $lineref, $start) = @_;
  
      local $self->{ match } = {
          start => $start,
          line  => $$lineref,
      };
  
      my $endtag = $self->{ end };
      return $self->tag_error('no end token defined for tag') 
          unless defined $endtag and length $endtag;
      
      my $regex = $self->{ end_regex } ||= do {
          $endtag = ref $endtag eq 'Regexp' ? $endtag : quotemeta($endtag);
          qr/ \G (.*?) ($endtag) /sx;
      };
      
      return $self->error('unterminated tag')
          unless $$textref =~ /$regex/gc;
  
      my ($body, $end) = ($1, $2);
  
      my $size = ( $start =~ tr/\n// ) 
               + ( $body  =~ tr/\n// ) 
               + ( $end   =~ tr/\n// );
  
      my $match = $self->{ match };
      $match->{ text } = \$body;
      $match->{ size } = $size;
      $match->{ end  } = $end;
  
      $handler = $self->parse(\$body, $handler);
  
      $$lineref += $size;
  
      return $handler;
  }
  
  
  #------------------------------------------------------------------------
  # end()
  # end($token)
  #
  # Accessor method to get/set the end token.
  #------------------------------------------------------------------------
  
  sub end {
      my $self = shift;
      if (@_) {
          $self->{ end } = shift;
          delete $self->{ end_regex };
      }
      return $self->{ end };
  }
  
  
  #------------------------------------------------------------------------
  # match()
  #
  # Accessor method to get the current match.
  #------------------------------------------------------------------------
  
  sub match {
      my $self = shift;
      return $self->{ match };
  }
  
  
  1;
  __END__
  
  =head1 NAME
  
  Template::TT3::Tag::Closed - template tag with start and end tokens
  
  =head1 SYNOPSIS
  
  # TODO
  
  =head1 DESCRIPTION
  
  #TODO
              
  =head1 METHODS
  
  =head2 new()
  
  
  =head2 scan($content, $document, $start, $end)
  
  
  
  =head2 end()
  
  Accessor method used to get/set the literal string or regular expression
  used to mark the end of the tag.  An argument passed to set a new 
  value should be a literal string or regular expression as per start().
  
      $tag->end('%>');
      print $tag->end();     # %>
  
  =head1 AUTHOR
  
  Andy Wardley  E<lt>abw@wardley.orgE<gt>
  
  =head1 VERSION
  
  $Revision: 1.1 $
  
  =head1 COPYRIGHT
  
    Copyright (C) 1996-2003 Andy Wardley.  All Rights Reserved.
    Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  
  This module is free software; you can redistribute it and/or
  modify it under the same terms as Perl itself.
  
  =head1 SEE ALSO
  
  L<Template::TT3::Tag>
  
  =cut
  
  # Local Variables:
  # mode: perl
  # perl-indent-level: 4
  # indent-tabs-mode: nil
  # End:
  #
  # vim: expandtab shiftwidth=4: