Zucchini/Template.pm

filter: Statementstmt 83.0% 172 / 207 Branchbran 58.3% 42 / 72 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 96.0% 24 / 25 Podpod 92.8% 13 / 14 totaltotal 78.0% 252 / 323 CC 44 SCAR 19.5
CC
44
Cov
76%
process_file
19.3
process_directory
8.8
same_file
7.0
CRAP 7.0
« Zucchini.pm ↑ Summary Zucchini/Config.pm »
1 package Zucchini::Template;
2 6 $Zucchini::Template::VERSION = '0.0.21';
3 {
4 6 $Zucchini::Template::DIST = 'Zucchini';
5 }
6 # ABSTRACT: process templates and output static files
7 # vim: ts=8 sts=4 et sw=4 sr sta
8 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use strict; # for kwalitee testing
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Zucchini::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use HTML::Lint;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # object attributes
21 6 has config => (
22 reader => 'get_config',
23 writer => 'set_config',
24 isa => ZucchiniConfig,
25 is => 'ro',
26 );
27  
28 6 has ttobject => (
29 reader => 'get_ttobject',
30 writer => 'set_ttobject',
31 isa => TemplateToolkit,
32 is => 'ro',
33 );
34  
35 4 ▶sub process_site {
Subroutine process_site: called CC=1 SCAR=0.0
Pod documented
36 4 my $self = shift;
37 4 my $directory = $self->get_config->get_siteconfig->{source_dir};
38  
39 # start the directory descent ...
40 4 $self->process_directory( $directory );
41  
42 4 return;
43 }
44  
45 6 ▶sub process_directory {
Subroutine process_directory: called CC=8 SCAR=21.7
Pod documented
46 6 my $self = shift;
47 6 my $directory = shift;
48  
49 # for easier access - we should probably objectify this better - TODO
50 6 my $config = $self->get_config->get_siteconfig();
51 6 my $cliopt = $self->get_config->get_options();
52  
53 # function variables
54 6 my (@list, $relpath);
55  
56 # get the list of stuff in the directory
57 6 @list = $self->directory_contents($directory);
58 # get our relative path from 'source_dir'
59 6 $relpath = $self->relative_path_from_full($directory);
60  
61 # loop through the items in the list and Do The Right Thing
62 6 foreach my $item (@list) {
63 # process individual files
64 24 ▶ if (-f file($directory,$item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f file($directory, $item)) { } 22 2 24
elsif (-d file($directory, $item)) { } 2 0 2
65 # skip ignored files
66 22 ▶ if ($self->ignore_file($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_file($item)) 2 20 22
67 2 next;
68 }
69  
70 # getting this far means we should (try to) process the file
71 20 $self->process_file($directory, $item);
72 20 next;
73 }
74  
75 # process directories
76 elsif (-d file($directory,$item)) {
77 # skip ignored dirs
78 2 ▶ if ($self->ignore_directory($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_directory($item)) 0 2 2
79 0 next;
80 }
81  
82 2 my $outdir = dir($config->{output_dir}, $relpath, $item);
83 # make sure the directory exists in the output tree
84 2 ▶ if (! -d $outdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $outdir) 2 0 2
85 2 warn "output directory '$outdir' does not exist\n";
86 2 ▶ if (not mkdir($outdir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $outdir) 0 2 2
87 0 carp "couldn't create output directory: $!";
88 0 exit;
89 }
90 2 warn "created: $outdir\n";
91  
92 }
93  
94 # process the subdirectory
95 2 $self->process_directory(dir($directory,$item));
96 2 next;
97 }
98  
99 # not a file or directory?
100 # we don't handle Odd Stuff (yet?)
101 else {
102 0 warn "unhandled file-type for '" . dir($directory,$item) . "\n";
103 0 next;
104 }
105 }
106  
107 6 return;
108 }
109  
110 6 ▶sub directory_contents {
Subroutine directory_contents: called CC=2 SCAR=7.0
Pod documented
111 6 my $self = shift;
112 6 my $directory = shift;
113 6 my (@list);
114  
115 # get a list of everything (except . and ..) in $directory
116 6 ▶ opendir(my $dh, $directory)
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $directory or die "can't open '${directory}': $!\n" 6 0 6
117 or die("can't open '$directory': $!\n");
118  
119 6 @list = grep { $_ !~ /^\.\.?$/ } readdir($dh);
120  
121 6 return @list;
122 }
123  
124 0 ▶sub file_checksum {
Subroutine file_checksum: not called CC=2 SCAR=17.9
Pod documented
125 0 my $self = shift;
126 0 my $file = shift;
127 0 my ($md5);
128  
129 # try to open the file
130 0 ▶ open(my $fh,$file) or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or do { warn "Can't open ${file}: $!"; return undef } 0 0 0
131 0 warn "Can't open $file: $!";
132 0 return undef;
133 };
134 0 binmode($fh);
135  
136 0 $md5 = Digest::MD5->new->addfile($fh)->hexdigest;
137  
138 0 return $md5;
139 }
140  
141 14 ▶sub file_modified {
Subroutine file_modified: called CC=4 SCAR=14.0
Pod documented
142 14 my $self = shift;
143 14 my ($template_file, $templated_file) = @_;
144 14 my ($template_stat, $templated_stat);
145  
146 # if the destination file doesn't exist, it's "modified"
147 14 ▶ if (not -e $templated_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $templated_file) 13 1 14
148 13 return 1;
149 }
150  
151 # get stat info for each file
152 1 ▶ $template_stat = stat( $template_file) or die "no file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$template_stat = &Zucchini::Template::stat($template_file) or die "no file: $!\n" 1 0 1
153 1 ▶ $templated_stat = stat($templated_file) or die "no file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$templated_stat = &Zucchini::Template::stat($templated_file) or die "no file: $!\n" 1 0 1
154  
155 # return true if the templated file is OLDER than the template itself
156 # i.e. the source has been altered since we last generated the final result
157 1 return ($templated_stat->mtime < $template_stat->mtime);
158 }
159  
160 2 ▶sub ignore_directory {
Subroutine ignore_directory: called CC=4 SCAR=16.4
Pod documented
161 2 my ($self, $directory) = @_;
162  
163 2 foreach my $ignore_me (@{ $self->get_config->ignored_directories }) {
164 6 my $regex = qr/ \A $ignore_me \z /x;
165  
166 6 ▶ if ($directory =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directory =~ /$regex/) 0 6 6
167 0 ▶ warn "ignoring directory '$directory'. Match on '$regex'.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_config->verbose 0 0 0
168 if ($self->get_config->verbose);
169 0 return 1;
170 }
171 }
172  
173 2 return;
174 }
175  
176 22 ▶sub ignore_file {
Subroutine ignore_file: called CC=4 SCAR=13.9
Pod documented
177 22 my ($self, $filename) = @_;
178  
179 22 foreach my $ignore_me (@{ $self->get_config->ignored_files }) {
180 22 my $regex = qr/ $ignore_me /x;
181  
182 22 ▶ if ($filename =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /$regex/) 2 20 22
183 2 ▶ warn "ignoring file '$filename'. Match on '$regex'.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_config->verbose 0 2 2
184 if ($self->get_config->verbose);
185 2 return 1;
186 }
187 }
188  
189 20 return;
190 }
191  
192 19 ▶sub item_name {
Subroutine item_name: called CC=2 SCAR=7.2
Pod documented
193 19 my $self = shift;
194 19 my ($directory, $item) = @_;
195 19 my ($filename);
196  
197 # TODO - objectify better
198 19 my $cliopt = $self->get_config->get_options();
199 19 my $config = $self->get_config->get_siteconfig();
200  
201 # default case - just the item name
202 19 $filename = $item;
203  
204 # if we want to see the relative path
205 19 ▶ if ($cliopt->{showpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cliopt->{"showpath"}) 0 19 19
206 # get the full path to the file
207 0 $filename = file($directory,$item);
208 # remove path to sourcedir
209 0 $filename =~ s{\A$config->{source_dir}/?}{}xms;
210 }
211  
212 19 return $filename;
213 }
214  
215 20 ▶sub process_file {
Subroutine process_file: called CC=13 SCAR=29.6
Pod documented
216 20 my $self = shift;
217 20 my $directory = shift;
218 20 my $item = shift;
219 20 my ($relpath);
220  
221 # stuff we used to pass through in the script
222 # TODO objectify this
223 20 my $config = $self->get_config->get_siteconfig();
224 20 my $cliopt = $self->get_config->get_options();
225  
226 # get the relative path
227 20 $relpath = $self->relative_path_from_full($directory);
228  
229 # push the section name into the vars to replace
230 my $site_vars = {
231 source_dir => $config->{source_dir},
232 20 %{ $config->{tags} }
233 };
234  
235 # some files should be run through TT
236 20 ▶ if ($self->template_file($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->template_file($item)) { } 18 2 20
237  
238 # only create the template object once - it's stupid to create
239 # a new one for each file we template
240 18 ▶ if (not defined $self->get_ttobject) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get_ttobject) 4 14 18
241 4 $self->_prepare_template_object;
242 }
243  
244 # if the template and the destination have the same timestamp, nothing's changed
245 # HOWEVER, we only care if we're not forcing the template-output to be regenerated
246 18 ▶ if (not $cliopt->{force}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cliopt->{"force"}) 18 0 18
247 18 ▶ if ($self->always_process($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->always_process($item)) { } 4 14 18
elsif (not $self->file_modified(file($directory, $item), file($config->{'output_dir'}, $relpath, $item))) { } 1 13 14
248 # bypass modified check
249 }
250 elsif (not $self->file_modified(
251 file($directory,$item),
252 file($config->{output_dir},$relpath,$item)
253 )
254 ) {
255 1 ▶ warn "unchanged: " . $self->item_name($directory,$item) . qq{\n}
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_config->verbose(2) 0 1 1
256 if ($self->get_config->verbose(2));
257 1 return;
258 }
259 }
260  
261 17 warn (q{templating: } . $self->item_name($directory, $item) . qq{\n});
262 17 $self->show_destination($directory, $item);
263  
264 # ->process doesn't like Path::Class thingies being thrown at it
265 # so we force it to Stringify
266 $self->get_ttobject->process(
267 file($directory,$item) . q{},
268 $site_vars,
269 17 ▶ file($config->{output_dir},$relpath,$item) . q{}
Branch: 1 branch
BranchTrueFalseTotal
$self->get_ttobject->process(file($directory, $item) . '', $site_vars, file($config->{"output_dir"}, $relpath, $item) . '') or Carp::croak("\n" . $self->get_ttobject->error) 17 0 17
270 )
271 or Carp::croak ("\n" . $self->get_ttobject->error());
272  
273 # if we're doing lint-checking
274 17 ▶ if ($self->get_config()->get_siteconfig()->{lint_check}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_config->get_siteconfig->{"lint_check"}) 0 17 17
275 # check for HTML errors in file
276 0 ▶ if ($item =~ m{\.html?\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\.html?\z/) 0 0 0
277 0 my $lint;
278 0 eval "use HTML::Lint::Pluggable";
279 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
280 # create a new HTML::Lint object
281 0 $lint = HTML::Lint->new();
282 }
283 else {
284 # create a new HTML::Lint::Pluggable object
285 0 $lint = HTML::Lint::Pluggable->new();
286 # this gives us HTML5 support
287 0 $lint->load_plugins(qw/HTML5/);
288 }
289  
290 $lint->parse_file(
291 0 file($config->{output_dir},$relpath,$item) . q{}
292 );
293 0 foreach my $error ( $lint->errors ) {
294 # let the user know where and what the error is
295 0 warn (
296 q{!! }
297 . $self->item_name($directory, $item)
298 . q{: line }
299 . $error->line
300 . q{: }
301 . $error->errtext
302 . qq{\n}
303 );
304 }
305 }
306 }
307 }
308 # others should be copied (if they've changed
309 else {
310 # only copy files if the MD5 hasn't changed
311 2 ▶ if (not $self->same_file(
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->same_file(file($directory, $item), file($config->{"output_dir"}, $relpath, $item))) 2 0 2
312 file($directory,$item),
313 file($config->{output_dir},$relpath,$item)
314 )
315 ) {
316 2 warn (q{Copying: } . $self->item_name($directory, $item) . qq{\n});
317 # the ".q{}" forces stringification and resolves issues with
318 # File::Copy::_eq() in perl-5.10
319 copy(
320 file($directory,$item) . q{},
321 2 file($config->{output_dir},$relpath,$item) . q{}
322 );
323 2 $self->show_destination($directory, $item);
324 }
325 }
326  
327 19 return;
328 }
329  
330 45 ▶sub relative_path_from_full {
Subroutine relative_path_from_full: called CC=1 SCAR=0.0
Pod documented
331 45 my $self = shift;
332 45 my $directory = shift;
333 45 my $config = $self->get_config->get_siteconfig();
334 45 my ($relpath);
335  
336 # get the relative path from the full srcdir path
337 45 $relpath = $directory;
338 # remove source_dir from directory path
339 45 $relpath =~ s:^$config->{source_dir}::;
340 # remove leading / (if any)
341 45 $relpath =~ s:^/::; # fixme - assuming unix system
342  
343 45 return $relpath;
344 }
345  
346 2 ▶sub same_file {
Subroutine same_file: called CC=4 SCAR=19.4
Pod documented
347 2 my $self = shift;
348 2 my ($file1, $file2) = @_;
349  
350 2 ▶ if (! -f $file2 or ! -f $file2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file2 and -f $file2) 2 0 2
Condition33%1/3
-f $file2 and -f $file2
!ll&&!rl&&r
200
MC/DC0%0/2
-f $file2 and -f $file2
-f $file2 -f $file2
Truth table33%1/3
A and B
A= -f $file2B= -f $file2
ABresult
0X0
100
111
351 2 return 0;
352 }
353  
354 0 ▶ if ($self->file_checksum($file1) eq $self->file_checksum($file2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_checksum($file1) eq $self->file_checksum($file2)) 0 0 0
355 0 return 1;
356 }
357  
358 0 return 0;
359 }
360  
361 19 ▶sub show_destination {
Subroutine show_destination: called CC=3 SCAR=12.7
Pod documented
362 19 my $self = shift;
363 19 my ($directory, $item) = @_;
364 19 my ($relpath);
365  
366 # stuff we used to pass through in the script
367 # TODO objectify this
368 19 my $config = $self->get_config->get_siteconfig();
369 19 my $cliopt = $self->get_config->get_options();
370  
371 # get the relative path for the directory
372 19 $relpath = $self->relative_path_from_full($directory);
373  
374 19 ▶ if ($cliopt->{showdest}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cliopt->{"showdest"}) 0 19 19
375 0 ▶ if ($relpath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relpath) { } 0 0 0
376 warn(
377 q{ --> }
378 0 . file($config->{output_dir},$relpath,$item)
379 . qq{\n}
380 );
381 }
382 # top-level files don't have a relpath and we'd prefer not to have
383 # '//' in the path
384 else {
385 warn(
386 q{ --> }
387 0 . file($config->{output_dir},$item)
388 . qq{\n}
389 );
390 }
391 }
392  
393 19 return;
394 }
395  
396 20 ▶sub template_file {
Subroutine template_file: called CC=3 SCAR=11.0
Pod documented
397 20 my ($self,$filename) = @_;
398 20 my $config = $self->get_config->get_siteconfig();
399  
400 20 foreach my $ignore_me (@{ $self->get_config->templated_files }) {
401 22 my $regex = qr/ $ignore_me /x;
402  
403 22 ▶ if ($filename =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /$regex/) 18 4 22
404 18 return 1;
405 }
406 }
407  
408 2 return;
409 }
410  
411 18 ▶sub always_process {
Subroutine always_process: called CC=4 SCAR=13.9
Pod undocumented
412 18 my ($self,$filename) = @_;
413 18 my $config = $self->get_config->get_siteconfig();
414  
415 # if we haven't got anything listed in our siteconfig, we don't have any
416 # special cases to worry about
417 return
418 18 ▶ if (not defined($self->get_config->always_process));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_config->always_process 12 6 18
419  
420 # loop through our special cases ...
421 6 foreach my $always_process (@{ $self->get_config->always_process }) {
422 10 my $regex = qr/ $always_process /x;
423  
424 10 ▶ if ($filename =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /$regex/) 4 6 10
425 4 return 1;
426 }
427 }
428  
429 2 return;
430 }
431  
432  
433  
434 5 ▶sub _prepare_template_object {
Subroutine _prepare_template_object: called CC=3 SCAR=11.0
435 5 my $self = shift;
436 5 my $config = $self->get_config->get_siteconfig();
437 #my $cliopt = $self->get_config->get_options();
438  
439 5 my $tt_config = {
440 ABSOLUTE => 1,
441 EVAL_PERL => 0,
442 INCLUDE_PATH => "$config->{source_dir}:$config->{includes_dir}",
443 };
444 5 ▶ if (defined $config->{plugin_base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"plugin_base"}) 0 5 5
445 0 $tt_config->{PLUGIN_BASE} = $config->{plugin_base};
446 }
447  
448 # if we've been given any tt_options, merge them into the config
449 # now
450 5 ▶ if (defined $config->{tt_options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"tt_options"}) 1 4 5
451 my %merged_cfg = (
452 1 %{ $tt_config },
453 1 %{ $config->{tt_options} }
454 );
455 1 $tt_config = \%merged_cfg;
456 }
457  
458 $self->set_ttobject(
459 5 Template->new( $tt_config )
460 );
461  
462 5 return;
463 }
464  
465 6 1;
466  
467 __END__
« Zucchini.pm ↑ Summary Zucchini/Config.pm »