| 1 | 7 | ▶ | use strict; use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | package Template::Toolkit::Simple; | ||||||||||||||||||||||||||||
| 3 | 7 | our $VERSION = '0.31'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Template::Constants qw( :debug ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use YAML::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | our @EXPORT = qw(tt); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | sub tt { | ||||||||||||||||||||||||||
|
Subroutine
tt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 3 | return Template::Toolkit::Simple->new(); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 7 | my $default = { | |||||||||||||||||||||||||||
| 19 | data => undef, | ||||||||||||||||||||||||||||
| 20 | config => undef, | ||||||||||||||||||||||||||||
| 21 | output => undef, | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | encoding => 'utf8', | ||||||||||||||||||||||||||||
| 24 | include_path => undef, | ||||||||||||||||||||||||||||
| 25 | eval_perl => 0, | ||||||||||||||||||||||||||||
| 26 | start_tag => quotemeta('[' . '%'), | ||||||||||||||||||||||||||||
| 27 | end_tag => quotemeta('%' . ']'), | ||||||||||||||||||||||||||||
| 28 | tag_style => 'template', | ||||||||||||||||||||||||||||
| 29 | pre_chomp => 0, | ||||||||||||||||||||||||||||
| 30 | post_chomp => 0, | ||||||||||||||||||||||||||||
| 31 | trim => 0, | ||||||||||||||||||||||||||||
| 32 | interpolate => 0, | ||||||||||||||||||||||||||||
| 33 | anycase => 0, | ||||||||||||||||||||||||||||
| 34 | delimiter => ':', | ||||||||||||||||||||||||||||
| 35 | absolute => 0, | ||||||||||||||||||||||||||||
| 36 | relative => 0, | ||||||||||||||||||||||||||||
| 37 | strict => 0, | ||||||||||||||||||||||||||||
| 38 | default => undef, | ||||||||||||||||||||||||||||
| 39 | blocks => undef, | ||||||||||||||||||||||||||||
| 40 | auto_reset => 1, | ||||||||||||||||||||||||||||
| 41 | recursion => 0, | ||||||||||||||||||||||||||||
| 42 | pre_process => undef, | ||||||||||||||||||||||||||||
| 43 | post_process => undef, | ||||||||||||||||||||||||||||
| 44 | process_template => undef, | ||||||||||||||||||||||||||||
| 45 | error_template => undef, | ||||||||||||||||||||||||||||
| 46 | output_path => undef, | ||||||||||||||||||||||||||||
| 47 | debug => 0, | ||||||||||||||||||||||||||||
| 48 | cache_size => undef, | ||||||||||||||||||||||||||||
| 49 | compile_ext => undef, | ||||||||||||||||||||||||||||
| 50 | compile_dir => undef, | ||||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | my $abbreviations = { | |||||||||||||||||||||||||||
| 54 | data => 'd', | ||||||||||||||||||||||||||||
| 55 | include_path => 'path|i', | ||||||||||||||||||||||||||||
| 56 | output => 'o', | ||||||||||||||||||||||||||||
| 57 | config => 'c', | ||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 62 | 6 | ▶ | return bless shift || {%$default}, $class; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {%$default}
MC/DC100%1/1
shift() || {%$default}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 203 | ▶ | sub field { | ||||||||||||||||||||||||||
|
Subroutine
field: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 203 | my ($name, $value) = @_; | |||||||||||||||||||||||||||
| 67 | 12 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 12 | ▶ | $self->{$name} = @_ ? shift : $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 12 | return $self; | |||||||||||||||||||||||||||
| 71 | 203 | }; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 7 | for my $name (keys %$default) { | |||||||||||||||||||||||||||
| 76 | 217 | ▶ | next if $name =~ /^(data|config)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 203 | my $value = $default->{$name}; | |||||||||||||||||||||||||||
| 78 | 203 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 119 | ▶ | $value = 1 - $value if $value =~/^[01]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 119 | ▶ | $value = [] if $name eq 'include_path'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 203 | *{__PACKAGE__ . '::' . $name} = field($name, $value); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||
| 88 | 7 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 7 | *path = \&include_path; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 6 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=8 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | 6 | ▶ | my $template = shift | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | or die "render method requires a template name"; | ||||||||||||||||||||||||||||
| 96 | 6 | ▶ | if ($template =~ qr{//}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | my $path; | |||||||||||||||||||||||||||
| 98 | 1 | ($path, $template) = split '//', $template, 2; | |||||||||||||||||||||||||||
| 99 | 1 | $self->include_path($path); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 6 | ▶ | $self->data(shift(@_)) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 6 | ▶ | $self->output(shift(@_)) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 6 | ▶ | if ($self->{output}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | $self->process($template, $self->{data}, $self->{output}) | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | or $self->croak; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | return ''; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 6 | my $output = ''; | |||||||||||||||||||||||||||
| 111 | 6 | ▶ | $self->process($template, $self->{data}, \$output) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | or $self->croak; | ||||||||||||||||||||||||||||
| 113 | 6 | return Encode::encode_utf8($output); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | return <<'...' | ||||||||||||||||||||||||||||
| 118 | Usage: | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | tt-render --path=path/to/templates/ --data=data.yaml foo.tt2 | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | ... | ||||||||||||||||||||||||||||
| 123 | 0 | } | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub croak { | ||||||||||||||||||||||||||
|
Subroutine
croak: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 127 | 0 | require Carp; | |||||||||||||||||||||||||||
| 128 | 0 | my $error = $self->{tt}->error; | |||||||||||||||||||||||||||
| 129 | 0 | chomp $error; | |||||||||||||||||||||||||||
| 130 | 0 | Carp::croak($error . "\n"); | |||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 6 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | $self->{tt} = Template->new( | ||||||||||||||||||||||||||||
| 137 | ENCODING => $self->{encoding}, | ||||||||||||||||||||||||||||
| 138 | INCLUDE_PATH => $self->{include_path}, | ||||||||||||||||||||||||||||
| 139 | EVAL_PERL => $self->{eval_perl}, | ||||||||||||||||||||||||||||
| 140 | START_TAG => $self->{start_tag}, | ||||||||||||||||||||||||||||
| 141 | END_TAG => $self->{end_tag}, | ||||||||||||||||||||||||||||
| 142 | PRE_CHOMP => $self->{pre_chomp}, | ||||||||||||||||||||||||||||
| 143 | POST_CHOMP => $self->{post_chomp}, | ||||||||||||||||||||||||||||
| 144 | TRIM => $self->{trim}, | ||||||||||||||||||||||||||||
| 145 | INTERPOLATE => $self->{interpolate}, | ||||||||||||||||||||||||||||
| 146 | ANYCASE => $self->{anycase}, | ||||||||||||||||||||||||||||
| 147 | DELIMITER => $self->{delimiter}, | ||||||||||||||||||||||||||||
| 148 | ABSOLUTE => $self->{absolute}, | ||||||||||||||||||||||||||||
| 149 | STRICT => $self->{strict}, | ||||||||||||||||||||||||||||
| 150 | DEFAULT => $self->{default}, | ||||||||||||||||||||||||||||
| 151 | BLOCKS => $self->{blocks}, | ||||||||||||||||||||||||||||
| 152 | AUTO_RESET => $self->{auto_reset}, | ||||||||||||||||||||||||||||
| 153 | RECURSION => $self->{recursion}, | ||||||||||||||||||||||||||||
| 154 | PRE_PROCESS => $self->{pre_process}, | ||||||||||||||||||||||||||||
| 155 | POST_PROCESS => $self->{post_process}, | ||||||||||||||||||||||||||||
| 156 | PROCESS_TEMPLATE => $self->{process_template}, | ||||||||||||||||||||||||||||
| 157 | ERROR_TEMPLATE => $self->{error_template}, | ||||||||||||||||||||||||||||
| 158 | OUTPUT_PATH => $self->{output_path}, | ||||||||||||||||||||||||||||
| 159 | DEBUG => | ||||||||||||||||||||||||||||
| 160 | ($self->{debug} && DEBUG_ALL ^ DEBUG_CALLER ^ DEBUG_CONTEXT), | ||||||||||||||||||||||||||||
| 161 | CACHE_SIZE => $self->{cache_size}, | ||||||||||||||||||||||||||||
| 162 | COMPILE_EXT => $self->{compile_ext}, | ||||||||||||||||||||||||||||
| 163 | COMPILE_DIR => $self->{compile_dir}, | ||||||||||||||||||||||||||||
| 164 | 6 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'debug'} && 6111
MC/DC0%0/1
$self->{'debug'} && 6111
$self->{'debug'}
Truth table50%1/2
A
A= $self->{'debug'}
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 6 | return $self->{tt}->process(@_); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 6 | ▶ | sub data { | ||||||||||||||||||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 170 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 171 | 6 | $self->{data} = $self->_file_to_hash(@_); | |||||||||||||||||||||||||||
| 172 | 6 | return $self; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 177 | 0 | $self = { | |||||||||||||||||||||||||||
| 178 | %$self, | ||||||||||||||||||||||||||||
| 179 | $self->_file_to_hash(@_) | ||||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||
| 181 | 0 | return $self; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 6 | ▶ | sub _file_to_hash { | ||||||||||||||||||||||||||
|
Subroutine
_file_to_hash: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 185 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 186 | 6 | my $file_name = shift; | |||||||||||||||||||||||||||
| 187 | return | ||||||||||||||||||||||||||||
| 188 | 6 | ▶ | (ref($file_name) eq 'HASH') | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 189 | ? $file_name | ||||||||||||||||||||||||||||
| 190 | : ($file_name =~ /\.(?:yaml|yml)$/i) | ||||||||||||||||||||||||||||
| 191 | ? $self->_load_yaml($file_name) | ||||||||||||||||||||||||||||
| 192 | : ($file_name =~ /\.json$/i) | ||||||||||||||||||||||||||||
| 193 | ? $self->_load_json($file_name) | ||||||||||||||||||||||||||||
| 194 | : ($file_name =~ /\.xml$/i) | ||||||||||||||||||||||||||||
| 195 | ? $self->_load_xml($file_name) | ||||||||||||||||||||||||||||
| 196 | : die "Expected '$file_name' to end with .yaml, .json or .xml"; | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 4 | ▶ | sub _load_yaml { | ||||||||||||||||||||||||||
|
Subroutine
_load_yaml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 200 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 201 | 4 | YAML::XS::LoadFile(shift); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1 | ▶ | sub _load_json { | ||||||||||||||||||||||||||
|
Subroutine
_load_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 206 | 1 | require JSON::XS; | |||||||||||||||||||||||||||
| 207 | 1 | my $json = do { local $/; open my $json, '<', shift; <$json> }; | |||||||||||||||||||||||||||
| 208 | 1 | JSON::XS::decode_json($json); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 1 | ▶ | sub _load_xml { | ||||||||||||||||||||||||||
|
Subroutine
_load_xml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 212 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 213 | 1 | require XML::Simple; | |||||||||||||||||||||||||||
| 214 | 1 | XML::Simple::XMLin(shift); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 3 | ▶ | sub _run_command { | ||||||||||||||||||||||||||
|
Subroutine
_run_command: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 218 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 219 | 3 | my $self = $class->new($default); | |||||||||||||||||||||||||||
| 220 | 3 | local @ARGV = @_; | |||||||||||||||||||||||||||
| 221 | 3 | ▶ | my $template = pop or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | print STDERR $self->usage(); | |||||||||||||||||||||||||||
| 223 | 0 | return; | |||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||
| 225 | 8 | ▶ | my $setter = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 226 | 8 | my ($name, $value) = @_; | |||||||||||||||||||||||||||
| 227 | 8 | my $method = lc($name); | |||||||||||||||||||||||||||
| 228 | 8 | $method =~ s/-/_/g; | |||||||||||||||||||||||||||
| 229 | 8 | ▶ | $value = quotemeta($value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | if $method =~ /_tag$/; | ||||||||||||||||||||||||||||
| 231 | 8 | $self->$method($value); | |||||||||||||||||||||||||||
| 232 | 3 | }; | |||||||||||||||||||||||||||
| 233 | GetOptions( | ||||||||||||||||||||||||||||
| 234 | map { | ||||||||||||||||||||||||||||
| 235 | 3 | my $option = $_; | |||||||||||||||||||||||||||
| 236 | 93 | my $option2 = $option; | |||||||||||||||||||||||||||
| 237 | 93 | ▶ | $option .= "|$option2" if $option2 =~ s/_/-/g; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | $option .= "|$abbreviations->{$_}" | ||||||||||||||||||||||||||||
| 239 | 93 | ▶ | if defined $abbreviations->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 93 | ▶ | $option .= ((not defined $default->{$_} or $option =~/\-tag$/) ? '=s' : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $default->{$_} or $option =~ /\-tag$/
MC/DC100%2/2
not defined $default->{$_} or $option =~ /\-tag$/
not defined $default->{$_}
$option =~ /\-tag$/
Truth table100%3/3
not(A) or B
A= not defined $default->{$_}B= $option =~ /\-tag$/
| |||||||||||||||||||||||||||||
| 241 | 93 | ($option, $setter); | |||||||||||||||||||||||||||
| 242 | } keys %$default | ||||||||||||||||||||||||||||
| 243 | ); | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 3 | print STDOUT $self->render($template); | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 7 | 1; | |||||||||||||||||||||||||||