[Templates-cvs] cvs commit: TT3/lib/Template/TT3/Tag Directive.pm
cvs@template-toolkit.org
cvs@template-toolkit.org
Thu, 11 Dec 2003 17:26:21 +0000
cvs 03/12/11 17:26:20
Added: lib/Template/TT3/Tag Directive.pm
Log:
added Directve.pm
Revision Changes Path
1.1 TT3/lib/Template/TT3/Tag/Directive.pm
Index: Directive.pm
===================================================================
#========================================================================
#
# Template::TT3::Tag::Directive
#
# DESCRIPTION
# Module implementing a Template Toolkit style directive with pre-chomp
# and post-chomp flags, comments, etc.
#
# 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: Directive.pm,v 1.1 2003/12/11 17:26:20 abw Exp $
#
#========================================================================
package Template::TT3::Tag::Directive;
use strict;
use warnings;
use Template::TT3::Tag;
use Template::TT3::Constants qw( :chomp );
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 => 'directive',
start => '[%',
end => '%]',
pre_chomp => 0,
post_chomp => 0,
};
sub parse {
my ($self, $textref, $document) = @_;
my ($flag, $comment, $result);
$self->debug("parsing directive\n") if $DEBUG;
# check for opening comment/chomp flag and remove leading whitespace
$$textref =~ s/ ^ ([-=+\#])? \s* //sx;
$flag = $1 || '';
# examine opening flag and pre_chomp options
if ($flag eq '#') {
# it's a comment
$comment = 1;
}
elsif ($flag eq '+') {
# do nothing - leave whitespace alone
}
elsif ($flag eq '=' || $self->{ pre_chomp } == CHOMP_COLLAPSE) {
# collapse preceding whitespace into a single space
$document->pre_chomp(CHOMP_COLLAPSE);
}
elsif ($flag eq '-' || $self->{ pre_chomp }) {
# remove all preceding whitespace
$document->pre_chomp(CHOMP_ALL);
}
# check for closing chomp flag and remove trailing whitespace
$$textref =~ s/ \s* ([-+])? $ //sx;
$flag = $1 || '';
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# here's where we do something interesting...
$result = $comment || $document->tag($textref)
|| $self->error($document->error());
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# examine closing flag and post_chomp options
if ($flag eq '+') {
# do nothing - leave whitespace alone
}
elsif ($flag eq '=' || $self->{ post_chomp } == CHOMP_COLLAPSE) {
# collapse followint whitespace into a single space
$document->post_chomp(CHOMP_COLLAPSE);
}
elsif ($flag eq '-' || $self->{ post_chomp }) {
# remove all following whitespace
$document->post_chomp(CHOMP_ALL);
}
return $result;
}
1;
__END__
=head1 NAME
Template::TT3::Tag - base class scanner for embedded tags
=head1 SYNOPSIS
package Template::TT3::Tag;
# TODO
=head1 DESCRIPTION
# TODO
=head1 METHODS
=head2 new()
# TODO
=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.
=cut
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: