| 1 | package Kelp::Template; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 41 | ▶ | use Kelp::Base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 41 | ▶ | use Template::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 41 | ▶ | use Path::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 41 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 0 | ▶ | attr paths => sub { [] }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 9 | 41 | attr encoding => 'UTF-8'; | |||||||||||||||||||
| 10 | 19 | ▶ | attr tt => sub { Template::Tiny->new }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub process | ||||||||||||||||||||
| 13 | 97 | ▶ | { | ||||||||||||||||||
|
Subroutine
process: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 14 | 97 | my ($self, $template, $vars) = @_; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 97 | my $ref = ref $template; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # A GLOB or an IO object will be read and returned as a SCALAR template | ||||||||||||||||||||
| 19 | # No reference means a file name | ||||||||||||||||||||
| 20 | 97 | ▶ | if (!$ref) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 21 | 76 | $template = $self->_read_file($self->find_template($template)); | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | ▶ | elsif ($ref =~ /^IO/ || $ref eq 'GLOB') { | |||||||||||||||||||
|
Condition100%3/3
$ref =~ /^IO/ or $ref eq "GLOB"
MC/DC100%2/2
$ref =~ /^IO/ or $ref eq "GLOB"
$ref =~ /^IO/
$ref eq "GLOB"
Truth table100%3/3
A or B
A= $ref =~ /^IO/B= $ref eq "GLOB"
| |||||||||||||||||||||
| 24 | 5 | $template = $self->_read_file($template); | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | elsif ($ref ne 'SCALAR') { | ||||||||||||||||||||
| 27 | 0 | croak "Template reference must be SCALAR, GLOB or an IO object"; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 72 | my $output; | |||||||||||||||||||
| 31 | 72 | $self->tt->process($template, $vars, \$output); | |||||||||||||||||||
| 32 | 72 | return $output; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | sub find_template | ||||||||||||||||||||
| 36 | 76 | ▶ | { | ||||||||||||||||||
|
Subroutine
find_template: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 37 | 76 | my ($self, $name) = @_; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 76 | my $file; | |||||||||||||||||||
| 40 | 76 | for my $p ('.', @{$self->paths}) { | |||||||||||||||||||
| 41 | 150 | $file = "$p/$name"; | |||||||||||||||||||
| 42 | 150 | ▶ | return $file if -e $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 25 | return undef; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub _read_file | ||||||||||||||||||||
| 49 | 81 | ▶ | { | ||||||||||||||||||
|
Subroutine
_read_file: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 50 | 81 | my ($self, $file) = @_; | |||||||||||||||||||
| 51 | 81 | my $text; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 81 | ▶ | if (ref $file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # read the entire file | ||||||||||||||||||||
| 56 | 5 | local $/ = undef; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # make sure to properly rewind the handle after we read from it | ||||||||||||||||||||
| 59 | 5 | my $pos = tell $file; | |||||||||||||||||||
| 60 | 5 | $text = readline $file; | |||||||||||||||||||
| 61 | 5 | seek $file, $pos, 0; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||
| 64 | 76 | $text = path($file)->slurp( | |||||||||||||||||||
| 65 | {binmode => ':encoding(' . $self->encoding . ')'} | ||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 56 | return \$text; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 41 | 1; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | __END__ | ||||||||||||||||||||