[Templates-cvs] cvs commit: TT3/lib/Template/TT3 Compiler.pm Scanner.pm Tagset.pm
cvs@template-toolkit.org
cvs@template-toolkit.org
Wed, 01 Dec 2004 11:10:42 +0000
cvs 04/12/01 11:10:42
Modified: lib/Template/TT3 Compiler.pm Scanner.pm Tagset.pm
Log:
* added new Template::TT3::* module which subclass the Template::* modules
Revision Changes Path
1.8 +14 -195 TT3/lib/Template/TT3/Compiler.pm
Index: Compiler.pm
===================================================================
RCS file: /template-toolkit/TT3/lib/Template/TT3/Compiler.pm,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Compiler.pm 2004/11/24 16:16:15 1.7
+++ Compiler.pm 2004/12/01 11:10:42 1.8
@@ -3,8 +3,8 @@
# Template::TT3::Compiler
#
# DESCRIPTION
-# This module implements a facade for the entire compiler sub-system,
-# providing a simple API which abstracts the internal complexity.
+# Subclass of Template::Compiler which defaults to using the TT3
+# scanner.
#
# AUTHOR
# Andy Wardley <abw@wardley.org>
@@ -16,7 +16,7 @@
# modify it under the same terms as Perl itself.
#
# REVISION
-# $Id: Compiler.pm,v 1.7 2004/11/24 16:16:15 abw Exp $
+# $Id: Compiler.pm,v 1.8 2004/12/01 11:10:42 abw Exp $
#
#========================================================================
@@ -24,204 +24,23 @@
use strict;
use warnings;
-use Template::TT3::Base;
-use base qw( Template::TT3::Base );
-use vars qw( $VERSION $DEBUG $ERROR $WARNING
- $SCANNER $PARSER $GRAMMAR $HANDLER $TAGSET $GENERATOR
- @CONFIG );
-
-$VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
-$DEBUG = 0 unless defined $DEBUG;
-$ERROR = '';
-$SCANNER = 'Template::TT3::Scanner';
-$PARSER = 'Template::TT3::Parser';
-$GRAMMAR = 'Template::TT3::Grammar';
-$HANDLER = 'Template::TT3::Handler';
-$TAGSET = 'Template::TT3::Tagset';
-$GENERATOR = 'Template::TT3::Generator';
-@CONFIG = qw( scanner parser grammar handler tagset generator );
+use Template::TT3::Scanner;
+use Template::Compiler;
+use base qw( Template::Compiler );
+
+our $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
+our $DEBUG = 0 unless defined $DEBUG;
+our $ERROR = '';
+our $SCANNER = 'Template::TT3::Scanner';
-#------------------------------------------------------------------------
-# init(\%config)
-#
-# Initialiser method called by base class new() method.
-#------------------------------------------------------------------------
-
-sub init {
- my ($self, $config) = @_;
- my $class = ref $self || $self;
-
- # the scanner, parser, grammar and handler options default
- # the the values defined in the package variables of the
- # subclass, or failing that, the base class values above.
- # this is all handled by the pkgconf() base class methof
-
- $self->pkgconf($config, @CONFIG)
- || return;
-
- foreach my $key (@CONFIG) {
- my $load_key = "load_$key";
- $self->{ $load_key } = $config->{ $load_key }
- if exists $config->{ $load_key };
- }
-
- $self->{ config } = $config;
-
- return $self;
-}
-
-sub compile {
- my $self = shift;
- $self->compile_text(@_);
-}
-
-sub compile_text {
- my $self = shift;
- my $text = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
- my $parsed = $self->parse($text, $config) || return;
- return $self->generate($parsed, $config);
-}
-
-
-sub parse {
- my $self = shift;
- my $text = shift;
- my $textref = ref $text ? $text : \$text;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
- my $handler = $self->handler($config) || return;
-
- # create tagset if there isn't already one in the config
- $config->{ tagset } ||= $self->tagset($config) || return;
-
- # create scanner using tagset defined in config
- my $scanner = $self->scanner($config) || return;
-
- # start handler
- $handler->start()
- || return $self->error($handler->error());
-
- # scanner template
- $handler = $scanner->scan($textref, $handler)
- || return $self->error($scanner->error());
-
- # end handler
- return $handler->end()
- || $self->error($handler->error());
-}
-
-sub generate {
- my $self = shift;
- my $parsed = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
- my $generator = $config->{ generator } || $self->generator() || return;
- return $generator->generate($parsed) || $self->error($generator->error());
-}
-
-
-sub handler {
- my $self = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
-
- $self->debug("creating handler\n") if $DEBUG;
-
- # merge in any config options left over from constructor
- $config = { %{ $self->{ config } }, %$config };
-
- # create parser if necessary
- $config->{ parser } ||= $self->parser($config) || return;
-
- # reference grammar used by parser if not already set
- $config->{ grammar } ||= $config->{ parser }->grammar();
-
- # create handler object
- return $self->object( handler => $config );
-}
-
-
-sub tagset {
- my $self = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
-
- # merge in any config options left over from constructor
- $config = { %{ $self->{ config } }, %$config };
-
- $self->debug("creating tagset\n") if $DEBUG;
-
- # create parser for directive and variable tags to use
- $config->{ parser } ||= $self->parser($config) || return;
-
- # create tagset
- return $self->object( tagset => $config );
-}
-
-
-sub parser {
- my $self = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
-
- # merge in any config options left over from constructor
- $config = { %{ $self->{ config } }, %$config };
-
- $self->debug("creating parser\n") if $DEBUG;
-
- # create grammar for parser
- $config->{ grammar } ||= $self->grammar($config) || return;
-
- # create parser
- return $self->object( parser => $config );
-}
-
-
-sub grammar {
- my $self = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
-
- # merge in any config options left over from constructor
- $config = { %{ $self->{ config } }, %$config };
-
- $self->debug("creating grammar\n") if $DEBUG;
-
- return $self->object( grammar => $config );
-}
-
-
-sub scanner {
- my $self = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
-
- # merge in any config options left over from constructor
- $config = { %{ $self->{ config } }, %$config };
-
- $self->debug("creating scanner\n") if $DEBUG;
-# $self->debug("tagset: $config->{ tagset }\n");
-
- return $self->object( scanner => $config );
-}
-
-sub generator {
- my $self = shift;
- my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
-
- # merge in any config options left over from constructor
- $config = { %{ $self->{ config } }, %$config };
-
- $self->debug("creating generator\n") if $DEBUG;
-
- return $self->object( generator => $config );
-}
-
-
-
-
-
1;
+
__END__
=head1 NAME
-Template::TT3::* - TODO
+Template::TT3::Compiler - template compiler for TT3
=head1 SYNOPSIS
@@ -243,7 +62,7 @@
=head1 VERSION
-$Revision: 1.7 $
+$Revision: 1.8 $
=head1 COPYRIGHT
1.13 +5 -4 TT3/lib/Template/TT3/Scanner.pm
Index: Scanner.pm
===================================================================
RCS file: /template-toolkit/TT3/lib/Template/TT3/Scanner.pm,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- Scanner.pm 2004/11/18 13:44:28 1.12
+++ Scanner.pm 2004/12/01 11:10:42 1.13
@@ -16,7 +16,7 @@
# modify it under the same terms as Perl itself.
#
# REVISION
-# $Id: Scanner.pm,v 1.12 2004/11/18 13:44:28 abw Exp $
+# $Id: Scanner.pm,v 1.13 2004/12/01 11:10:42 abw Exp $
#
#========================================================================
@@ -24,13 +24,14 @@
use strict;
use warnings;
+use Template::TT3::Tagset;
use Template::Scanner;
use base qw( Template::Scanner );
-our $VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/);
+our $VERSION = sprintf("%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/);
our $DEBUG = 0 unless defined $DEBUG;
our $ERROR = '';
-our $TAGSET = 'Template::Tagset::TT';
+our $TAGSET = 'Template::TT3::Tagset';
1;
@@ -65,7 +66,7 @@
=head1 VERSION
-$Revision: 1.12 $
+$Revision: 1.13 $
=head1 COPYRIGHT
1.4 +106 -3 TT3/lib/Template/TT3/Tagset.pm
Index: Tagset.pm
===================================================================
RCS file: /template-toolkit/TT3/lib/Template/TT3/Tagset.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Tagset.pm 2004/11/18 13:43:54 1.3
+++ Tagset.pm 2004/12/01 11:10:42 1.4
@@ -16,7 +16,7 @@
# modify it under the same terms as Perl itself.
#
# REVISION
-# $Id: Tagset.pm,v 1.3 2004/11/18 13:43:54 abw Exp $
+# $Id: Tagset.pm,v 1.4 2004/12/01 11:10:42 abw Exp $
#
#========================================================================
@@ -24,16 +24,119 @@
use strict;
use warnings;
+use Template::TT3::Tag::Directive;
+use Template::Tagset::Interpolate;
use Template::Tagset;
use base qw( Template::Tagset );
-our $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
+our $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
our $DEBUG = 0 unless defined $DEBUG;
our $ERROR = '';
+our $TAGS = {
+ directive => 'Template::TT3::Tag::Directive',
+ interpolate => 'Template::Tagset::Interpolate',
+} unless defined $TAGS;
+#------------------------------------------------------------------------
+# pkgtags($config)
+#
+# Custom method to instantiate the default $TAGS for this package (or
+# any subclass which may define a new $TAGS set), including the
+# 'directive' tag and 'interpolate' tagset.
+#------------------------------------------------------------------------
+
+sub pkgtags {
+ my ($self, $config) = @_;
+
+ # $TAGS may be re-defined by subclass and can be a list or hash ref
+ my $pkgtags = $self->pkgvar( TAGS => $TAGS );
+ $pkgtags = UNIVERSAL::isa($pkgtags, 'HASH') ? { %$pkgtags }
+ : UNIVERSAL::isa($pkgtags, 'ARRAY') ? { @$pkgtags }
+ : return $self->error("invalid $TAGS package variable: $pkgtags");
+ my $pkglist = UNIVERSAL::isa($pkgtags, 'HASH') ? [ %$pkgtags ] : $pkgtags;
+ my $dirclass = $pkgtags->{ directive };
+ my $intclass = $pkgtags->{ interpolate };
+ my ($dirtag, $inttag);
+
+ # create new directive tag and interpolate tagset object unless we've
+ # already got references to objects for either of them
+
+ $dirtag = ref $dirclass ? $dirclass
+ : $dirclass->new( %$config )
+ || return $self->error( "failed to create directive tag: ",
+ $dirclass->error() );
+
+ $inttag = ref $intclass ? $intclass
+ : $intclass->new( %$config,
+ enabled => $config->{ interpolate } ? 1 : 0 )
+ || return $self->error( "failed to create interpolate tagset: ",
+ $intclass->error() );
+
+ return [ @$pkglist, directive => $dirtag, interpolate => $inttag ];
+}
+
+
+#------------------------------------------------------------------------
+# directive()
+# directive($flag)
+#
+# Enables or disables the directive tag when an argument is passed, or
+# returns the directive tag.
+#------------------------------------------------------------------------
+
+sub directive {
+ my $self = shift;
+ return @_ ? $self->switch( directive => @_ ) : $self->tag('directive');
+}
+
+
+#------------------------------------------------------------------------
+# interpolate()
+# interpolate($flag)
+#
+# Enables or disables the interpolate tag when an argument is passed, or
+# returns the interpolate tag.
+#------------------------------------------------------------------------
+
+sub interpolate {
+ my $self = shift;
+ return @_ ? $self->switch( interpolate => @_ ) : $self->tag('interpolate');
+}
+
+#------------------------------------------------------------------------
+# tag_style()
+# tag_start()
+# tag_end()
+#
+# Delegate to the style(), start() and end() tags of the directive tag.
+#------------------------------------------------------------------------
+
+sub tag_style {
+ my $self = shift;
+ my $dir = $self->directive() || return;
+ $self->debug("tag_style($dir => $_[0])\n") if $DEBUG;
+ return $dir->style(@_) || $self->error($dir->error());
+}
+
+sub tag_start {
+ my $self = shift;
+ my $dir = $self->directive() || return;
+ $self->debug( "tag_start($dir => $_[0])\n" ) if $DEBUG;
+ return $dir->start(@_) || $self->error($dir->error());
+}
+
+sub tag_end {
+ my $self = shift;
+ my $dir = $self->directive() || return;
+ $self->debug("tag_end($dir => $_[0])\n") if $DEBUG;
+ return $dir->end(@_) || $self->error($dir->error());
+}
+
+
1;
+
__END__
=head1 NAME
@@ -54,7 +157,7 @@
=head1 VERSION
-$Revision: 1.3 $
+$Revision: 1.4 $
=head1 COPYRIGHT