blib/lib/Statocles/Template.pm

filter: Statementstmt 91.4% 118 / 129 Branchbran 82.5% 33 / 40 Conditioncond 75.0% 12 / 16 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 95.4% 21 / 22 Podpod 100% 4 / 4 totaltotal 87.7% 193 / 220 CC 30 SCAR 14.9
CC
30
Cov
88%
merge_state
12.0
include
8.6
__ANON__
5.0
CRAP 4.5
« blib/lib/Statocles/Command/build.pm ↑ Summary blib/lib/Statocles/Theme.pm »
1 package Statocles::Template;
2 63 our $VERSION = '0.099';
3 # ABSTRACT: A template object to pass around
4  
5 63 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 63 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 63 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 63 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =attr content
11 #pod
12 #pod The main template string. This will be generated by reading the file C<path> by
13 #pod default.
14 #pod
15 #pod =cut
16  
17 has content => (
18 is => 'ro',
19 isa => Str,
20 lazy => 1,
21 18 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 18 my ( $self ) = @_;
23 18 return Path::Tiny->new( $self->path )->slurp;
24 },
25 63 );
26  
27 #pod =attr path
28 #pod
29 #pod The path to the file for this template. Optional.
30 #pod
31 #pod =cut
32  
33 has path => (
34 is => 'ro',
35 isa => Str,
36 455 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 455 return "$_[0]"; # Force stringify in case of Path::Tiny objects
38 },
39 63 );
40  
41 #pod =attr theme
42 #pod
43 #pod The theme this template was created from. Used for includes and other
44 #pod information.
45 #pod
46 #pod =cut
47  
48 63 has theme => (
49 is => 'ro',
50 isa => ThemeType,
51 coerce => ThemeType->coercion,
52 );
53  
54 #pod =attr include_paths
55 #pod
56 #pod An array of paths to look for includes. Will be
57 #pod used in addition to the L<include_paths from the
58 #pod Theme|Statocles::Theme/include_paths>.
59 #pod
60 #pod =cut
61  
62 has include_paths => (
63 is => 'ro',
64 isa => ArrayRef[Path],
65 452 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 455 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
67 455 my ( $thing ) = @_;
68 455 ▶ if ( ref $thing eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq "ARRAY") 455 0 455
69 455 return [ map { Path->coercion->( $_ ) } @$thing ];
70 }
71 0 return [ Path->coercion->( $thing ) ];
72 },
73 63 );
74  
75 has _template => (
76 is => 'ro',
77 isa => InstanceOf['Mojo::Template'],
78 lazy => 1,
79 183 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
80 183 my ( $self ) = @_;
81 183 my %config;
82 183 ▶ if ( $self->theme ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->theme) 145 38 183
83 3 %config = map { $_ => $self->theme->$_ }
84 145 grep { $self->theme->$_ }
85 qw(
86 tag_start tag_end
87 line_start trim_mark
88 replace_mark expression_mark
89 escape_mark comment_mark
90 capture_start capture_end
91 );
92 }
93 183 my $t = Mojo::Template->new(
94 name => $self->path,
95 %config,
96 );
97 183 $t->parse( $self->content );
98 183 return $t;
99 },
100 63 );
101  
102 #pod =method BUILDARGS
103 #pod
104 #pod Set the default path to something useful for in-memory templates.
105 #pod
106 #pod =cut
107  
108 455 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
109 455 my ( $orig, $self, @args ) = @_;
110 455 my $args = $self->$orig( @args );
111 455 ▶ if ( !$args->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"path"}) 282 173 455
112 282 my ( $i, $caller_class ) = ( 0, (caller 0)[0] );
113 282 ▶ while ( $caller_class->isa( 'Statocles::Template' )
Branch: 1 branch
BranchTrueFalseTotal
if ($caller_class->isa("Statocles::Template") or $caller_class->isa("Sub::Quote") or $caller_class->isa("Method::Generate::Constructor")) 849 282 1131
Condition66%4/6
$caller_class->isa("Statocles::Template") or $caller_class->isa("Sub::Quote") or $caller_class->isa("Method::Generate::Constructor")
l!l&&r!l&&!r
8490282
$caller_class->isa("Statocles::Template") or $caller_class->isa("Sub::Quote")
l!l&&r!l&&!r
8490282
MC/DC33%1/3
$caller_class->isa("Statocles::Template") or $caller_class->isa("Sub::Quote") or $caller_class->isa("Method::Generate::Constructor")
$caller_class->isa("Statocles::Template") $caller_class->isa("Sub::Quote") $caller_class->isa("Method::Generate::Constructor")
Truth table50%2/4
A or B or C
A= $caller_class->isa("Statocles::Template")B= $caller_class->isa("Sub::Quote")C= $caller_class->isa("Method::Generate::Constructor")
ABCresult
1XX1
01X1
0011
0000
114 || $caller_class->isa( 'Sub::Quote' )
115 || $caller_class->isa( 'Method::Generate::Constructor' )
116 ) {
117 #; say "Class: $caller_class";
118 849 $i++;
119 849 $caller_class = (caller $i)[0];
120 }
121 #; say "Class: $caller_class";
122 282 $args->{path} = join " line ", (caller($i))[1,2];
123 }
124 455 return $args;
125 63 };
126  
127 #pod =method render
128 #pod
129 #pod my $html = $tmpl->render( %args )
130 #pod
131 #pod Render this template, passing in %args. Each key in %args will be available as
132 #pod a scalar in the template.
133 #pod
134 #pod =cut
135  
136 296 ▶sub render {
Subroutine render: called CC=5 SCAR=16.1
Pod documented
137 296 my ( $self, %args ) = @_;
138 296 my $t = $self->_template;
139 296 $t->prepend( $self->_prelude( '_tmpl', keys %args ) );
140  
141 296 my $content;
142 {
143 # Add the helper subs, like Mojolicious::Plugin::EPRenderer does
144 63 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
145 63 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
146  
147 # Add theme helpers first, to ensure default helpers do not get
148 # overridden.
149 296 ▶ if ( $self->theme ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->theme) 258 38 296
150 258 my %theme_helpers = %{ $self->theme->_helpers };
151 258 for my $helper ( keys %theme_helpers ) {
152 22 ▶ *{"@{[$t->namespace]}::$helper"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 8 $theme_helpers{ $helper }->( \%args, @_ );
154 22 };
155 }
156 }
157  
158 # Add default helpers
159 296 ▶ local *{"@{[$t->namespace]}::include"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
160 122 ▶ if ( $_[0] eq '-raw' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "-raw") 0 122 122
161 0 return $self->include( @_ );
162 }
163 122 my ( $name, %extra_args ) = @_;
164 122 my $inner_tmpl = $self->include( $name );
165 120 ▶ return $inner_tmpl->render( %args, %extra_args ) || '';
Condition100%2/2
$inner_tmpl->render(%args, %extra_args) || ''
l!l
9111
MC/DC100%1/1
$inner_tmpl->render(%args, %extra_args) || ''
$inner_tmpl->render(%args, %extra_args)
Truth table100%2/2
A
A= $inner_tmpl->render(%args, %extra_args)
Aresult
11
00
166 296 };
167  
168 296 ▶ local *{"@{[$t->namespace]}::markdown"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
169 7 my ( $text, %extra_args ) = @_;
170 die "Cannot use markdown helper: No site object given to template"
171 7 ▶ unless exists $args{site};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"site"} 1 6 7
172 6 ▶ return $args{site}->markdown->markdown( ref $text eq 'CODE' ? $text->() : $text );
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'CODE' ? : 1 5 6
173 296 };
174  
175 296 ▶ local *{"@{[$t->namespace]}::content"} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
176 105 my ( $section, $content ) = @_;
177 105 ▶ if ( $content ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content) { } 14 91 105
elsif ($section) { } 41 50 91
178 14 ▶ if ( ref $content eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "CODE") 11 3 14
179 11 $content = $content->();
180 }
181 14 $args{page}->_content_sections->{ $section } = $content;
182 14 return;
183 }
184 elsif ( $section ) {
185 41 ▶ return $args{page}->_content_sections->{ $section } // '';
Condition100%2/2
$args{"page"}->_content_sections->{$section} // ""
l!l
2021
MC/DC100%1/1
$args{"page"}->_content_sections->{$section} // ""
$args{"page"}->_content_sections->{$section}
Truth table100%2/2
A
A= $args{"page"}->_content_sections->{$section}
Aresult
11
00
186 }
187 50 return $args{content};
188 296 };
189  
190 296 $content = eval { $t->process( \%args ) };
191 }
192  
193 296 ▶ if ( blessed $content && $content->isa( 'Mojo::Exception' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($content) and $content->isa("Mojo::Exception")) 13 283 296
Condition66%2/3
builtin::blessed($content) and $content->isa("Mojo::Exception")
!ll&&!rl&&r
283013
MC/DC50%1/2
builtin::blessed($content) and $content->isa("Mojo::Exception")
builtin::blessed($content) $content->isa("Mojo::Exception")
Truth table66%2/3
A and B
A= builtin::blessed($content)B= $content->isa("Mojo::Exception")
ABresult
0X0
100
111
194 13 die "Error in template: " . $content;
195 }
196 283 return $content;
197 }
198  
199 # Build the Perl string that will unpack the passed-in args
200 # This is how Mojolicious::Plugin::EPRenderer does it, but I'm probably
201 # doing something wrong here...
202 296 ▶sub _prelude {
Subroutine _prelude: called CC=1 SCAR=0.0
203 296 my ( $self, @vars ) = @_;
204 return join " ",
205 'use strict; use warnings; no warnings "ambiguous";',
206 'my $vars = shift;',
207 296 map( { "my \$$_ = \$vars->{'$_'};" } @vars ),
208 ;
209 }
210  
211 #pod =method include
212 #pod
213 #pod my $tmpl = $tmpl->include( $path );
214 #pod my $tmpl = $tmpl->include( @path_parts );
215 #pod
216 #pod Get the desired L<template|Statocles::Template> to include based on the given
217 #pod C<path> or C<path_parts>. Looks through all the L<include_paths|/include_paths>.
218 #pod If nothing is found, looks in the L<theme includes|Statocles::Theme/include>.
219 #pod
220 #pod =cut
221  
222 122 ▶sub include {
Subroutine include: called CC=8 SCAR=21.5
Pod documented
223 122 my ( $self, @path ) = @_;
224 122 my $render = 1;
225 122 ▶ if ( $path[0] eq '-raw' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path[0] eq "-raw") 0 122 122
226 # Allow raw files to not be passed through the template renderer
227 # This override flag will always exist, but in the future we may
228 # add better detection to possible file types to process
229 0 $render = 0;
230 0 shift @path;
231 }
232 122 my $path = Path::Tiny->new( @path );
233  
234 122 my @search_paths = @{ $self->include_paths };
235 122 for my $search_path ( @search_paths ) {
236 3 ▶ if ( $search_path->child( $path )->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_path->child($path)->is_file) 1 2 3
237 1 ▶ if ( $render ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render) 1 0 1
238 1 return $self->theme->build_template(
239 $path, $search_path->child( $path )->slurp_utf8,
240 );
241 }
242 0 return $search_path->child( $path )->slurp_utf8;
243 }
244 }
245  
246 121 my $include = eval {
247 121 ▶ $self->theme->include( !$render ? ( '-raw', @path ) : @path );
Branch: 1 branch
BranchTrueFalseTotal
!$render ? : 0 121 121
248 };
249 121 ▶ if ( $@ && $@ =~ /^Can not find include/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and $@ =~ /^Can not find include/) 2 119 121
Condition66%2/3
$@ and $@ =~ /^Can not find include/
!ll&&!rl&&r
11902
MC/DC50%1/2
$@ and $@ =~ /^Can not find include/
$@ $@ =~ /^Can not find include/
Truth table66%2/3
A and B
A= $@B= $@ =~ /^Can not find include/
ABresult
0X0
100
111
250 die qq{Can not find include "$path" in include directories: }
251 2 . join( ", ", map { sprintf q{"%s"}, $_ } @search_paths, @{ $self->theme->include_paths }, $self->theme->path )
252 . "\n";
253 }
254  
255 119 return $include;
256 }
257  
258 #pod =method merge_state
259 #pod
260 #pod $tmpl->merge_state( $state );
261 #pod
262 #pod Merge the given C<$state> hash reference into the existing. Keys
263 #pod in C<$state> override those in L<the state attribute|/state>.
264 #pod
265 #pod =cut
266  
267 0 ▶sub merge_state {
Subroutine merge_state: not called CC=3 SCAR=24.8
Pod documented
268 0 my ( $self, $new_state ) = @_;
269 0 for my $key ( keys %$new_state ) {
270 0 my $value = $new_state->{ $key };
271 0 ▶ $value = dclone $value if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
272 0 $self->state->{ $key } = $value;
273 }
274 0 return;
275 }
276  
277 #pod =method coercion
278 #pod
279 #pod my $coerce = Statocles::Template->coercion;
280 #pod
281 #pod has template => (
282 #pod is => 'ro',
283 #pod isa => InstanceOf['Statocles::Template'],
284 #pod coerce => Statocles::Template->coercion,
285 #pod );
286 #pod
287 #pod A class method to returns a coercion sub to convert strings into template
288 #pod objects.
289 #pod
290 #pod =cut
291  
292 60 ▶sub coercion {
Subroutine coercion: called CC=1 SCAR=0.0
Pod documented
293 60 my ( $class ) = @_;
294 337 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
295 337 ▶ die "Template is undef" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 336 337
296 336 ▶ return !ref $_[0]
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 3 333 336
297 ? Statocles::Template->new( content => $_[0] )
298 : $_[0]
299 ;
300 60 };
301 }
302  
303 63 1;
304  
305 __END__
« blib/lib/Statocles/Command/build.pm ↑ Summary blib/lib/Statocles/Theme.pm »