| 1 | 3 | ▶ | use v5.40; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 3 | ▶ | use experimental 'class'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 1 | ▶ | class Minima::View::HTML :isa(Minima::View); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use Path::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use Template; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use Template::Constants qw/ :debug /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 3 | field $app :param; | |||||||||||
| 13 | 3 | field @include; | |||||||||||
| 14 | 3 | field $template; | |||||||||||
| 15 | 3 | field @before_template; | |||||||||||
| 16 | 3 | field @after_template; | |||||||||||
| 17 | 3 | field $default_data = {}; | |||||||||||
| 18 | |||||||||||||
| 19 | field %settings = ( | ||||||||||||
| 20 | block_indexing => 1, # block robots with a <meta> | ||||||||||||
| 21 | name_as_class => 1, # include template name in @classes | ||||||||||||
| 22 | theme_color => '', # hex color for the <meta theme-color> | ||||||||||||
| 23 | 3 | ); | |||||||||||
| 24 | |||||||||||||
| 25 | field %content = ( | ||||||||||||
| 26 | title => undef, | ||||||||||||
| 27 | description => undef, | ||||||||||||
| 28 | header_scripts => [], # scripts to be loaded in <head> | ||||||||||||
| 29 | header_css => [], # CSS to be loaded in <head> | ||||||||||||
| 30 | body_open => [], # used right after the opening of <body> | ||||||||||||
| 31 | body_close => [], # used right before the closing of </body> | ||||||||||||
| 32 | scripts => [], # scripts to be embedded directly in <body> | ||||||||||||
| 33 | classes => [], # classes added to <main> or <body> | ||||||||||||
| 34 | ); | ||||||||||||
| 35 | |||||||||||||
| 36 | 11 | ▶ | ADJUST { | ||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||
| 37 | 11 | my $config = $app->config; | |||||||||||
| 38 | |||||||||||||
| 39 | 11 | ▶ | $content{title} = $config->{default_title} // ''; | ||||||||||
|
Condition100%2/2
$config->{"default_title"} // ""
MC/DC100%1/1
$config->{"default_title"} // ""
$config->{"default_title"}
Truth table100%2/2
A
A= $config->{"default_title"}
| |||||||||||||
| 40 | |||||||||||||
| 41 | 11 | ▶ | $settings{block_indexing} = $config->{block_indexing} // 1; | ||||||||||
|
Condition100%2/2
$config->{"block_indexing"} // 1
MC/DC100%1/1
$config->{"block_indexing"} // 1
$config->{"block_indexing"}
Truth table100%2/2
A
A= $config->{"block_indexing"}
| |||||||||||||
| 42 | 11 | ▶ | $settings{name_as_class} = $config->{name_as_class} // 1; | ||||||||||
|
Condition100%2/2
$config->{"name_as_class"} // 1
MC/DC100%1/1
$config->{"name_as_class"} // 1
$config->{"name_as_class"}
Truth table100%2/2
A
A= $config->{"name_as_class"}
| |||||||||||||
| 43 | 11 | ▶ | $settings{theme_color} = $config->{theme_color} // ''; | ||||||||||
|
Condition100%2/2
$config->{"theme_color"} // ""
MC/DC100%1/1
$config->{"theme_color"} // ""
$config->{"theme_color"}
Truth table100%2/2
A
A= $config->{"theme_color"}
| |||||||||||||
| 44 | |||||||||||||
| 45 | 11 | ▶ | if (exists $config->{templates_dir}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 4 | ▶ | if (ref $config->{templates_dir} eq ref []) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | $self->add_directory($_) | ||||||||||||
| 48 | 3 | for reverse @{ $config->{templates_dir} }; | |||||||||||
| 49 | } | ||||||||||||
| 50 | } else { | ||||||||||||
| 51 | 7 | $self->add_directory('js'); | |||||||||||
| 52 | 7 | $self->add_directory('templates'); | |||||||||||
| 53 | } | ||||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 23 | ▶ | method add_directory ($d) { unshift @include, $app->path($d) } | ||||||||||
|
Subroutine
add_directory: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 57 | 1 | ▶ | method clear_directories { @include = () } | ||||||||||
|
Subroutine
clear_directories: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 58 | 16 | ▶ | method set_template ($t) { $template = $self->_ext($t) } | ||||||||||
|
Subroutine
set_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 59 | 2 | ▶ | method add_before_template ($p) { push @before_template, $self->_ext($p) } | ||||||||||
|
Subroutine
add_before_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 60 | 2 | ▶ | method add_after_template ($p) { push @after_template, $self->_ext($p) } | ||||||||||
|
Subroutine
add_after_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 61 | 1 | ▶ | method set_default_data ($d) { $default_data = $d } | ||||||||||
|
Subroutine
set_default_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 62 | |||||||||||||
| 63 | 1 | ▶ | method set_block_indexing ($n = 1) { $settings{block_indexing} = $n } | ||||||||||
|
Subroutine
set_block_indexing: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 64 | 1 | ▶ | method set_name_as_class ($n = 1) { $settings{name_as_class} = $n } | ||||||||||
|
Subroutine
set_name_as_class: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 65 | 1 | ▶ | method set_theme_color ($c) { $settings{theme_color} = $c } | ||||||||||
|
Subroutine
set_theme_color: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 66 | |||||||||||||
| 67 | 6 | ▶ | method set_title ($t, $d = undef) | ||||||||||
|
Subroutine
set_title: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 68 | 6 | { | |||||||||||
| 69 | 6 | $content{title} = $t; | |||||||||||
| 70 | 6 | $content{description} = $d; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | 2 | ▶ | method set_compound_title ($t, $d = undef) | ||||||||||
|
Subroutine
set_compound_title: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 74 | 2 | { | |||||||||||
| 75 | $self->set_title( | ||||||||||||
| 76 | 2 | ▶ | ( $content{title} ? "$content{title} • $t" : $t ), | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | $d | ||||||||||||
| 78 | ); | ||||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 1 | ▶ | method set_description ($d) { $content{description} = $d } | ||||||||||
|
Subroutine
set_description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 82 | 2 | ▶ | method add_header_script ($s) { push @{$content{header_scripts}}, $s } | ||||||||||
|
Subroutine
add_header_script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 83 | 2 | ▶ | method add_header_css ($c) { push @{$content{header_css}}, $c } | ||||||||||
|
Subroutine
add_header_css: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 84 | 2 | ▶ | method add_script ($s) { push @{$content{scripts}}, $s } | ||||||||||
|
Subroutine
add_script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 85 | 2 | ▶ | method add_class ($c) { push @{$content{classes}}, $c } | ||||||||||
|
Subroutine
add_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 86 | |||||||||||||
| 87 | 2 | ▶ | method add_body_open ($p) | ||||||||||
|
Subroutine
add_body_open: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 88 | { | ||||||||||||
| 89 | 2 | push @{$content{body_open}}, $self->_ext($p) | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | 1 | ▶ | method add_body_close ($p) | ||||||||||
|
Subroutine
add_body_close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 93 | { | ||||||||||||
| 94 | 1 | push @{$content{body_close}}, $self->_ext($p) | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | 1 | ▶ | method prepare_response ($response) | ||||||||||
|
Subroutine
prepare_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 98 | { | ||||||||||||
| 99 | 1 | $response->content_type('text/html; charset=utf-8'); | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 37 | ▶ | method render ($data = {}) | ||||||||||
|
Subroutine
render: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||
| 103 | 37 | { | |||||||||||
| 104 | 37 | ▶ | croak "No template set." unless $template; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | |||||||||||||
| 106 | # Merge default data | ||||||||||||
| 107 | 36 | $data = { %$default_data, %$data }; | |||||||||||
| 108 | |||||||||||||
| 109 | # Build vars to send to template | ||||||||||||
| 110 | 36 | my %vars = ( %content, settings => \%settings ); | |||||||||||
| 111 | 36 | $data->{view} = \%vars; | |||||||||||
| 112 | |||||||||||||
| 113 | # Format CSS classes | ||||||||||||
| 114 | 36 | my @classes = @{ $content{classes} }; | |||||||||||
| 115 | 36 | ▶ | if ($settings{name_as_class}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | 34 | my $clean_name = $template; | |||||||||||
| 117 | 34 | $clean_name =~ s/\.\w+$//; | |||||||||||
| 118 | 34 | $clean_name =~ tr/./-/; | |||||||||||
| 119 | 34 | push @classes, $clean_name; | |||||||||||
| 120 | } | ||||||||||||
| 121 | 36 | $vars{classes} = "@classes"; | |||||||||||
| 122 | |||||||||||||
| 123 | # If any var is undef, replace with empty string | ||||||||||||
| 124 | 36 | ▶ | $vars{$_} //= '' for keys %vars; | ||||||||||
|
Condition100%2/2
$vars{$_} //= ""
MC/DC100%1/1
$vars{$_} //= ""
$vars{$_}
Truth table100%2/2
A
A= $vars{$_}
| |||||||||||||
| 125 | |||||||||||||
| 126 | # Setup Template Toolkit: | ||||||||||||
| 127 | # Create a default and overwrite with user configuration. | ||||||||||||
| 128 | 36 | my %tt_default = ( | |||||||||||
| 129 | INCLUDE_PATH => \@include, | ||||||||||||
| 130 | OUTLINE_TAG => '%%', | ||||||||||||
| 131 | ANYCASE => 1, | ||||||||||||
| 132 | ENCODING => 'utf8', | ||||||||||||
| 133 | ); | ||||||||||||
| 134 | 36 | ▶ | my $tt_app_config = $app->config->{tt} // {}; | ||||||||||
|
Condition100%2/2
$app->config->{"tt"} // {}
MC/DC100%1/1
$app->config->{"tt"} // {}
$app->config->{"tt"}
Truth table100%2/2
A
A= $app->config->{"tt"}
| |||||||||||||
| 135 | 36 | my %tt_config = ( %tt_default, %$tt_app_config ); | |||||||||||
| 136 | 36 | my $tt = Template->new(\%tt_config); | |||||||||||
| 137 | |||||||||||||
| 138 | # Render | ||||||||||||
| 139 | 36 | my ( $body, $r ); | |||||||||||
| 140 | |||||||||||||
| 141 | 36 | for my $t (@before_template, $template, @after_template) { | |||||||||||
| 142 | 40 | $r = $tt->process($t, $data, \$body); | |||||||||||
| 143 | 40 | ▶ | croak "Failed to load template `$t` (include path: `@include`): ", | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | $tt->error, "\n" unless $r; | ||||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 31 | utf8::encode($body); | |||||||||||
| 148 | 31 | $body; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 23 | ▶ | method _ext ($file) | ||||||||||
|
Subroutine
_ext: called CC=3 SCAR=11.0
| |||||||||||||
| 152 | { | ||||||||||||
| 153 | 23 | ▶ | my $ext = $app->config->{template_ext} // 'ht'; | ||||||||||
|
Condition100%2/2
$app->config->{"template_ext"} // "ht"
MC/DC100%1/1
$app->config->{"template_ext"} // "ht"
$app->config->{"template_ext"}
Truth table100%2/2
A
A= $app->config->{"template_ext"}
| |||||||||||||
| 154 | 23 | ▶ | $file = "$file.$ext" unless $file =~ /\.\w+$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | 23 | $file; | |||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | __END__ | ||||||||||||