| 1 | package Text::MicroMason::HTMLTemplate; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | require Text::MicroMason::Base; | |||||||||||||||||||||||||||
| 4 | 1 | require Text::MicroMason::TemplateDir; | |||||||||||||||||||||||||||
| 5 | 1 | require Text::MicroMason::StoreOne; | |||||||||||||||||||||||||||
| 6 | 1 | require Text::MicroMason::HasParams; | |||||||||||||||||||||||||||
| 7 | 1 | push @ISA, map "Text::MicroMason::$_", qw( TemplateDir StoreOne HasParams ); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | ###################################################################### | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | my %param_mapping = ( ### <<== INCOMPLETE ### | |||||||||||||||||||||||||||
| 14 | global_vars => 'loop_global_vars', | ||||||||||||||||||||||||||||
| 15 | cache => '-CompileCache', | ||||||||||||||||||||||||||||
| 16 | path => '-TemplatePaths', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ###################################################################### | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | sub output { (shift)->execute_again( @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
output: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ###################################################################### | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my $prefix_re = '[tT][mM][pP][lL]_'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 33 | ▶ | sub lex_token { | ||||||||||||||||||||||||||
|
Subroutine
lex_token: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | # warn " Lexer: " . pos($_) . " of " . length($_) . "\n"; | ||||||||||||||||||||||||||||
| 29 | # Tags in format "<TMPL_FOO>", "<TMPL_FOO NAME=VAR>", or "</TMPL_FOO>" | ||||||||||||||||||||||||||||
| 30 | 33 | ▶ | /\G \<(\/?)($prefix_re\w+)\s*(.*?)\> /gcxs | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 31 | ? ( ( $1 ? "tmpl_end" : lc($2) ) => { $_[0]->parse_args($3) } ) : | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Things that don't match the above | ||||||||||||||||||||||||||||
| 34 | /\G ( (?: [^<] | <(?!\/?$prefix_re) )+ ) /gcxs ? ( 'text' => $1 ) : | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Lexer error | ||||||||||||||||||||||||||||
| 37 | () | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 15 | ▶ | sub parse_args { | ||||||||||||||||||||||||||
|
Subroutine
parse_args: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | 15 | my $args = "$_[0]"; | |||||||||||||||||||||||||||
| 43 | 15 | ▶ | return () unless length($args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 9 | ▶ | return ( name => $args ) unless ( $args =~ /=/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 5 | my @tokens; | |||||||||||||||||||||||||||
| 46 | 5 | ▶ | until ( $args =~ /\G\z/gc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | push ( @tokens, | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$args =~ /\G ( .{0,20} ) /cgsx and die "Couldn't find applicable parsing rule at '$1'\n"
MC/DC0%0/1
$args =~ /\G ( .{0,20} ) /cgsx and die "Couldn't find applicable parsing rule at '$1'\n"
$args =~ /\G ( .{0,20} ) /cgsx
Truth table0%0/2
A
A= $args =~ /\G ( .{0,20} ) /cgsx
| |||||||||||||||||||||||||||||
| 48 | $args =~ /\G \s* (\w+) \= (?: \"([^\"]+)\" | ( \w+ ) ) (?= \s | \z ) /gcxs | ||||||||||||||||||||||||||||
| 49 | ? ( lc($1) => ( defined($2) ? $2 : $3 ) ) : | ||||||||||||||||||||||||||||
| 50 | $args =~ /\G ( .{0,20} ) /gcxs | ||||||||||||||||||||||||||||
| 51 | && die "Couldn't find applicable parsing rule at '$1'\n" | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 5 | @tokens; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | ###################################################################### | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | sub assemble_tmpl_var { | ||||||||||||||||||||||||||
|
Subroutine
assemble_tmpl_var: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 3 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | my $output = "\$m->param( '$args->{name}' )"; | |||||||||||||||||||||||||||
| 63 | 3 | ▶ | if ( defined $args->{default} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $output = "local \$_ = $output; defined ? \$_ : '$args->{default}'" | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if ( $args->{escape} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $output = "\$m->filter( $output, '$args->{escape}' )" | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 3 | expr => "$output;" | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub assemble_tmpl_include { | ||||||||||||||||||||||||||
|
Subroutine
assemble_tmpl_include: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 0 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 74 | file => $args->{name} | ||||||||||||||||||||||||||||
| 75 | 0 | } | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | sub assemble_tmpl_loop { | ||||||||||||||||||||||||||
|
Subroutine
assemble_tmpl_loop: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 2 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 79 | 2 | ▶ | if ( ! $self->{loop_context_vars} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | perl => q/foreach my $args ( $m->param( '/ . $args->{name} . q/' ) ) { | |||||||||||||||||||||||||||
| 81 | local $m->{params} = [ $args, $m->{loop_global_vars} ? @{$m->{params}} : () ];/ | ||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||
| 83 | 1 | perl => q/my @loop = $m->param( '/ . $args->{name} . q/' ); | |||||||||||||||||||||||||||
| 84 | foreach my $count ( 0 .. $#loop ) { | ||||||||||||||||||||||||||||
| 85 | my $args = $loop[ $count ]; | ||||||||||||||||||||||||||||
| 86 | my %loop_context = ( | ||||||||||||||||||||||||||||
| 87 | __counter__ => $count, | ||||||||||||||||||||||||||||
| 88 | __odd__ => ( $count % 2 ), | ||||||||||||||||||||||||||||
| 89 | __first__ => ( $count == 0 ), | ||||||||||||||||||||||||||||
| 90 | __inner__ => ( $count > 0 and $count < $#loop ), | ||||||||||||||||||||||||||||
| 91 | __last__ => ( $count == $#loop ), | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | local $m->{params} = [ $args, \%loop_context, $m->{loop_global_vars} ? @{$m->{params}} : () ]; | ||||||||||||||||||||||||||||
| 94 | / | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub assemble_tmpl_if { | ||||||||||||||||||||||||||
|
Subroutine
assemble_tmpl_if: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 2 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 100 | 2 | perl => q/if ( $m->param( '/ . $args->{name} . q/' ) ) { / | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | ▶ | sub assemble_tmpl_unless { | ||||||||||||||||||||||||||
|
Subroutine
assemble_tmpl_unless: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 2 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 105 | 2 | perl => q/if ( ! $m->param( '/ . $args->{name} . q/' ) ) { / | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub assemble_tmpl_else { | ||||||||||||||||||||||||||
|
Subroutine
assemble_tmpl_else: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | perl => "} else {" | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | sub assemble_tmpl_end { | ||||||||||||||||||||||||||
|
Subroutine
assemble_tmpl_end: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 6 | perl => "}" | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | ###################################################################### | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | ▶ | use vars qw( %Filters ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 11 | ▶ | sub defaults { | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 11 | (shift)->NEXT('defaults'), filters => \%Filters, | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Output filtering | ||||||||||||||||||||||||||||
| 125 | $Filters{1} = $Filters{html} = \&HTML::Entities::encode | ||||||||||||||||||||||||||||
| 126 | 1 | ▶ | if eval { require HTML::Entities}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | $Filters{url} = \&URI::Escape::uri_escape if eval { require URI::Escape }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # $result = $mason->filter( @filters, $content ); | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub filter { | ||||||||||||||||||||||||||
|
Subroutine
filter: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 132 | 0 | my $content = pop; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | foreach my $filter ( @_ ) { | |||||||||||||||||||||||||||
| 135 | my $function = ( ref $filter eq 'CODE' ) ? $filter : | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $self->{filters}{ $filter } || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"filters"}{$filter} or $self->croak_msg("No definition for a filter named '${filter}'")
MC/DC0%0/2
$self->{"filters"}{$filter} or $self->croak_msg("No definition for a filter named '${filter}'")
$self->{"filters"}{$filter}
$self->croak_msg("No definition for a filter named '${filter}'")
Truth table0%0/3
A or B
A= $self->{"filters"}{$filter}B= $self->croak_msg("No definition for a filter named '${filter}'")
| |||||||||||||||||||||||||||||
| 137 | $self->croak_msg("No definition for a filter named '$filter'" ); | ||||||||||||||||||||||||||||
| 138 | 0 | $content = &$function($content) | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | $content | ||||||||||||||||||||||||||||
| 141 | 0 | } | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | ###################################################################### | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | 1; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||