blib/lib/Statocles/Theme.pm

filter: Statementstmt 91.5% 87 / 95 Branchbran 76.9% 20 / 26 Conditioncond 66.6% 12 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 95.0% 19 / 20 Podpod 100% 7 / 7 totaltotal 84.6% 149 / 176 CC 21 SCAR 13.4
CC
21
Cov
86%
include
6.6
__ANON__
6.1
read
5.8
CRAP 3.8
« blib/lib/Statocles/Template.pm ↑ Summary blib/lib/Statocles/App.pm »
1 package Statocles::Theme;
2 49 our $VERSION = '0.099';
3 # ABSTRACT: Templates, headers, footers, and navigation
4  
5 51 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 51 ▶use File::Share qw( dist_dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 51 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 ▶use Statocles::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 with 'Statocles::App';
10  
11 #pod =attr url_root
12 #pod
13 #pod The root URL for this application. Defaults to C</theme>.
14 #pod
15 #pod =cut
16  
17 93 ▶has '+url_root' => ( default => sub { '/theme' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 #pod =attr path
20 #pod
21 #pod The path to the theme. Can be a string that starts with C<::> to pick a default theme.
22 #pod Included bundled default themes are:
23 #pod
24 #pod =over
25 #pod
26 #pod =item default
27 #pod
28 #pod A clean default theme. Good for starting your own theme.
29 #pod
30 #pod =item bootstrap
31 #pod
32 #pod A theme using Bootstrap 3.
33 #pod
34 #pod =back
35 #pod
36 #pod =cut
37  
38 has path => (
39 is => 'ro',
40 isa => Path,
41 0 ▶ default => sub { Path->coercion->( 'theme' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 49 coerce => Path->coercion,
43 );
44  
45 #pod =attr include_paths
46 #pod
47 #pod An array of paths to look for includes. The L</path> is added at the end
48 #pod of this list.
49 #pod
50 #pod =cut
51  
52 has include_paths => (
53 is => 'ro',
54 isa => ArrayRef[Path],
55 92 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 93 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
57 93 my ( $thing ) = @_;
58 93 ▶ if ( ref $thing eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq "ARRAY") 93 0 93
59 93 return [ map { Path->coercion->( $_ ) } @$thing ];
60 }
61 0 return [ Path->coercion->( $thing ) ];
62 },
63 49 );
64  
65 #pod =attr tag_start
66 #pod
67 #pod String that indicates the start of a template tag. Defaults to
68 #pod C<< <% >>.
69 #pod
70 #pod =attr tag_end
71 #pod
72 #pod String that indicates the end of a template tag. Defaults to C<< %> >>.
73 #pod
74 #pod =attr line_start
75 #pod
76 #pod String that indicates the start of a line of template code.
77 #pod Defaults to C<%>.
78 #pod
79 #pod =attr expression_mark
80 #pod
81 #pod String that indicates an expression to be evaluated and inserted into
82 #pod the template. Defaults to C<=>.
83 #pod
84 #pod =attr escape_mark
85 #pod
86 #pod String that escapes the template directives. Defaults to C<%>.
87 #pod
88 #pod =attr comment_mark
89 #pod
90 #pod String that indicates a comment. Defaults to C<#>.
91 #pod
92 #pod =attr capture_start
93 #pod
94 #pod Keyword that starts capturing string. Defaults to C<begin>.
95 #pod
96 #pod =attr capture_end
97 #pod
98 #pod Keyword that ends capturing string. Defaults to C<end>.
99 #pod
100 #pod =attr trim_mark
101 #pod
102 #pod String that indicates that whitespace should be trimmed. Defaults to
103 #pod C<=>.
104 #pod
105 #pod =cut
106  
107 49 has [qw(
108 tag_start tag_end
109 line_start trim_mark
110 replace_mark expression_mark
111 escape_mark comment_mark
112 capture_start capture_end
113 )] => (
114 is => 'ro',
115 isa => Maybe[Str],
116 );
117  
118 #pod =attr _templates
119 #pod
120 #pod The cached template objects for this theme.
121 #pod
122 #pod =cut
123  
124 has '+_templates' => (
125 is => 'ro',
126 isa => HashRef[InstanceOf['Statocles::Template']],
127 30 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
128 49 lazy => 1, # Must be lazy or the clearer won't re-init the default
129 clearer => '_clear_templates',
130 );
131  
132 #pod =attr _includes
133 #pod
134 #pod The cached template objects for the includes.
135 #pod
136 #pod =cut
137  
138 has _includes => (
139 is => 'ro',
140 isa => HashRef[InstanceOf['Statocles::Template']],
141 16 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
142 49 lazy => 1, # Must be lazy or the clearer won't re-init the default
143 clearer => '_clear_includes',
144 );
145  
146 # The helpers added to this theme.
147 has _helpers => (
148 is => 'ro',
149 isa => HashRef[CodeRef],
150 93 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
151 49 init_arg => 'helpers', # Allow initialization via config file
152 );
153  
154 # If true, add the files in the path
155 has _add_path_files => (
156 is => 'ro',
157 55 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
158 49 );
159  
160 #pod =method BUILDARGS
161 #pod
162 #pod Handle the path :: share theme.
163 #pod
164 #pod =cut
165  
166 93 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
167 93 my ( $orig, $self, @args ) = @_;
168 93 my $args = $self->$orig( @args );
169 93 ▶ if ( $args->{store} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"store"}) 61 32 93
170 61 $args->{path} = delete $args->{store};
171 }
172 93 ▶ if ( $args->{path} && !ref $args->{path} && $args->{path} =~ /^::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"path"} and not ref $args->{"path"} and $args->{"path"} =~ /^::/) 38 55 93
Condition83%5/6
$args->{"path"} and not ref $args->{"path"} and $args->{"path"} =~ /^::/
!ll&&!rl&&r
411438
$args->{"path"} and not ref $args->{"path"}
!ll&&!rl&&r
04152
MC/DC66%2/3
$args->{"path"} and not ref $args->{"path"} and $args->{"path"} =~ /^::/
$args->{"path"} not ref $args->{"path"} $args->{"path"} =~ /^::/
Truth table75%3/4
A and not(B) and C
A= $args->{"path"}B= not ref $args->{"path"}C= $args->{"path"} =~ /^::/
ABCresult
0XX0
10X0
1100
1111
173 38 my $name = substr $args->{path}, 2;
174 38 $args->{path} = Path::Tiny->new( dist_dir( 'Statocles' ) )->child( 'theme', $name );
175 38 $args->{_add_path_files} = 1;
176 }
177 93 ▶ if ( $args->{include_stores} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"include_stores"}) 0 93 93
178 0 $args->{include_paths} = delete $args->{include_stores};
179 }
180 93 return $args;
181 49 };
182  
183 #pod =method read
184 #pod
185 #pod my $tmpl = $theme->read( $path )
186 #pod
187 #pod Read the template for the given C<path> and create the
188 #pod L<template|Statocles::Template> object.
189 #pod
190 #pod =cut
191  
192 68 ▶sub read {
Subroutine read: called CC=5 SCAR=17.6
Pod documented
193 68 my ( $self, $path ) = @_;
194 68 $path .= '.ep';
195  
196 68 my $content = eval { $self->path->child( $path )->slurp_utf8; };
197 68 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 67 68
198 1 ▶ if ( blessed $@ && $@->isa( 'Path::Tiny::Error' ) && $@->{op} =~ /^open/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa('Path::Tiny::Error') and $@->{'op'} =~ /^open/) { } 1 0 1
Condition33%2/6
builtin::blessed($@) and $@->isa("Path::Tiny::Error") and $@->{"op"} =~ /^open/
!ll&&!rl&&r
001
builtin::blessed($@) and $@->isa("Path::Tiny::Error")
!ll&&!rl&&r
001
MC/DC0%0/3
builtin::blessed($@) and $@->isa("Path::Tiny::Error") and $@->{"op"} =~ /^open/
builtin::blessed($@) $@->isa("Path::Tiny::Error") $@->{"op"} =~ /^open/
Truth table25%1/4
A and B and C
A= builtin::blessed($@)B= $@->isa("Path::Tiny::Error")C= $@->{"op"} =~ /^open/
ABCresult
0XX0
10X0
1100
1111
199 1 die sprintf 'ERROR: Template "%s" does not exist in theme directory "%s"' . "\n",
200 $path, $self->path;
201 }
202 else {
203 0 die $@;
204 }
205 }
206  
207 67 return $self->build_template( $path, $content );
208 }
209  
210 #pod =method build_template
211 #pod
212 #pod my $tmpl = $theme->build_template( $path, $content )
213 #pod
214 #pod Build a new L<Statocles::Template> object with the given C<path> and C<content>.
215 #pod
216 #pod =cut
217  
218 155 ▶sub build_template {
Subroutine build_template: called CC=1 SCAR=0.0
Pod documented
219 155 my ( $self, $path, $content ) = @_;
220  
221 155 return Statocles::Template->new(
222 path => $path,
223 content => $content,
224 theme => $self,
225 );
226 }
227  
228 #pod =method template
229 #pod
230 #pod my $tmpl = $theme->template( $path )
231 #pod my $tmpl = $theme->template( @path_parts )
232 #pod
233 #pod Get the L<template|Statocles::Template> at the given C<path>, or with the
234 #pod given C<path_parts>.
235 #pod
236 #pod =cut
237  
238 113 ▶sub template {
Subroutine template: called CC=2 SCAR=6.9
Pod documented
239 113 my ( $self, @path ) = @_;
240 113 my $path = Path::Tiny->new( @path );
241 113 ▶ return $self->_templates->{ $path } ||= $self->read( $path );
Condition100%3/3
$self->_templates->{$path} ||= $self->read($path)
l!l&&r!l&&!r
45671
MC/DC100%2/2
$self->_templates->{$path} ||= $self->read($path)
$self->_templates->{$path} $self->read($path)
Truth table100%3/3
A ||= B
A= $self->_templates->{$path}B= $self->read($path)
ABresult
1X1
011
000
242 }
243  
244 #pod =method include
245 #pod
246 #pod my $tmpl = $theme->include( $path );
247 #pod my $tmpl = $theme->include( @path_parts );
248 #pod
249 #pod Get the desired L<template|Statocles::Template> to include based on the given
250 #pod C<path> or C<path_parts>. Looks through all the
251 #pod L<include_paths|/include_paths> before looking in the L<main path|/path>.
252 #pod
253 #pod =cut
254  
255 121 ▶sub include {
Subroutine include: called CC=6 SCAR=18.8
Pod documented
256 121 my ( $self, @path ) = @_;
257 121 my $render = 1;
258 121 ▶ if ( $path[0] eq '-raw' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path[0] eq "-raw") 0 121 121
259 # Allow raw files to not be passed through the template renderer
260 # This override flag will always exist, but in the future we may
261 # add better detection to possible file types to process
262 0 $render = 0;
263 0 shift @path;
264 }
265 121 my $path = Path::Tiny->new( @path );
266  
267 121 my @search_paths = ( @{ $self->include_paths }, $self->path );
268 121 for my $search_path ( @search_paths ) {
269 122 ▶ if ( $search_path->child( $path )->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_path->child($path)->is_file) 119 3 122
270 119 ▶ if ( $render ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render) 119 0 119
271 119 ▶ return $self->_includes->{ $path } ||= $self->build_template(
Condition66%2/3
$self->_includes->{$path} ||= $self->build_template($path, $search_path->child($path)->slurp_utf8)
l!l&&r!l&&!r
70490
MC/DC0%0/2
$self->_includes->{$path} ||= $self->build_template($path, $search_path->child($path)->slurp_utf8)
$self->_includes->{$path} $self->build_template($path, $search_path->child($path)->slurp_utf8)
Truth table66%2/3
A ||= B
A= $self->_includes->{$path}B= $self->build_template($path, $search_path->child($path)->slurp_utf8)
ABresult
1X1
011
000
272 $path, $search_path->child( $path )->slurp_utf8,
273 );
274 }
275 0 return $search_path->child( $path )->slurp_utf8;
276 }
277 }
278  
279 die qq{Can not find include "$path" in include directories: }
280 2 . join( ", ", map { sprintf q{"%s"}, $_ } @search_paths )
281 . "\n";
282 }
283  
284 #pod =method helper
285 #pod
286 #pod $theme->helper( $name, $sub );
287 #pod
288 #pod Register a helper on this theme. Helpers are functions that are added to
289 #pod the template to allow for additional features. Helpers are usually added
290 #pod by L<Statocles plugins|Statocles::Plugin>.
291 #pod
292 #pod There are a L<default set of helpers available to all
293 #pod templates|Statocles::Template/DEFAULT HELPERS> which cannot be
294 #pod overridden by this method.
295 #pod
296 #pod =cut
297  
298 8 ▶sub helper {
Subroutine helper: called CC=1 SCAR=0.0
Pod documented
299 8 my ( $self, $name, $sub ) = @_;
300 8 $self->_helpers->{ $name } = $sub;
301 8 return;
302 }
303  
304 #pod =method clear
305 #pod
306 #pod $theme->clear;
307 #pod
308 #pod Clear out the cached templates and includes. Used by the daemon when it
309 #pod detects a change to the theme files.
310 #pod
311 #pod =cut
312  
313 1 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
314 1 my ( $self ) = @_;
315 1 $self->_clear_templates;
316 1 $self->_clear_includes;
317 1 return;
318 }
319  
320 #pod =method pages
321 #pod
322 #pod Get the extra, non-template files to deploy with the rest of the site, like CSS,
323 #pod JavaScript, and images.
324 #pod
325 #pod Templates, files that end in C<.ep>, will not be deployed with the rest of the
326 #pod site.
327 #pod
328 #pod =cut
329  
330 2 ▶sub pages {
Subroutine pages: called CC=5 SCAR=16.1
Pod documented
331 2 my ( $self, $pages, %args ) = @_;
332  
333 0 my %has = map { $_->path => 1 } @$pages;
334  
335 # Find extra files in the main path to add
336 2 my @files;
337 2 my $iter = $self->path->iterator({ recurse => 1 });
338 2 ▶ while ( my $path = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = &$iter()) 58 2 60
339 58 ▶ next if !$path->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_file 20 38 58
340 38 ▶ next if $path =~ /[.]ep$/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /[.]ep$/ 36 2 38
341 2 ▶ next if $has{ $path }++;
Branch: 1 branch
BranchTrueFalseTotal
if $has{$path}++ 0 2 2
342 #; say "Theme file path: $path";
343 2 my $rel_path = Path::Tiny->new( $self->url_root, $path->relative( $self->path ) );
344 2 push @files, Statocles::Page::File->new(
345 site => $self->site,
346 path => $rel_path->stringify,
347 file_path => $path,
348 );
349 }
350  
351 #; say "Found pages: " . join ', ', map { $_->path } @files;
352 2 return @files;
353 };
354  
355 49 1;
356  
357 __END__
« blib/lib/Statocles/Template.pm ↑ Summary blib/lib/Statocles/App.pm »