| 1 | package HTML::Template::Nest; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.020001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use HTML::Template; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | sub new{ | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | my ($class,%opts) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | my $self = {%opts}; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | #defaults | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | $self->{comment_tokens} = [ '<!--','-->' ] unless $self->{comment_tokens}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | $self->{name_label} = 'NAME' unless $self->{name_label}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | $self->{template_dir} = '' unless defined $self->{template_dir}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | $self->{template_ext} = '.html' unless defined $self->{template_ext}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | $self->{show_labels} = 0 unless defined $self->{show_labels}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | bless $self,$class; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | sub template_dir{ | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | ▶ | my($self,$dir) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
template_dir: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 12 | ▶ | confess "Expected a scalar directory name but got a ".ref($dir) if $dir && ref($dir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dir and ref $dir
MC/DC0%0/2
$dir and ref $dir
$dir
ref $dir
Truth table66%2/3
A and B
A= $dirB= ref $dir
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 12 | ▶ | $self->{template_dir} = $dir if $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 12 | return $self->{template_dir}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | sub comment_tokens{ | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | my ($self,$token1,$token2) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
comment_tokens: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | if (defined $token1 ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | $token2 = $token2 || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$token2 || ''
MC/DC0%0/1
$token2 || ''
$token2
Truth table50%1/2
A
A= $token2
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | $self->{'comment_tokens'} = [ $token1, $token2 ]; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | return $self->{'comment_tokens'}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | sub show_labels{ | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 13 | ▶ | my ($self,$show) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
show_labels: called CC=5 SCAR=17.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 13 | ▶ | confess "Expected a boolean but got $show" if $show && ! ( $show == 0 || $show == 1 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$show and not $show == 0 || $show == 1
$show == 0 || $show == 1
MC/DC0%0/3
$show and not $show == 0 || $show == 1
$show
$show == 0
$show == 1
Truth table50%2/4
A and not(B || C)
A= $showB= $show == 0C= $show == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 13 | ▶ | $self->{show_labels} = $show if $show; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 13 | return $self->{show_labels}; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | sub template_ext{ | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 12 | ▶ | my ($self,$ext) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
template_ext: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 12 | ▶ | confess "Expected a scalar extension name but got a ".ref($ext) if defined $ext && ref($ext); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ext and ref $ext
MC/DC0%0/2
defined $ext and ref $ext
defined $ext
ref $ext
Truth table66%2/3
A and B
A= defined $extB= ref $ext
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 12 | ▶ | $self->{template_ext} = $ext if defined $ext; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 12 | return $self->{template_ext}; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | sub name_label{ | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 32 | ▶ | my ($self,$label) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name_label: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 32 | ▶ | confess "Expected a scalar name label but got a ".ref($label) if defined $label && ref($label); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $label and ref $label
MC/DC0%0/2
defined $label and ref $label
defined $label
ref $label
Truth table66%2/3
A and B
A= defined $labelB= ref $label
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 32 | ▶ | $self->{name_label} = $label if $label; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 32 | return $self->{name_label}; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | sub to_html{ | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 16 | ▶ | my ($self,$comp) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_html: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 16 | my $html; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 16 | ▶ | if ( ref($comp) =~ /array/i ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | $html = $self->_array_to_html( $comp ); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } elsif( ref( $comp ) =~ /hash/i ){ | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 10 | $html = $self->_hash_to_html( $comp ); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | $html = $comp; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 16 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | sub _hash_to_html{ | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 10 | ▶ | my ($self,$h) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hash_to_html: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | confess "Expected a hashref. Instead got a ".ref($h) unless ref($h) =~ /hash/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 10 | my $template_name = $h->{ $self->name_label }; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 10 | ▶ | confess 'Encountered hash with no name_label ("'.$self->name_label.'"): '.Dumper( $h ) unless $template_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 10 | my $param = {}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 10 | foreach my $k ( keys %$h ){ | |||||||||||||||||||||||||||||||||||||||||
| 104 | 20 | ▶ | next if $k eq $self->name_label; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 10 | $param->{$k} = $self->to_html( $h->{$k} ); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 10 | my $filename = File::Spec->catdir( | |||||||||||||||||||||||||||||||||||||||||
| 109 | $self->template_dir, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | $template_name.$self->template_ext | ||||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 10 | my $temp = HTML::Template->new( filename => $filename ); | |||||||||||||||||||||||||||||||||||||||||
| 114 | 10 | $temp->param( $_ => $param->{$_} ) foreach keys %$param; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | my $html = $temp->output; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 10 | ▶ | if ( $self->show_labels ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | my $ca = $self->{comment_tokens}->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 5 | my $cb = $self->{comment_tokens}->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 5 | $html = "$ca BEGIN $template_name $cb\n$html\n$ca END $template_name $cb\n"; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 10 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | sub _array_to_html{ | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | my ($self, $arr, $delim) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_array_to_html: called CC=5 SCAR=17.2
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | die "Expected an array. Instead got a ".ref($arr) unless ref($arr) =~ /array/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 2 | my $html = ''; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | foreach my $comp (@$arr){ | |||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | $html.= $delim if ($delim && $html); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$delim and $html
MC/DC0%0/2
$delim and $html
$delim
$html
Truth table33%1/3
A and B
A= $delimB= $html
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | $html.= $self->to_html( $comp ); | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | return $html; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||||||||||