Bricklayer-Templater

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Wed Aug 29 22:33:24 CDT 2007  jeremy@marzhillstudios.com
  * debugging flag for common::object handler

Wed Aug 29 20:35:39 CDT 2007  jeremy@marzhillstudios.com
  * up the Bricklayer version

Wed Aug 29 20:34:58 CDT 2007  jeremy@marzhillstudios.com
  * expand testing for the parser

Wed Aug 29 20:34:00 CDT 2007  jeremy@marzhillstudios.com
  * correctly handle tagid and start and end brackets in templater

Wed Aug 29 20:33:39 CDT 2007  jeremy@marzhillstudios.com
  * cleanup sequencer code

Wed Aug 29 20:33:17 CDT 2007  jeremy@marzhillstudios.com
  * clean up parser code

Wed Aug 29 20:32:35 CDT 2007  jeremy@marzhillstudios.com
  * correctly handle negation in the object handler

lib/Bricklayer/Templater/Handler.pm  view on Meta::CPAN

=item type
    
    $self->type() returns the tag type. This shoule be either 'block' or 'single' or 'text'. You
    probably will never need to use this.

=item tagname
    
    $self->tagname() returns the tag's name. This should be the classname minus the 
    Bricklayer::Templater::Handler:: part.

=item tagid
    
    $self->tagid() returns the template engines current template tag identifier. It is equivalent to
    calling $self->app()->identifier.

=back

=head3 handler object Methods

=over 1

=item load

lib/Bricklayer/Templater/Handler.pm  view on Meta::CPAN

}

sub type {
	return $_[0]->{Token}{type};
}

sub tagname {
	return $_[0]->{Token}{tagname};
}

sub tagid {
	return $_[0]->app()->identifier();
}

sub app {
    return $_[0]->{App};
}

sub parse_block {
	$_[0]->app->run_sequencer($_[0]->block(), $_[1]);
	return ;

lib/Bricklayer/Templater/Parser.pm  view on Meta::CPAN

use strict;
use Carp;

# Global Variables
my $DefaultTagID = 'BK';

# Package Methods
sub parse_text {
	# Function wide variables
	my $textblock = shift; 	#this variable contains the block of text to be parsed.
	my $tagID = shift or croak('Must pass in tagid');		# this variable contains the ID of the tag it is optional
    my $startbracket = shift or croak('Must pass in startbracket');
	my $endbracket   = shift or croak('Must pass in endbracket');
    my $TagStartText = $startbracket.$tagID; # Start tag prefix (it gets passed in)
    my $TagEndText = $startbracket.'/'.$tagID; # End tag prefix (it also gets passed in)
	#carp("startbracket: $startbracket");
	#carp("endbracket: $endbracket");
	#carp("TagStartText: $TagStartText");
	#carp("TagEndText: $TagEndText");
    my @tokens = ();				#this variable will contain the tokenized text.
	 

lib/Bricklayer/Templater/Parser.pm  view on Meta::CPAN

				$block = substr($textblock, 0, $endpos);
				#remove the text block from $textblock.
				$textblock = substr($textblock, $endpos);
			}			

			# create and populate the token
			%token = (
				type => "text", 
				tagname => "text_block", 
				block => $block,
		        tagid => $tagID
			);

			#put the token at the bottom of the stack.
			push @tokens, \%token; 
		} 
		# check to see if the first part of the string is a tag
		elsif (substr($textblock, 0, length($TagStartText)) eq $TagStartText) {
			my $tag;
			
			# get the tag from the string.

lib/Bricklayer/Templater/Parser.pm  view on Meta::CPAN

				$tag = substr($tag, 0, index($tag, $endbracket));

				%attributes = parse_attributes($tag);
		
				# populate the token			
				%token = (
					type => "container",
					tagname => $tagname,
					block => $block,
					attributes => \%attributes,
					tagid => $tagID
				);
				
				
			} else {
				# this parses a single tag out.

				# Get the tag name.
				$tagname = substr($tag, length($TagStartText), index($tag, $endbracket)-(length($TagStartText)+1));
				if (index($tagname, ' ') != -1) {
					$tagname = substr($tagname, 0, index($tagname, ' '));

lib/Bricklayer/Templater/Parser.pm  view on Meta::CPAN

				$tag = substr($tag,length($TagStartText));
				$tag = substr($tag, 0, index($tag, $endbracket));
				%attributes = parse_attributes($tag);
		
				# populate the token
				%token = (
					type => "single", 
					tagname => $tagname,
					block => undef,
					attributes => \%attributes,
					tagid => $tagID
				);
			}
			
			# put the token on the bottom of the stack.
			push @tokens, \%token;
		}
	} 
			
		
	return @tokens;

t/01bricklayer_templater_parser.t  view on Meta::CPAN

my $TEMPLATE = '<BKcommon::row><BKutil::bench></BKutil::bench></BKcommon::row><BKutil::tester></BKutil::tester>';
my $TEMPLATE2 = '<BKcommon::row attrib="1"><BKutil::bench></BKutil::bench></BKcommon::row><BKutil::tester></BKutil::tester>';

plan tests =>  1 # test that parser module can be loaded
              +3 # test that parser fails without proper setup variables
              +3 # test for simple template parsing
              +2 # test that template attributes are parsed correctly
              +3 # test for mytrim
	      ;

my @cmp = ({type => 'container', tagname => 'common::row', attributes => {undef}, block => '<BKutil::bench></BKutil::bench>', tagid => 'BK'}, {type => 'container', tagname => 'util::tester', attributes => {undef}, block => '', tagid => 'BK'});
my @cmp2 = ({type => 'container', tagname => 'common::row', attributes => {attrib => 1}, block => '<BKutil::bench></BKutil::bench>', tagid => 'BK'}, {type => 'container', tagname => 'util::tester', attributes => {undef}, block => '', tagid => 'BK'});

my @tokens;

{ 
    use_ok('Bricklayer::Templater::Parser');
}

{
    dies_ok(sub {Bricklayer::Templater::Parser::parse_text($TEMPLATE, undef, '<', '>'); }, 'parse without tagid fails' );
    dies_ok(sub {Bricklayer::Templater::Parser::parse_text($TEMPLATE, 'BK', undef, '>'); }, 'parse without start_bracket fails' );
    dies_ok(sub {Bricklayer::Templater::Parser::parse_text($TEMPLATE, undef, '<', undef); }, 'parse without end_bracket fails' );
}

{
    ok(@tokens = Bricklayer::Templater::Parser::parse_text($TEMPLATE, 'BK', '<', '>'), 'Succeeded in parsing simple template');
    ok(scalar @tokens == 2, 'There were two tokens');
    is_deeply(\@tokens, \@cmp, 'Token Structure is correct');
    ok(@tokens = Bricklayer::Templater::Parser::parse_text($TEMPLATE2, 'BK', '<', '>'), 'Succeeded in parsing simple template with attributes');
    is_deeply(\@tokens, \@cmp2, 'Token Structure with attributes is correct');

t/02bricklayer_templater.t  view on Meta::CPAN

use Test::MockObject;
use Test::MockObject::Extends;
use Cwd;
use strict;

use lib '..';
my @coremethods = qw{new};
my @tmethods = qw{load_template_file run_templater run_sequencer _page publish clear app WD};
my @attribs = qw{app WD start_bracket end_bracket ext identifier _page _template};
my $TEMPLATE = '<BKcommon::row attrib="1"><BKutil::bench></BKutil::bench></BKcommon::row><BKutil::tester></BKutil::tester>';
my @cmp2 = ({type => 'container', tagname => 'common::row', attributes => {attrib => 1}, block => '<BKutil::bench></BKutil::bench>', tagid => 'BK'}, {type => 'container', tagname => 'util::tester', attributes => {undef}, block => '', tagid => 'BK'});
my $t;
my $app = bless({}, 'Some::Class');
my $cwd = cwd();
my $ep = 'tester was here :-)';
plan tests =>  1 # test that module can be loaded
              +4 # test instance creation
              +1 # test that the core and templater BK methods exist
              +1 # test that the attribute accessors exist
              +4 # test the defaults are set for the accessors correctly
              +2 # test that the app and wd methods return correct values

t/03bricklayer_templater_handler.t  view on Meta::CPAN

             block      => 'block text',
             type       => 'someType',
             tagname     => 'someTag',
};

my $app = Test::MockObject->new({});
$app->fake_module('Some::App');
$app->mock('identifier' => sub { "BK" });
$app->mock('run_sequencer' => sub { die 'ran sequencer'; });

my @methods = qw{attributes block type tagname tagid app parse_block run_handler };

BEGIN: {
    plan tests =>  1 # test that module compiles
                  +1 # test that can create a handler
                  +2 # test argument validation
                  +1 # test methods exist
                  +6 # test attribute methods
                  +1 # test parse_block() functionality
                  ;    

t/03bricklayer_templater_handler.t  view on Meta::CPAN

    ok($h = Bricklayer::Templater::Handler->load($token, $app), 'Loaded handler object'); 
    dies_ok(sub {Bricklayer::Templater::Handler->load(undef, $app)}, 'failed Loading handler object without Token'); 
    dies_ok(sub {Bricklayer::Templater::Handler->load($token, undef)}, 'failed Loading handler object without context object'); 
}

{
    can_ok($h, @methods);
    isa_ok($h->app(), 'Test::MockObject');
    ok($h->attributes()->{foo} eq 'bar', 'Attributes are accessed correctly');
    ok($h->block() eq $token->{block}, 'Block is accessed correctly');
    ok($h->tagid eq $app->identifier(), 'tagid ['.$app->identifier().'] is accessed correctly');
    ok($h->type eq $token->{type}, 'type ['.$token->{type}.'] is accessed correctly');
    ok($h->tagname eq $token->{tagname}, 'tagname ['.$token->{tagname}.'] is accessed correctly');
}

{
   dies_ok(sub {$h->parse_block();}, 'sequencer was called'); 
}

t/bricklayer_templater_handlers.t  view on Meta::CPAN

             block      => 'block text',
             type       => 'someType',
             tagname     => 'someTag',
};

my $app = Test::MockObject->new({});
$app->fake_module('Some::App');
$app->mock('identifier' => sub { "BK" });
$app->mock('run_sequencer' => sub { die 'ran sequencer'; });

my @methods = qw{attributes block type tagname data tagid app parse_block run_handler };

BEGIN: {
    plan tests =>  1 # test that module compiles
                  ;    

    use_ok('Bricklayer::Templater::Handler');
}



( run in 2.009 seconds using v1.01-cache-2.11-cpan-5735350b133 )