lib/Templer/Site.pm

filter: Statementstmt 93.2% 192 / 206 Branchbran 58.8% 53 / 90 Conditioncond 50.0% 21 / 42 MC/DCmcdc 32.0% 8 / 25 Subroutinesub 100% 21 / 21 Podpod 100% 10 / 10 totaltotal 77.4% 305 / 394 CC 72 SCAR 31.3
CC
72
Cov
79%
build
55.8
sync
14.4
_findFiles
10.0
CRAP 22.8
« lib/Templer/Plugin/Breadcrumbs.pm ↑ Summary lib/Templer/Plugin/Hash.pm »
1  
2 =head1 NAME
3  
4 Templer::Site - An interface to a templer site.
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 use strict;
11 use warnings;
12  
13 use Templer::Site;
14  
15 # Create the helper.
16 my $site = Templer::Site->new( suffix => ".skx" );
17  
18 # Get the pages/assets.
19 my @pages = $site->pages();
20 my @assets = $site->assets();
21  
22 =cut
23  
24 =head1 DESCRIPTION
25  
26 This class encapsulates a site. A site is comprised of "pages" and "assets".
27  
28 =over 8
29  
30 =item Pages
31  
32 Pages are things which are template expanded. These are represented
33 by instances of the C<Templer::Site::Page> class.
34  
35 =item Assets
36  
37 Assets are files that are merely copied from the input directory to
38 the output path. If we're running in "in-place" mode then they are
39 ignored.
40  
41 Assets are represented by instances of the C<Templer::Site::Assets> class.
42  
43 =back
44  
45 This class contains helpers for finding and returning arrays of
46 both such objects, and the code necessary to work with them and build
47 a site.
48  
49 =cut
50  
51 =head1 LICENSE
52  
53 This module is free software; you can redistribute it and/or modify it
54 under the terms of either:
55  
56 a) the GNU General Public License as published by the Free Software
57 Foundation; either version 2, or (at your option) any later version,
58 or
59  
60 b) the Perl "Artistic License".
61  
62 =cut
63  
64 =head1 AUTHOR
65  
66 Steve Kemp <steve@steve.org.uk>
67  
68 =cut
69  
70 =head1 COPYRIGHT AND LICENSE
71  
72 Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>.
73  
74 This library is free software. You can modify and or distribute it under
75 the same terms as Perl itself.
76  
77 =cut
78  
79 =head1 METHODS
80  
81 =cut
82  
83  
84 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87  
88  
89 package Templer::Site;
90  
91  
92 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 11 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 11 ▶use File::Path qw! mkpath !;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 11 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 11 ▶use Templer::Site::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 11 ▶use Templer::Site::Asset;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99  
100  
101 =head2 new
102  
103 Constructor, this should be given a hash of arguments for example:
104  
105 =over 8
106  
107 =item input
108  
109 The input directory to process.
110  
111 =item output
112  
113 The output directory to write to.
114  
115 =item suffix
116  
117 The suffixe that will discover "Pages", for example '.skx', or '.tmplr'.
118  
119 =cut
120  
121 =back
122  
123 =cut
124  
125 sub new
126 11 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod documented
127 11 my ( $proto, %supplied ) = (@_);
128 11 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
129  
130 11 my $self = {};
131  
132 #
133 # Allow user supplied values to override our defaults
134 #
135 11 foreach my $key ( keys %supplied )
136 {
137 33 $self->{ lc $key } = $supplied{ $key };
138 }
139  
140 11 bless( $self, $class );
141 11 return $self;
142 }
143  
144  
145  
146 =head2 init
147  
148 Ensure that the input directory exists.
149  
150 Create the output directory if we're not running in-place.
151  
152 Create array of destination files.
153  
154 =cut
155  
156 sub init
157 6 ▶{
Subroutine init: called CC=4 SCAR=14.0
Pod documented
158 6 my ($self) = (@_);
159  
160 #
161 # Ensure we have an input directory.
162 #
163 6 my $input = $self->{ 'input' };
164 6 ▶ if ( !-d $input )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $input) 0 6 6
165 {
166 0 print "The input directory doesn't exist: $input\n";
167 0 exit;
168 }
169  
170 #
171 # Ensure input directory contains a unique trailing /
172 #
173 6 $self->{ 'input' } .= "/";
174 6 $self->{ 'input' } =~ s{/+$}{/};
175  
176 #
177 # Ensure output directory contains a unique trailing /
178 #
179 6 $self->{ 'output' } .= "/";
180 6 $self->{ 'output' } =~ s{/+$}{/};
181  
182 #
183 # Create the output directory if missing, unless we're in-place
184 #
185 6 my $output = $self->{ 'output' };
186 6 my $inplace = $self->{ 'in-place' };
187  
188 6 ▶ File::Path::mkpath( $output, { verbose => 0, mode => oct(755) } )
Branch: 1 branch
BranchTrueFalseTotal
unless -d $output or $inplace 4 2 6
Condition100%3/3
-d $output or $inplace
l!l&&r!l&&!r
114
MC/DC100%2/2
-d $output or $inplace
-d $output $inplace
Truth table100%3/3
A or B
A= -d $outputB= $inplace
ABresult
1X1
011
000
189 if ( !-d $output && ( !$inplace ) );
190  
191 #
192 # We will store the list of all destination files
193 #
194 6 $self->set( "output-files", [] );
195 }
196  
197  
198 =head2 pages
199  
200 A site comprises of a collection of pages and a collection of static resources
201 which aren't touched/modified - these are "assets".
202  
203 Return a C<Templer::Site::Page> object for each page we've found.
204  
205 B<NOTE> We don't process pages with a "." prefix, i.e. dotfiles.
206  
207 =cut
208  
209 sub pages
210 2 ▶{
Subroutine pages: called CC=4 SCAR=17.2
Pod documented
211 2 my ( $self, %args ) = (@_);
212  
213 my $dir =
214 2 ▶ $args{ 'directory' } || $self->{ 'directory' } || $self->{ 'input' };
Condition33%2/6
$args{'directory'} || $self->{'directory'} || $self->{'input'}
l!l&&r!l&&!r
200
$args{"directory"} or $self->{"directory"}
l!l&&r!l&&!r
200
MC/DC0%0/3
$args{'directory'} || $self->{'directory'} || $self->{'input'}
$args{"directory"} $self->{"directory"} $self->{'input'}
Truth table25%1/4
A or B || C
A= $args{"directory"}B= $self->{"directory"}C= $self->{'input'}
ABCresult
1XX1
01X1
0011
0000
215 2 ▶ my $suffix = $args{ 'suffix' } || $self->{ 'suffix' };
Condition33%1/3
$args{'suffix'} || $self->{'suffix'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{'suffix'} || $self->{'suffix'}
$args{'suffix'} $self->{'suffix'}
Truth table33%1/3
A || B
A= $args{'suffix'}B= $self->{'suffix'}
ABresult
1X1
011
000
216  
217 return (
218 2 $self->_findFiles( must_match => $suffix . "\$",
219 object => "Templer::Site::Page",
220 directory => $dir,
221 hide_dotfiles => 1,
222 ) );
223 }
224  
225  
226 =head2 assets
227  
228 A site comprises of a collection of pages and a collection of static resources
229 which aren't touched/modified - these are "assets".
230  
231 Return a C<Templer::Site::Asset> object for each asset we find.
232  
233 B<NOTE> We include files which have a "." prefix here - to correctly
234 copy files such as ".htpasswd", ".htaccess", etc.
235  
236 =cut
237  
238 sub assets
239 2 ▶{
Subroutine assets: called CC=4 SCAR=17.2
Pod documented
240 2 my ( $self, %args ) = (@_);
241  
242 my $dir =
243 2 ▶ $args{ 'directory' } || $self->{ 'directory' } || $self->{ 'input' };
Condition33%2/6
$args{'directory'} || $self->{'directory'} || $self->{'input'}
l!l&&r!l&&!r
200
$args{"directory"} or $self->{"directory"}
l!l&&r!l&&!r
200
MC/DC0%0/3
$args{'directory'} || $self->{'directory'} || $self->{'input'}
$args{"directory"} $self->{"directory"} $self->{'input'}
Truth table25%1/4
A or B || C
A= $args{"directory"}B= $self->{"directory"}C= $self->{'input'}
ABCresult
1XX1
01X1
0011
0000
244 2 ▶ my $suffix = $args{ 'suffix' } || $self->{ 'suffix' };
Condition33%1/3
$args{'suffix'} || $self->{'suffix'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{'suffix'} || $self->{'suffix'}
$args{'suffix'} $self->{'suffix'}
Truth table33%1/3
A || B
A= $args{'suffix'}B= $self->{'suffix'}
ABresult
1X1
011
000
245  
246 return (
247 2 $self->_findFiles( must_not_match => $suffix . "\$",
248 object => "Templer::Site::Asset",
249 directory => $dir,
250 hide_dotfiles => 0,
251 ) );
252  
253 }
254  
255  
256 =head2 _findFiles
257  
258 Internal method to find files beneath the given directory and return a new object
259 for each one.
260  
261 We assume that the object constructor receives a hash as its sole
262 argument with the key "file" containing the file path.
263  
264 =cut
265  
266 sub _findFiles
267 5 ▶{
Subroutine _findFiles: called CC=10 SCAR=23.0
268 5 my ( $self, %args ) = (@_);
269  
270 #
271 # Remove the trailing "/" on the end of the directory to search.
272 #
273 5 $args{ 'directory' } =~ s/\/$//g;
274  
275 #
276 # Should we hide dotfiles?
277 #
278 5 my $dotfiles = $args{ 'hide_dotfiles' };
279  
280  
281 #
282 # Files we've found. Ignoring the suffix just now.
283 #
284 5 my %files;
285  
286 File::Find::find( {
287 26 ▶ wanted => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
288 26 my $name = $File::Find::name;
289 26 ▶ $files{ $name } += 1 unless ( $dotfiles && ( $name =~ /\/\./ ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $dotfiles and $name =~ m[/\.] 25 1 26
Condition100%3/3
$dotfiles and $name =~ m[/\.]
!ll&&!rl&&r
2051
MC/DC100%2/2
$dotfiles and $name =~ m[/\.]
$dotfiles $name =~ m[/\.]
Truth table100%3/3
A and B
A= $dotfilesB= $name =~ m[/\.]
ABresult
0X0
100
111
290 },
291 follow => 0,
292 no_chdir => 1
293 },
294 5 $args{ 'directory' } );
295  
296 #
297 # Remove the input
298 #
299 5 delete $files{ $args{ 'directory' } };
300  
301 #
302 # OK now we need to find the matches.
303 #
304 5 my @matches;
305  
306 #
307 # The class-object we're going to construct.
308 #
309 5 my $class = $args{ 'object' };
310  
311 5 ▶ if ( $args{ 'must_match' } )
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'must_match'}) { } 2 3 5
elsif ($args{'must_not_match'}) { } 2 1 3
312 {
313 2 foreach my $file ( sort keys %files )
314 {
315 3 ▶ next if ( -d $file );
Branch: 1 branch
BranchTrueFalseTotal
if -d $file 0 3 3
316 3 ▶ next unless ( $file =~ /$args{'must_match'}/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /$args{"must_match"}/ 1 2 3
317 2 push( @matches, $class->new( file => $file ) );
318 }
319 }
320 elsif ( $args{ 'must_not_match' } )
321 {
322 2 foreach my $file ( sort keys %files )
323 {
324 12 ▶ next if ( $file =~ /$args{'must_not_match'}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /$args{"must_not_match"}/ 2 10 12
325 10 push( @matches, $class->new( file => $file ) );
326 }
327 }
328 else
329 {
330 1 @matches = map {$class->new( file => $_ )} keys %files;
331 }
332  
333 #
334 # Every pages depend on the global configuration file
335 #
336 5 ▶ if ( $args{ 'object' } eq "Templer::Site::Page" )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"object"} eq "Templer::Site::Page") 2 3 5
337 {
338 2 foreach my $page (@matches)
339 {
340 2 $page->add_dependency( $self->{ 'config' } );
341 }
342 }
343  
344 5 @matches;
345 }
346  
347  
348  
349 =head2 build
350  
351 Build the site.
352  
353 This is the method which does all the page-expansion, site-generation, etc.
354  
355 The return value is the count of pages built.
356  
357 =cut
358  
359 sub build
360 2 ▶{
Subroutine build: called CC=29 SCAR=40.2
Pod documented
361 2 my ($self) = (@_);
362  
363 #
364 # If we have a plugin directory then load the plugins beneath it.
365 #
366 # NOTE: The bundled/built-in plugins will always be available.
367 #
368 2 my $PLUGINS = Templer::Plugin::Factory->new();
369 2 ▶ if ( -d $self->{ 'plugin-path' } )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{"plugin-path"}) 0 2 2
370 {
371 print "Loading plugins from : $self->{ 'plugin-path' }\n"
372 0 ▶ if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
373  
374 0 $PLUGINS->load_plugins( $self->{ 'plugin-path' } );
375 }
376  
377 #
378 # Initialize all plugins.
379 #
380 2 $PLUGINS->init($self);
381  
382 #
383 # Setup an array of include-paths.
384 #
385 2 my @INCLUDES;
386 2 foreach my $path ( split( /:/, $self->{ 'include-path' } ) )
387 {
388 2 ▶ push( @INCLUDES, $path ) if ( -d $path );
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 0 2 2
389 }
390 2 $self->set( "include-path", \@INCLUDES );
391  
392  
393 #
394 # Find all the pages we'll process.
395 #
396 # (Assets are copied later.)
397 #
398 2 my @pages = $self->pages( directory => $self->{ 'input' } );
399  
400  
401 #
402 # A count of the pages we've rebuilt.
403 #
404 2 my $rebuilt = 0;
405  
406  
407 #
408 # For each page we've found.
409 #
410 2 foreach my $page (@pages)
411 {
412  
413 #
414 # The path of the page, on-disk.
415 #
416 2 my $src = $page->source();
417 2 ▶ print "\nProcessing page: $src\n" if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
418  
419  
420 #
421 # Convert the input path to a suitable output path.
422 #
423 2 my $dst = $src;
424  
425 #
426 # The page might have its own idea of where it wants to
427 # go - so set that if it is set.
428 #
429 2 ▶ if ( $page->field("output") )
Branch: 1 branch
BranchTrueFalseTotal
if ($page->field('output')) { } 0 2 2
430 {
431 0 $dst = $self->{ 'output' } . $page->field("output");
432 }
433 else
434 {
435 $dst =~ s/^$self->{'input'}/$self->{'output'}/g
436 2 ▶ unless ( $self->{ 'in-place' } );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"in-place"} 2 0 2
437  
438 2 $dst =~ s/$self->{'suffix'}/.html/g;
439 }
440  
441 #
442 # Store the destination file path
443 #
444 2 push( @{ $self->{ 'output-files' } }, $dst );
445  
446 #
447 # Show the transformation.
448 #
449 2 ▶ print "File: $src\n" if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
450 2 ▶ print "Dest: $dst\n" if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
451  
452  
453 #
454 # The template to expand the content into will come from the page, or
455 # the global configuration object.
456 #
457 my $template = $page->layout() ||
458 2 ▶ $self->{ 'layout' };
Condition33%1/3
$page->layout || $self->{'layout'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$page->layout || $self->{'layout'}
$page->layout $self->{'layout'}
Truth table33%1/3
A || B
A= $page->layoutB= $self->{'layout'}
ABresult
1X1
011
000
459 print "Layout file is: $self->{'layout-path'}/$template\n"
460 2 ▶ if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
461  
462 #
463 # Ensure the template exists.
464 #
465 2 ▶ if ( !-e $self->{ 'layout-path' } . "/" . $template )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"layout-path"} . '/' . $template) 0 2 2
466 {
467 0 print
468 "WARNING: Layout file missing: $self->{'layout-path'}/$template\n";
469 0 next;
470 }
471  
472 #
473 # The template-data we'll expand for the page/template.
474 #
475 # (All fields from the page, and from the configuration file.)
476 #
477 2 my %data = ( $self->fields(), $page->fields() );
478  
479 #
480 # There may be template filters on templates
481 #
482 2 my @filters;
483 2 my $filter = $data{ 'template-filter' };
484  
485 2 ▶ if ($filter)
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 1 1 2
486 {
487 1 foreach my $f ( split( /,/, $filter ) )
488 {
489 2 $f =~ s/^\s+|\s+$//g;
490 2 ▶ next unless ($f);
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 2 2
491  
492 2 my $helper = $PLUGINS->filter($f);
493  
494 push( @filters,
495 { sub =>
496 4 ▶ sub {my $s = shift; $$s = $helper->filter($$s);},
Subroutine __ANON__: called CC=1 SCAR=0.0
497 2 format => 'scalar',
498 } );
499 }
500 }
501  
502 #
503 # Load the HTML::Template module against the layout.
504 #
505 my $tmpl =
506 HTML::Template->new(
507 filename => $self->{ 'layout-path' } . "/" . $template,
508 die_on_bad_params => 0,
509 2 path => [@INCLUDES, $self->{ 'layout-path' }],
510 search_path_on_include => 1,
511 global_vars => 1,
512 loop_context_vars => 1,
513 utf8 => 1,
514 filter => \@filters,
515 );
516  
517 #
518 # Use the plugin-factory to expand each of the variables.
519 #
520 2 my $ref = $PLUGINS->expand_variables( $self, $page, \%data );
521 2 %data = %$ref;
522  
523  
524 2 ▶ if ( $self->{ 'debug' } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 2 2
525 {
526 0 print "Post-expansion variables on : $src\n";
527 0 print "\t" . Dumper( \%data );
528 }
529  
530  
531 #
532 # At this point we can tell if we need to rebuild the page.
533 #
534 # We want to build the page if:
535 #
536 # * The output page is missing.
537 #
538 # * The input page, or any dependancy is newer than the output.
539 #
540 2 my $rebuild = 0;
541 2 ▶ $rebuild = 1 if ( !-e $dst );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dst 2 0 2
542  
543 2 ▶ if ( !$rebuild )
Branch: 1 branch
BranchTrueFalseTotal
unless ($rebuild) 0 2 2
544 {
545  
546 #
547 # Get the dependencies of the page - add in the page source,
548 # and the template path.
549 #
550 0 my @deps = ( $self->{ 'layout-path' } . "/" . $template,
551 $page->source(), $page->dependencies() );
552  
553 0 foreach my $d (@deps)
554 {
555 0 ▶ if ( -M $d < -M $dst )
Branch: 1 branch
BranchTrueFalseTotal
if (-M $d < -M $dst) 0 0 0
556 {
557 0 ▶ $self->{ 'verbose' } &&
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and print "Triggering rebuild: $d is more recent than $dst\n" 0 0 0
558 print "Triggering rebuild: $d is more recent than $dst\n";
559 0 $rebuild = 1;
560 }
561 }
562 }
563  
564 #
565 # Forced rebuild via the command-line.
566 #
567 2 ▶ $rebuild = 1 if ( $self->{ 'force' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"force"} 0 2 2
568  
569 #
570 # OK skip if we're not rebuilding, otherwise increase the count.
571 #
572 2 ▶ next unless ($rebuild);
Branch: 1 branch
BranchTrueFalseTotal
unless $rebuild 0 2 2
573 2 $rebuilt += 1;
574  
575  
576 #
577 # Load the HTML::Template module against the body of the page.
578 #
579 # (Includes are relative to the path of the input.)
580 #
581 2 my $dirName = $page->source();
582 2 ▶ if ( $dirName =~ /^(.*)\/(.*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($dirName =~ m[^(.*)/(.*)$]) 2 0 2
583 {
584 2 $dirName = $1;
585 }
586 2 my $body = HTML::Template->new( scalarref => \$page->content( \%data ),
587 die_on_bad_params => 0,
588 path => [@INCLUDES, $dirName],
589 search_path_on_include => 1,
590 global_vars => 1,
591 loop_context_vars => 1,
592 utf8 => 1,
593 filter => \@filters,
594 );
595  
596  
597 #
598 # Template-expand the body of the page.
599 #
600 2 $body->param( \%data );
601 2 $data{ 'content' } = $body->output();
602  
603  
604 #
605 # Make the (updated) global and per-page data available
606 # to the template object.
607 #
608 2 $tmpl->param( \%data );
609  
610 #
611 # Make sure the output path exists.
612 #
613 2 my $path = $dst;
614 2 ▶ if ( $path =~ /^(.*)\/(.*)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^(.*)/(.*)$]) 2 0 2
615 {
616 2 $path = $1;
617 2 ▶ File::Path::mkpath( $path, { verbose => 0, mode => oct(755) } )
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 2 2
618 if ( !-d $path );
619 }
620  
621 #
622 # Output the expanded template to the destination file.
623 #
624 2 ▶ open my $handle, ">:utf8", $dst or die "Failed to write to '$dst' - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, ">:utf8", $dst or die "Failed to write to '${dst}' - $!" 2 0 2
625 2 binmode( $handle, ":utf8" );
626 2 print $handle $tmpl->output();
627 2 close $handle;
628 }
629  
630 #
631 # Cleanup any plugins.
632 #
633 2 $PLUGINS->cleanup();
634  
635 #
636 # Return count of rebuilt pages.
637 #
638 2 return ($rebuilt);
639 }
640  
641  
642 =head2 copyAssets
643  
644 Copy all assets from the input directory to the output directory.
645  
646 This method will use tar to do so semi-efficiently.
647  
648 =cut
649  
650 sub copyAssets
651 2 ▶{
Subroutine copyAssets: called CC=9 SCAR=22.9
Pod documented
652 2 my ($self) = (@_);
653  
654  
655 #
656 # If we're running in-place then we don't need to copy assets.
657 #
658 2 ▶ return if ( $self->{ 'in-place' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in-place"} 0 2 2
659  
660 #
661 # The assets.
662 #
663 2 my @assets = $self->assets( directory => $self->{ 'input' } );
664  
665 #
666 # The files we're going to copy.
667 #
668 2 my @copy;
669  
670  
671 #
672 # We're going to build-up a command line to pass to tar
673 #
674 2 foreach my $asset (@assets)
675 {
676  
677 #
678 # Strip the input component of the filename(s).
679 #
680 10 my $src = $asset->source();
681 10 $src =~ s/^$self->{'input'}//g;
682  
683 #
684 # Store the destination file path
685 #
686 10 my $dst = $asset->source();
687 10 $dst =~ s/$self->{'input'}/$self->{'output'}/;
688 10 push( @{ $self->{ 'output-files' } }, $dst );
689  
690 #
691 # Filenames must be shell safe: we'll use it in a shell command
692 #
693 10 my $quoted_src;
694 10 ▶ if ( $src =~ /\'/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /\'/) { } 5 5 10
695 {
696 5 ( $quoted_src = "$src" ) =~ s{\\}{\\\\}g;
697 5 $quoted_src =~ s{\"}{\\\"}g;
698 5 $quoted_src =~ s{\$}{\\\$}g;
699 5 $quoted_src =~ s{\`}{\\\`}g;
700 5 $quoted_src = "\"$quoted_src\"";
701 }
702 else
703 {
704 5 $quoted_src = "'$src'";
705 }
706  
707 #
708 # We only copy asset which do not already exist or which exist but are
709 # newer than in destination (except for directory). If we've got an
710 # asset which is a directory that is already present, for example,
711 # we'll skip it.
712 #
713 10 ▶ if ( !-e "$self->{'output'}/$src" ||
Branch: 1 branch
BranchTrueFalseTotal
if (not -e qq[$self->{"output"}/$src] or not -d qq[$self->{"output"}/$src] and -M qq[$self->{"output"}/$src] > -M $asset->source) 10 0 10
Condition16%1/6
not -e qq[$self->{"output"}/$src] or not -d qq[$self->{"output"}/$src] and -M qq[$self->{"output"}/$src] > -M $asset->source
l!l&&r!l&&!r
1000
not -d qq[$self->{"output"}/$src] and -M qq[$self->{"output"}/$src] > -M $asset->source
!ll&&!rl&&r
000
MC/DC0%0/3
not -e qq[$self->{"output"}/$src] or not -d qq[$self->{"output"}/$src] and -M qq[$self->{"output"}/$src] > -M $asset->source
not -e qq[$self->{"output"}/$src] not -d qq[$self->{"output"}/$src] -M qq[$self->{"output"}/$src] > -M $asset->source
Truth table25%1/4
not(A) or not(B) and C
A= not -e qq[$self->{"output"}/$src]B= not -d qq[$self->{"output"}/$src]C= -M qq[$self->{"output"}/$src] > -M $asset->source
ABCresult
1XX1
0111
00X0
0100
714 ( !-d "$self->{'output'}/$src" &&
715 -M "$self->{'output'}/$src" > -M $asset->source() ) )
716 {
717 10 push( @copy, $quoted_src );
718 }
719 }
720  
721 #
722 # Run the copy, unless all files are present.
723 #
724 2 ▶ if ( scalar @copy ne 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @copy ne 0) 2 0 2
725 {
726  
727 #
728 # The horrible command we're going to execute.
729 #
730 2 my $cmd = "(cd $self->{'input'} && tar -cf - " .
731 join( " ", @copy ) . ") | ( cd $self->{'output'} && tar xf -)";
732 2 ▶ print "TAR: $cmd " if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 2 2
733 2 system($cmd );
734 }
735 }
736  
737  
738 =head2 sync
739  
740 Delete all files from output directory which do not come from the input directory.
741  
742 =cut
743  
744 sub sync
745 1 ▶{
Subroutine sync: called CC=14 SCAR=26.7
Pod documented
746 1 my ($self) = @_;
747  
748 1 ▶ return if $self->{ 'in-place' };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in-place"} 0 1 1
749  
750 1 ▶ return unless $self->{ 'sync' };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sync"} 0 1 1
751  
752 #
753 # Get list of created and existing files
754 #
755 1 my @created = sort @{ $self->{ 'output-files' } };
756  
757 my @existing = sort
758 5 map {$_->source();}
759 $self->_findFiles( object => "Templer::Site::Asset",
760 1 directory => $self->{ 'output' },
761 hide_dotfiles => 0,
762 );
763  
764 #
765 # Determine files to remove
766 #
767 1 my @files = ();
768 1 my @dirs = ();
769 1 my %count = ();
770 1 foreach ( @created, @existing )
771 {
772 8 $count{ $_ }++;
773 }
774 1 foreach ( keys %count )
775 {
776 5 ▶ push( @files, $_ ) if ( $count{ $_ } == 1 && !-d $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $count{$_} == 1 and not -d $_ 1 4 5
Condition100%3/3
$count{$_} == 1 and not -d $_
!ll&&!rl&&r
311
MC/DC100%2/2
$count{$_} == 1 and not -d $_
$count{$_} == 1 not -d $_
Truth table100%3/3
A and not(B)
A= $count{$_} == 1B= not -d $_
ABresult
0X0
100
111
777 5 ▶ push( @dirs, $_ ) if ( $count{ $_ } == 1 && -d $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $count{$_} == 1 and -d $_ 1 4 5
Condition100%3/3
$count{$_} == 1 and -d $_
!ll&&!rl&&r
311
MC/DC100%2/2
$count{$_} == 1 and -d $_
$count{$_} == 1 -d $_
Truth table100%3/3
A and B
A= $count{$_} == 1B= -d $_
ABresult
0X0
100
111
778 }
779 1 @files = sort @files;
780 1 @dirs = sort @dirs;
781  
782 #
783 # Removing files
784 #
785 1 ▶ if (@files)
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 1 0 1
786 {
787 1 ▶ print "\nRemoving files: @files\n" if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
788  
789 1 unlink @files;
790 }
791  
792 #
793 # Removing directories
794 #
795 1 ▶ if (@dirs)
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 1 0 1
796 {
797 1 ▶ print "\nRemoving directories: @dirs\n" if ( $self->{ 'verbose' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
798 1 foreach (@dirs)
799 {
800 1 rmdir $_;
801 }
802 }
803 }
804  
805 =head2 set
806  
807 Store/update a key/value pair in our internal store.
808  
809 This allows the values passed in the constructor to be updated/added to.
810  
811 =cut
812  
813 sub set
814 8 ▶{
Subroutine set: called CC=1 SCAR=0.0
Pod documented
815 8 my ( $self, $key, $values ) = (@_);
816 8 $self->{ $key } = $values;
817 }
818  
819  
820 =head2 fields
821  
822 Get all known key + value pairs from our store.
823  
824 This is called to get all global variables for template interpolation
825 as part of the build. (The global variables and the per-page variables
826 are each fetched and expanded via plugins prior to getting sent to the
827 HTML::Template object.).
828  
829 =cut
830  
831 sub fields
832 2 ▶{
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
833 2 my ($self) = (@_);
834  
835 2 %$self;
836 }
837  
838  
839 =head2 get
840  
841 Get a single value from our store of variables.
842  
843 =cut
844  
845 sub get
846 8 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod documented
847 8 my ( $self, $field ) = (@_);
848 8 return ( $self->{ $field } );
849 }
850  
851  
852  
853 11 1;
« lib/Templer/Plugin/Breadcrumbs.pm ↑ Summary lib/Templer/Plugin/Hash.pm »