KhatGallery/Core.pm

filter: Statementstmt 5.6% 41 / 727 Branchbran 0.0% 0 / 248 Conditioncond 0.0% 0 / 74 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 22.0% 13 / 59 Podpod 100% 45 / 45 totaltotal 8.2% 99 / 1197 CC 168 SCAR 47.1
CC
168
Cov
4%
index_needs_rebuilding
420.0
init
272.0
make_index_page
210.0
CRAP 111.0
↑ Summary KhatGallery.pm »
1 package HTML::KhatGallery::Core;
2 4 our $VERSION = '0.26'; # VERSION
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 HTML::KhatGallery::Core - the core methods for HTML::KhatGallery
9  
10 =head1 VERSION
11  
12 version 0.26
13  
14 =head1 SYNOPSIS
15  
16 # implicitly
17 use HTML::KhatGallery qw(HTML::KhatGallery::Core HTML::KhatGallery::Plugin::MyPlugin ...);
18  
19 # or explicitly
20 require HTML::KhatGallery;
21  
22 @plugins = qw(HTML::KhatGallery::Core HTML::KhatGallery::Plugin::MyPlugin ...);
23 HTML::KhatGallery->import(@plugins);
24 HTML::KhatGallery->run(%args);
25  
26  
27 =head1 DESCRIPTION
28  
29 HTML::KhatGallery is a photo-gallery generator.
30  
31 HTML::KhatGallery::Core provides the core functionality of the system.
32 Other functions can be added or overridden by plugin modules.
33  
34 =cut
35  
36 4 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use YAML qw(Dump LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use Image::ExifTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 =head1 CLASS METHODS
45  
46 =head2 run
47  
48 HTML::KhatGallery->run(%args);
49  
50 C<run> is the only method you should need to use from outside
51 this module; other methods are called internally by this one.
52  
53 This method orchestrates all the work; it creates a new object,
54 and applies all the actions.
55  
56 Arguments:
57  
58 =over
59  
60 =item B<captions_file>
61  
62 The name of the captions file; which is in the same directory
63 as the images which it describes. This file is in L<YAML> format.
64 For example:
65  
66 ---
67 index.html: this is the caption for the album as a whole
68 image1.png: this is the caption for image1.png
69 image2.jpg: I like the second image
70  
71 (default: captions.yml)
72  
73 =item B<clean>
74  
75 Instead of generating files, clean up the thumbnail directories to
76 remove thumbnails and image HTML pages for images which are no
77 longer there.
78  
79 =item B<debug_level>
80  
81 Set the level of debugging output. The higher the level, the more verbose.
82 (developer only)
83 (default: 0)
84  
85 =item B<dir_match>
86  
87 Regular expression to match the directories we are interested in.
88 Hidden directories and the thumbnail directory will never be included.
89  
90 =item B<force_html>
91  
92 Force the re-generation of all the HTML files even if they already
93 exist. If false (the default) then a given HTML file will only be
94 created if there is a change in that particular directory.
95  
96 =item B<force_images>
97  
98 Force the re-generation of the thumbnail images even if they already
99 exist. If false (the default) then a given (thumbnail) image file will
100 only be created if it doesn't already exist.
101  
102 =item B<image_match>
103  
104 Regular expression determining what filenames should be interpreted
105 as images.
106  
107 =item B<meta>
108  
109 Array reference containing formats for meta-data from the images.
110 Field names are surrounded by % characters. For example:
111  
112 meta => ['Date: %DateTime%', '%Comment%'],
113  
114 If an image doesn't have that particular field, the data for that field is not
115 shown. All the meta-data is placed after any caption the image has.
116  
117 =item B<page_template>
118  
119 Template for HTML pages. The default template is this:
120  
121 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
122 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
123 <html xmlns="http://www.w3.org/1999/xhtml">
124 <head>
125 <title><!--kg_title--></title>
126 <!--kg_style-->
127 </head>
128 <body>
129 <!--kg_content-->
130 </body>
131 </html>
132  
133 This can be a string or a filename.
134  
135 =item B<per_page>
136  
137 The number of images to display per index page.
138  
139 =item B<thumbdir>
140  
141 The name of the directory where thumbnails and image-pages are put.
142 It is a subdirectory below the directory where its images are.
143 (default: tn)
144  
145 =item B<thumb_geom>
146  
147 The size of the thumbnails. This doesn't actually define the dimensions
148 of the thumbnails, but their area. This gives better-quality thumbnails.
149 (default:100x100)
150  
151 =item B<top_dir>
152  
153 The directory to look for images in; this will be searched for images and
154 sub-directories. If this is not given, the current directory is used.
155  
156 =item B<top_out_dir>
157  
158 The directory to create galleries in; HTML and thumbnails will be created
159 there. If this is not given, it is the same as B<top_dir>.
160  
161 =item B<top_url>
162  
163 The URL of the top images directory; if the top_out_dir isn't the
164 same as the top_dir, then we need to know this in order
165 to link to the images in the images directory.
166  
167 =item B<verbose>
168  
169 Print informational messages.
170  
171 =back
172  
173 =cut
174 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
175 0 my $class = shift;
176 0 my %args = (
177 parent=>'',
178 @_
179 );
180  
181 0 my $self = $class->new(%args);
182 0 $self->init();
183 print "Processing directory $self->{top_dir}\n"
184 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
185  
186 0 $self->do_dir_actions('');
187 } # run
188  
189 =head1 OBJECT METHODS
190  
191 Only of interest to developers and those wishing to write plugins.
192  
193 =head2 new
194  
195 Make a new object. See L</run> for the arguments.
196 This method should not be overridden by plugin writers; use L</init>
197 instead.
198  
199 =cut
200  
201 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
202 0 my $class = shift;
203 0 ▶ my $self = bless ({@_}, ref ($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
204  
205 0 return ($self);
206 } # new
207  
208 =head2 init
209  
210 Do some initialization of the object after it's created.
211 See L</run> for the arguments.
212 Set up defaults for things which haven't been defined.
213  
214 Plugin writers should override this method rather than L</new>
215 if they want to do some initialization for their plugin.
216  
217 =cut
218  
219 0 ▶sub init {
Subroutine init: not called CC=16 SCAR=56.1
Pod documented
220 0 my $self = shift;
221  
222 # some defaults
223 0 ▶ $self->{per_page} ||= 16;
Condition0%0/2
$self->{"per_page"} ||= 16
l!l
00
MC/DC0%0/1
$self->{"per_page"} ||= 16
$self->{"per_page"}
Truth table0%0/2
A
A= $self->{"per_page"}
Aresult
11
00
224 0 ▶ $self->{thumbdir} ||= 'tn';
Condition0%0/2
$self->{"thumbdir"} ||= "tn"
l!l
00
MC/DC0%0/1
$self->{"thumbdir"} ||= "tn"
$self->{"thumbdir"}
Truth table0%0/2
A
A= $self->{"thumbdir"}
Aresult
11
00
225 0 ▶ $self->{captions_file} ||= 'captions.yml';
Condition0%0/2
$self->{"captions_file"} ||= "captions.yml"
l!l
00
MC/DC0%0/1
$self->{"captions_file"} ||= "captions.yml"
$self->{"captions_file"}
Truth table0%0/2
A
A= $self->{"captions_file"}
Aresult
11
00
226 0 ▶ $self->{thumb_geom} ||= '100x100';
Condition0%0/2
$self->{"thumb_geom"} ||= "100x100"
l!l
00
MC/DC0%0/1
$self->{"thumb_geom"} ||= "100x100"
$self->{"thumb_geom"}
Truth table0%0/2
A
A= $self->{"thumb_geom"}
Aresult
11
00
227 0 ▶ $self->{force_html} ||= 0;
Condition0%0/2
$self->{"force_html"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"force_html"} ||= 0
$self->{"force_html"}
Truth table0%0/2
A
A= $self->{"force_html"}
Aresult
11
00
228 0 ▶ $self->{force_images} ||= 0;
Condition0%0/2
$self->{"force_images"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"force_images"} ||= 0
$self->{"force_images"}
Truth table0%0/2
A
A= $self->{"force_images"}
Aresult
11
00
229  
230 0 ▶ $self->{debug_level} ||= 0;
Condition0%0/2
$self->{"debug_level"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"debug_level"} ||= 0
$self->{"debug_level"}
Truth table0%0/2
A
A= $self->{"debug_level"}
Aresult
11
00
231 # if there's no top dir, make it the current one
232 0 ▶ if (!defined $self->{top_dir})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"top_dir"}) 0 0 0
233 {
234 0 $self->{top_dir} = '.';
235 }
236 0 $self->{top_dir} = File::Spec->rel2abs($self->{top_dir});
237 0 $self->{top_base} = basename($self->{top_dir});
238  
239 # top_out_dir
240 0 ▶ if (!defined $self->{top_out_dir})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"top_out_dir"}) 0 0 0
241 {
242 0 $self->{top_out_dir} = $self->{top_dir};
243 }
244 0 $self->{top_out_dir} = File::Spec->rel2abs($self->{top_out_dir});
245 0 $self->{top_out_base} = basename($self->{top_out_dir});
246  
247 # trim top_url if it has a trailing slash
248 0 ▶ if (defined $self->{top_url})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'top_url'}) { } 0 0 0
249 {
250 0 $self->{top_url} =~ s!/$!!;
251 }
252 else
253 {
254 0 $self->{top_url} = '';
255 }
256  
257 # calculate width and height of thumbnail display
258 0 $self->{thumb_geom} =~ /(\d+)x(\d+)/;
259 0 $self->{thumb_width} = $1;
260 0 $self->{thumb_height} = $2;
261 0 $self->{pixelcount} = $self->{thumb_width} * $self->{thumb_height};
262  
263 0 ▶ if (!defined $self->{dir_actions})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"dir_actions"}) 0 0 0
264 {
265 0 $self->{dir_actions} = [qw(init_settings
266 read_captions
267 read_dir
268 read_out_dir
269 filter_images
270 sort_images
271 filter_dirs
272 sort_dirs
273 make_index_page
274 process_images
275 process_subdirs
276 tidy_up
277 )];
278 }
279 0 ▶ if (!defined $self->{clean_actions})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"clean_actions"}) 0 0 0
280 {
281 0 $self->{clean_actions} = [qw(init_settings
282 read_dir
283 filter_images
284 filter_dirs
285 clean_thumb_dir
286 clean_this_dir
287 process_subdirs
288 tidy_up
289 )];
290 }
291  
292 0 ▶ if (!defined $self->{image_actions})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"image_actions"}) 0 0 0
293 {
294 0 $self->{image_actions} = [qw(init_image_settings
295 make_thumbnail
296 make_image_page
297 image_tidy_up
298 )];
299 }
300  
301 0 ▶ if (!defined $self->{image_match})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"image_match"}) 0 0 0
302 {
303 0 my @img_ext = map {"\.$_\$"}
304 qw(jpg jpeg png gif tif tiff pcx xwd xpm xbm);
305 0 my $img_re = join('|', @img_ext);
306 0 $self->{image_match} = qr/$img_re/i;
307 }
308  
309 0 ▶ if (!defined $self->{page_template})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"page_template"}) 0 0 0
310 {
311 0 $self->{page_template} = <<EOT;
312 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
313 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
314 <html xmlns="http://www.w3.org/1999/xhtml">
315 <head>
316 <title><!--kg_title--></title>
317 <!--kg_style-->
318 </head>
319 <body>
320 <!--kg_content-->
321 </body>
322 </html>
323 EOT
324 }
325  
326 0 return ($self);
327 } # init
328  
329 =head2 do_dir_actions
330  
331 $self->do_dir_actions($dir);
332  
333 Do all the actions in the $self->{dir_actions} list, for the
334 given directory. If cleaning, do the actions in the 'clean_actions'
335 list instead.
336 If the dir is empty, this is taken to be the directory given in
337 $self->{top_dir}, the top-level directory.
338  
339 =cut
340 0 ▶sub do_dir_actions {
Subroutine do_dir_actions: not called CC=4 SCAR=24.4
Pod documented
341 0 my $self = shift;
342 0 my $dir = shift;
343  
344 0 my %state = ();
345 0 $state{stop} = 0;
346 0 $state{dir} = $dir;
347  
348 4 ▶ no strict qw(subs refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
349 my @actions = ($self->{clean}
350 0 ? @{$self->{clean_actions}}
351 0 ▶ : @{$self->{dir_actions}});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clean'} ? : 0 0 0
352 0 ▶ while (@actions)
Branch: 1 branch
BranchTrueFalseTotal
if (@actions) 0 0 0
353 {
354 0 my $action = shift @actions;
355 0 ▶ last if $state{stop};
Branch: 1 branch
BranchTrueFalseTotal
if $state{"stop"} 0 0 0
356 0 $state{action} = $action;
357 0 $self->debug(2, "action: $action");
358 0 $self->$action(\%state);
359 }
360 4 ▶ use strict qw(subs refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
361 0 1;
362 } # do_dir_actions
363  
364 =head2 do_image_actions
365  
366 $self->do_image_actions(\%dir_state, @images);
367  
368 Do all the actions in the $self->{image_actions} list, for the
369 given images.
370  
371 =cut
372 0 ▶sub do_image_actions {
Subroutine do_image_actions: not called CC=4 SCAR=25.6
Pod documented
373 0 my $self = shift;
374 0 my $dir_state = shift;
375 0 my @images = @_;
376  
377 0 my %images_state = ();
378  
379 4 ▶ no strict qw(subs refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
380 0 ▶ for (my $i = 0; $i < @images; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @images) 0 0 0
381 {
382 0 %images_state = ();
383 0 $images_state{stop} = 0;
384 0 $images_state{images} = \@images;
385 0 $images_state{num} = $i;
386 0 $images_state{cur_img} = $images[$i];
387 # pop off each action as we go;
388 # that way it's possible for an action to
389 # manipulate the actions array
390 0 @{$images_state{image_actions}} = @{$self->{image_actions}};
391 0 ▶ while (@{$images_state{image_actions}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$images_state{"image_actions"};}) 0 0 0
392 {
393 0 my $action = shift @{$images_state{image_actions}};
394 0 ▶ last if $images_state{stop};
Branch: 1 branch
BranchTrueFalseTotal
if $images_state{"stop"} 0 0 0
395 0 $images_state{action} = $action;
396 0 $self->debug(2, "image_action: $action");
397 0 $self->$action($dir_state,
398 \%images_state);
399 }
400 }
401 4 ▶ use strict qw(subs refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
402 0 1;
403 } # do_image_actions
404  
405 =head1 Dir Action Methods
406  
407 Methods implementing directory-related actions. All such actions
408 expect a reference to a state hash, and generally will update either
409 that hash or the object itself, or both, in the course of their
410 running.
411  
412 =head2 init_settings
413  
414 Initialize various settings that need to be set before everything
415 else.
416  
417 This is not the same as "init", because this is the start of
418 the dir_actions sequence; we do it for each directory (or sub-directory)
419 we traverse.
420  
421 =cut
422 0 ▶sub init_settings {
Subroutine init_settings: not called CC=2 SCAR=17.9
Pod documented
423 0 my $self = shift;
424 0 my $dir_state = shift;
425  
426 $dir_state->{abs_dir} = File::Spec->catdir(
427 0 realpath($self->{top_dir}), $dir_state->{dir});
428 $dir_state->{abs_out_dir} = File::Spec->catdir(
429 0 realpath($self->{top_out_dir}), $dir_state->{dir});
430 0 my @path = File::Spec->splitdir($dir_state->{abs_dir});
431 0 ▶ if ($dir_state->{dir})
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_state->{'dir'}) { } 0 0 0
432 {
433 0 $dir_state->{dirbase} = pop @path;
434 0 $dir_state->{parent} = pop @path;
435 0 $dir_state->{dir_url} = $self->{top_url} . '/' . $dir_state->{dir};
436 }
437 else # first dir
438 {
439 0 $dir_state->{dirbase} = pop @path;
440 0 $dir_state->{parent} = '';
441 0 $dir_state->{dir_url} = $self->{top_url};
442 }
443 # thumbnail dir for this directory
444 $dir_state->{abs_thumbdir} = File::Spec->catdir($dir_state->{abs_out_dir},
445 0 $self->{thumbdir});
446  
447 # reset the per-directory redo_html flag
448 0 $dir_state->{redo_html} = 0;
449  
450 } # init_settings
451  
452 =head2 read_captions
453  
454 Set the $dir_state->{captions} hash to contain all the
455 captions for this directory (if they exist)
456  
457 =cut
458 0 ▶sub read_captions {
Subroutine read_captions: not called CC=3 SCAR=24.8
Pod documented
459 0 my $self = shift;
460 0 my $dir_state = shift;
461  
462 my $captions_file = File::Spec->catfile($dir_state->{abs_dir},
463 0 $self->{captions_file});
464 0 ▶ if (!-f $captions_file)
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $captions_file) 0 0 0
465 {
466 $captions_file = File::Spec->catfile($dir_state->{abs_out_dir},
467 0 $self->{captions_file});
468 }
469 0 ▶ if (-f $captions_file)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $captions_file) 0 0 0
470 {
471 0 $dir_state->{captions} = {};
472 0 $dir_state->{captions} = LoadFile($captions_file);
473 }
474 } # read_captions
475  
476 =head2 read_dir
477  
478 Read the $dir_state->{dir} directory. Sets $dir_state->{subdirs}, and
479 $dir_state->{files} with the relative subdirs, and other files.
480  
481 =cut
482 0 ▶sub read_dir {
Subroutine read_dir: not called CC=7 SCAR=40.3
Pod documented
483 0 my $self = shift;
484 0 my $dir_state = shift;
485  
486 0 my $dh;
487 0 ▶ opendir($dh, $dir_state->{abs_dir}) or die "Can't opendir $dir_state->{abs_dir}: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir_state->{"abs_dir"} or die qq[Can't opendir $dir_state->{"abs_dir"}: $!] 0 0 0
488 0 my @subdirs = ();
489 0 my @files = ();
490 0 ▶ while (my $fn = readdir($dh))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dh)) 0 0 0
491 {
492 0 my $abs_fn = File::Spec->catfile($dir_state->{abs_dir}, $fn);
493 0 ▶ if ($fn =~ /^\./ or $fn eq $self->{thumbdir})
Branch: 3 branches
BranchTrueFalseTotal
if ($fn =~ /^\./ or $fn eq $self->{'thumbdir'}) { } 0 0 0
elsif (-d $abs_fn) { } 0 0 0
elsif ($fn =~ /\.html$/) { } 0 0 0
Condition0%0/3
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
$fn =~ /^\./ $fn eq $self->{"thumbdir"}
Truth table0%0/3
A or B
A= $fn =~ /^\./B= $fn eq $self->{"thumbdir"}
ABresult
1X1
011
000
494 {
495 # skip
496 }
497 elsif (-d $abs_fn)
498 {
499 0 push @subdirs, $fn;
500 }
501 # ignore any html files
502 elsif ($fn =~ /\.html$/)
503 {
504 }
505 else
506 {
507 0 push @files, $fn;
508 }
509 }
510 0 closedir($dh);
511  
512 0 $dir_state->{subdirs} = \@subdirs;
513 0 $dir_state->{files} = \@files;
514 } # read_dir
515  
516 =head2 read_out_dir
517  
518 Read the $dir_state->{dir} directory in the output tree.
519 Sets $dir_state->{index_files} with the index*.html files.
520  
521 =cut
522 0 ▶sub read_out_dir {
Subroutine read_out_dir: not called CC=7 SCAR=40.3
Pod documented
523 0 my $self = shift;
524 0 my $dir_state = shift;
525  
526 0 my @index_files = ();
527 0 ▶ if (-d $dir_state->{abs_out_dir})
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir_state->{"abs_out_dir"}) 0 0 0
528 {
529 0 my $dh;
530 0 ▶ opendir($dh, $dir_state->{abs_out_dir}) or die "Can't opendir $dir_state->{abs_out_dir}: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir_state->{"abs_out_dir"} or die qq[Can't opendir $dir_state->{"abs_out_dir"}: $!] 0 0 0
531 0 ▶ while (my $fn = readdir($dh))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dh)) 0 0 0
532 {
533 0 my $abs_fn = File::Spec->catfile($dir_state->{abs_out_dir}, $fn);
534 0 ▶ if ($fn =~ /^\./ or $fn eq $self->{thumbdir})
Branch: 2 branches
BranchTrueFalseTotal
if ($fn =~ /^\./ or $fn eq $self->{'thumbdir'}) { } 0 0 0
elsif ($fn =~ /index.*\.html$/) { } 0 0 0
Condition0%0/3
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
$fn =~ /^\./ $fn eq $self->{"thumbdir"}
Truth table0%0/3
A or B
A= $fn =~ /^\./B= $fn eq $self->{"thumbdir"}
ABresult
1X1
011
000
535 {
536 # skip
537 }
538 # remember the index files
539 elsif ($fn =~ /index.*\.html$/)
540 {
541 0 push @index_files, $fn;
542 }
543 }
544 0 closedir($dh);
545 }
546  
547 0 $dir_state->{index_files} = \@index_files;
548 } # read_out_dir
549  
550 =head2 filter_images
551  
552 Sets $dir_state->{files} to contain only image files that
553 we are interested in.
554  
555 =cut
556 0 ▶sub filter_images {
Subroutine filter_images: not called CC=3 SCAR=24.8
Pod documented
557 0 my $self = shift;
558 0 my $dir_state = shift;
559  
560 0 ▶ if ($self->{image_match}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"image_match"} and @{$dir_state->{"files"};}) 0 0 0
Condition0%0/3
$self->{"image_match"} and @{$dir_state->{"files"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"image_match"} and @{$dir_state->{"files"};}
$self->{"image_match"} @{$dir_state->{"files"};}
Truth table0%0/3
A and B
A= $self->{"image_match"}B= @{$dir_state->{"files"};}
ABresult
0X0
100
111
561 0 and @{$dir_state->{files}})
562 {
563 0 my $img_match = $self->{image_match};
564 my @images = grep {
565 0 /$img_match/
566 0 } @{$dir_state->{files}};
567 0 $dir_state->{files} = \@images;
568 }
569 } # filter_images
570  
571 =head2 sort_images
572  
573 Sorts the $dir_state->{files} array.
574  
575 =cut
576 0 ▶sub sort_images {
Subroutine sort_images: not called CC=2 SCAR=17.9
Pod documented
577 0 my $self = shift;
578 0 my $dir_state = shift;
579  
580 0 ▶ if (@{$dir_state->{files}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$dir_state->{"files"};}) 0 0 0
581 {
582 0 my @images = sort @{$dir_state->{files}};
583 0 $dir_state->{files} = \@images;
584 }
585 } # sort_images
586  
587 =head2 filter_dirs
588  
589 Sets $dir_state->{subdirs} to contain only directories that
590 we are interested in.
591  
592 =cut
593 0 ▶sub filter_dirs {
Subroutine filter_dirs: not called CC=3 SCAR=24.8
Pod documented
594 0 my $self = shift;
595 0 my $dir_state = shift;
596  
597 0 ▶ if ($self->{dir_match}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dir_match"} and @{$dir_state->{"subdirs"};}) 0 0 0
Condition0%0/3
$self->{"dir_match"} and @{$dir_state->{"subdirs"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir_match"} and @{$dir_state->{"subdirs"};}
$self->{"dir_match"} @{$dir_state->{"subdirs"};}
Truth table0%0/3
A and B
A= $self->{"dir_match"}B= @{$dir_state->{"subdirs"};}
ABresult
0X0
100
111
598 0 and @{$dir_state->{subdirs}})
599 {
600 0 my $dir_match = $self->{dir_match};
601 my @dirs = grep {
602 0 /$dir_match/
603 0 } @{$dir_state->{subdirs}};
604 0 $dir_state->{subdirs} = \@dirs;
605 }
606 } # filter_dirs
607  
608 =head2 sort_dirs
609  
610 Sorts the $dir_state->{subdirs} array.
611  
612 =cut
613 0 ▶sub sort_dirs {
Subroutine sort_dirs: not called CC=2 SCAR=17.9
Pod documented
614 0 my $self = shift;
615 0 my $dir_state = shift;
616  
617 0 ▶ if (@{$dir_state->{subdirs}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$dir_state->{"subdirs"};}) 0 0 0
618 {
619 0 my @dirs = sort @{$dir_state->{subdirs}};
620 0 $dir_state->{subdirs} = \@dirs;
621 }
622 } # sort_dirs
623  
624 =head2 make_index_page
625  
626 Make the index page(s) for this directory.
627  
628 =cut
629 0 ▶sub make_index_page {
Subroutine make_index_page: not called CC=14 SCAR=53.5
Pod documented
630 0 my $self = shift;
631 0 my $dir_state = shift;
632  
633 # determine the number of pages
634 # To make things easier, always put the subdirs on each index page
635 0 my $num_files = @{$dir_state->{files}};
636 0 my $pages = ceil($num_files / $self->{per_page});
637 # if there are only subdirs make sure you still make an index
638 0 ▶ if ($pages == 0 and @{$dir_state->{subdirs}})
Branch: 1 branch
BranchTrueFalseTotal
if ($pages == 0 and @{$dir_state->{"subdirs"};}) 0 0 0
Condition0%0/3
$pages == 0 and @{$dir_state->{"subdirs"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$pages == 0 and @{$dir_state->{"subdirs"};}
$pages == 0 @{$dir_state->{"subdirs"};}
Truth table0%0/3
A and B
A= $pages == 0B= @{$dir_state->{"subdirs"};}
ABresult
0X0
100
111
639 {
640 0 $pages = 1;
641 }
642 0 $dir_state->{pages} = $pages;
643  
644 # make the output dir if it doesn't exist
645 0 ▶ if (!-d $dir_state->{abs_out_dir})
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir_state->{"abs_out_dir"}) 0 0 0
646 {
647 0 mkdir $dir_state->{abs_out_dir};
648 }
649  
650 # if we have any new images in this directory, we need to re-make the index
651 # files because we don't know which index file it will appear in,
652 # and we need to re-make the other HTML files because
653 # we need to re-generate the prev/next links
654 0 $dir_state->{redo_html} = $self->index_needs_rebuilding($dir_state);
655  
656 # if forcing HTML, delete the old index pages
657 # just in case we are going to have fewer pages
658 # this time around
659 0 ▶ if ($self->{force_html} or $dir_state->{redo_html})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"force_html"} or $dir_state->{"redo_html"}) 0 0 0
Condition0%0/3
$self->{"force_html"} or $dir_state->{"redo_html"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"force_html"} or $dir_state->{"redo_html"}
$self->{"force_html"} $dir_state->{"redo_html"}
Truth table0%0/3
A or B
A= $self->{"force_html"}B= $dir_state->{"redo_html"}
ABresult
1X1
011
000
660 {
661 0 foreach my $if (@{$dir_state->{index_files}})
662 {
663 0 my $ff = File::Spec->catfile($dir_state->{abs_out_dir}, $if);
664 0 unlink $ff;
665 }
666 }
667  
668 0 ▶ if ($self->{verbose})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
669 {
670 # if the first index is gone, we're rebuilding all of them
671 0 my $first_index
672 = $self->get_index_pagename(dir_state=>$dir_state,
673 page=>1, get_filename=>1);
674 0 ▶ if (!-f $first_index)
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $first_index) 0 0 0
675 {
676 0 print "making $pages indexes\n";
677 }
678 }
679  
680 # for each page
681 0 ▶ for (my $page = 1; $page <= $pages; $page++)
Branch: 1 branch
BranchTrueFalseTotal
if ($page <= $pages) 0 0 0
682 {
683 # calculate the filename
684 0 my $ifile = $self->get_index_pagename(dir_state=>$dir_state,
685 page=>$page, get_filename=>1);
686 0 ▶ if (-f $ifile)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $ifile) 0 0 0
687 {
688 0 next;
689 }
690  
691 # figure which files are in this page
692 # Determine number of images to skip
693 0 my @images = ();
694 0 ▶ if (@{$dir_state->{files}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$dir_state->{"files"};}) 0 0 0
695 {
696 0 my $skip = $self->{per_page} * ($page-1);
697 # index of last entry to include
698 0 my $last = $skip + $self->{per_page};
699 0 ▶ $last = $num_files if ($last > $num_files);
Branch: 1 branch
BranchTrueFalseTotal
if $last > $num_files 0 0 0
700 0 $last--; # need the index, not the count
701 0 @images = @{$dir_state->{files}}[$skip .. $last];
702 }
703  
704 0 my @content = ();
705 0 push @content, $self->start_index_page($dir_state, $page);
706 # add the subdirs
707 0 push @content, $self->make_index_subdirs($dir_state, $page);
708 # add the images
709 0 push @content, $self->make_image_index(dir_state=>$dir_state,
710 page=>$page, images=>\@images);
711 0 push @content, $self->end_index_page($dir_state, $page);
712 0 my $content = join('', @content);
713  
714 # make the head stuff
715 0 my $title = $self->make_index_title($dir_state, $page);
716 0 my $style = $self->make_index_style($dir_state, $page);
717  
718 # put the page content in the template
719 0 my $out = $self->get_template($self->{page_template});
720 # save the content of the template in case we read it
721 # from a file
722 0 $self->{page_template} = $out;
723 0 $out =~ s/<!--kg_title-->/$title/;
724 0 $out =~ s/<!--kg_style-->/$style/;
725 0 $out =~ s/<!--kg_content-->/$content/;
726  
727 # write the page to the file
728 0 my $fh = undef;
729 0 ▶ open($fh, ">", $ifile) or die "Could not open $ifile for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $ifile or die "Could not open $ifile for writing: $!" 0 0 0
730 0 print $fh $out;
731 0 close($fh);
732 } # for each page
733 } # make_index_page
734  
735 =head2 clean_thumb_dir
736  
737 Clean unused thumbnails and image-pages from
738 the thumbnail directory of this directory
739  
740 =cut
741 0 ▶sub clean_thumb_dir {
Subroutine clean_thumb_dir: not called CC=13 SCAR=52.0
Pod documented
742 0 my $self = shift;
743 0 my $dir_state = shift;
744  
745 0 my $thumb_dir = File::Spec->catdir($dir_state->{abs_out_dir}, $self->{thumbdir});
746 0 my @pics = @{$dir_state->{files}};
747 0 $self->debug(2, "cleaning thumb dir: $thumb_dir");
748  
749 0 ▶ return unless -d $thumb_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $thumb_dir 0 0 0
750  
751 # store the pics as a hash to make checking easier
752 0 my %pics_hash = ();
753 0 foreach my $pic ( @pics )
754 {
755 0 $pics_hash{$pic} = 1;
756 }
757  
758 # Read the thumbnail directory
759 0 my $dirh;
760 0 opendir($dirh,$thumb_dir);
761 0 my @files = grep(!/^\.{1,2}$/, readdir($dirh));
762 0 closedir($dirh);
763  
764 # Check each file to make sure it's a currently used thumbnail or image_page
765 0 foreach my $file ( @files )
766 {
767 0 my $remove = '';
768 0 my $name = $file;
769 0 ▶ if ($name =~ s/\.html$//)
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ s/\.html$//) { } 0 0 0
elsif ($name =~ /(.+)\.jpg$/i) { } 0 0 0
770 {
771 # change the last underscore to a dot
772 0 $name =~ s/_([a-zA-Z0-9]+)$/.$1/;
773 $remove = "unused image page"
774 0 ▶ unless (exists $pics_hash{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pics_hash{$name} 0 0 0
775 }
776 elsif ($name =~ /(.+)\.jpg$/i) {
777 # Thumbnail?
778 0 $name = $1;
779 # change the last underscore to a dot
780 0 $name =~ s/_([a-zA-Z0-9]+)$/.$1/;
781 0 $self->debug(2, "thumb: $name");
782 $remove = "unused thumbnail"
783 0 ▶ unless (exists $pics_hash{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pics_hash{$name} 0 0 0
784 } else {
785 0 $remove = "unknown file";
786 }
787 0 ▶ if ($remove) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove) 0 0 0
788 0 ▶ print "Remove $remove: $file\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
789 0 my $fullname = File::Spec->catfile($thumb_dir, $file);
790 0 ▶ warn "Couldn't erase [$file]"
Branch: 1 branch
BranchTrueFalseTotal
unless unlink $fullname 0 0 0
791 unless unlink $fullname;
792 }
793 } # for each file
794  
795 # If the thumbnail dir is empty, it needs to be removed too.
796 0 opendir($dirh,$thumb_dir);
797 0 @files = readdir($dirh);
798 0 closedir($dirh);
799  
800 0 ▶ if (!@files)
Branch: 1 branch
BranchTrueFalseTotal
if (not @files) 0 0 0
801 {
802 0 ▶ warn "Couldn't erase empty dir [$thumb_dir]"
Branch: 1 branch
BranchTrueFalseTotal
unless rmdir $thumb_dir 0 0 0
803 unless rmdir $thumb_dir;
804 }
805 } # clean_thumb_dir
806  
807 =head2 clean_this_dir
808  
809 Do cleaning of this current directory.
810 Remove subdirs which no longer exist.
811  
812 =cut
813 0 ▶sub clean_this_dir {
Subroutine clean_this_dir: not called CC=12 SCAR=50.5
Pod documented
814 0 my $self = shift;
815 0 my $dir_state = shift;
816  
817 0 $self->debug(2, "cleaning dir: $dir_state->{abs_out_dir}");
818 0 ▶ return unless -d $dir_state->{abs_out_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir_state->{"abs_out_dir"} 0 0 0
819  
820 # store the dirs as a hash to make checking easier
821 0 my %dirs_hash = ();
822 0 foreach my $dir ( @{$dir_state->{subdirs}} )
823 {
824 0 $dirs_hash{$dir} = 1;
825 }
826  
827 0 my $dh;
828 0 ▶ opendir($dh, $dir_state->{abs_out_dir}) or die "Can't opendir $dir_state->{abs_out_dir}: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir_state->{"abs_out_dir"} or die qq[Can't opendir $dir_state->{"abs_out_dir"}: $!] 0 0 0
829 0 ▶ while (my $fn = readdir($dh))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dh)) 0 0 0
830 {
831 0 my $abs_fn = File::Spec->catfile($dir_state->{abs_out_dir}, $fn);
832 0 ▶ if ($fn =~ /^\./ or $fn eq $self->{thumbdir})
Branch: 3 branches
BranchTrueFalseTotal
if ($fn =~ /^\./ or $fn eq $self->{'thumbdir'}) { } 0 0 0
elsif ($fn =~ s/index.*\.html$//) { } 0 0 0
elsif (-d $abs_fn) { } 0 0 0
Condition0%0/3
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
$fn =~ /^\./ $fn eq $self->{"thumbdir"}
Truth table0%0/3
A or B
A= $fn =~ /^\./B= $fn eq $self->{"thumbdir"}
ABresult
1X1
011
000
833 {
834 # skip
835 }
836 elsif ($fn =~ s/index.*\.html$//)
837 {
838 # skip
839 }
840 elsif (-d $abs_fn)
841 {
842 0 ▶ if (!exists $dirs_hash{$fn})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $dirs_hash{$fn}) 0 0 0
843 {
844 0 ▶ print "Remove subdir $fn\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
845 0 ▶ warn "Couldn't erase [$fn]"
Branch: 1 branch
BranchTrueFalseTotal
unless rmdir $abs_fn 0 0 0
846 unless rmdir $abs_fn;
847 }
848 }
849 }
850 0 closedir($dh);
851  
852 } # clean_this_dir
853  
854 =head2 process_images
855  
856 Process the images from this directory.
857  
858 =cut
859 0 ▶sub process_images {
Subroutine process_images: not called CC=1 SCAR=6.9
Pod documented
860 0 my $self = shift;
861 0 my $dir_state = shift;
862  
863 0 $self->do_image_actions($dir_state, @{$dir_state->{files}});
864 } # process_images
865  
866 =head2 process_subdirs
867  
868 Process the sub-directories of this directory.
869  
870 =cut
871 0 ▶sub process_subdirs {
Subroutine process_subdirs: not called CC=4 SCAR=30.0
Pod documented
872 0 my $self = shift;
873 0 my $dir_state = shift;
874  
875 0 my @image_dirs = @{$dir_state->{subdirs}};
876  
877 0 foreach my $subdir (@image_dirs)
878 {
879 0 my $dir = $subdir;
880 0 ▶ if ($dir_state->{dir})
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_state->{"dir"}) 0 0 0
881 {
882 0 $dir = File::Spec->catdir($dir_state->{dir}, $subdir);
883 }
884 0 ▶ print "=== $dir ===\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
885 0 $self->do_dir_actions($dir);
886 }
887 } # process_subdirs
888  
889 =head2 tidy_up
890  
891 Cleanup after processing this directory.
892  
893 =cut
894 0 ▶sub tidy_up {
Subroutine tidy_up: not called CC=1 SCAR=6.9
Pod documented
895 0 my $self = shift;
896 0 my $dir_state = shift;
897  
898 } # tidy_up
899  
900 =head1 Image Action Methods
901  
902 Methods implementing per-image actions.
903  
904 =head2 init_image_settings
905  
906 Initialize settings for the current image.
907  
908 =cut
909 0 ▶sub init_image_settings {
Subroutine init_image_settings: not called CC=1 SCAR=6.9
Pod documented
910 0 my $self = shift;
911 0 my $dir_state = shift;
912 0 my $img_state = shift;
913  
914 $img_state->{abs_img} = File::Spec->catfile($dir_state->{abs_dir},
915 0 $img_state->{cur_img});
916 0 $img_state->{info} = $self->get_image_info($img_state->{abs_img});
917  
918 } # init_image_settings
919  
920 =head2 make_thumbnail
921  
922 Make a thumbnail of the current image.
923 Constant pixel count among generated images based on
924 http://www.chaosreigns.com/code/thumbnail/
925  
926 =cut
927 0 ▶sub make_thumbnail {
Subroutine make_thumbnail: not called CC=5 SCAR=34.0
Pod documented
928 0 my $self = shift;
929 0 my $dir_state = shift;
930 0 my $img_state = shift;
931  
932 my $thumb_file = $self->get_thumbnail_name(
933 dir_state=>$dir_state, image=>$img_state->{cur_img},
934 0 type=>'file');
935 0 ▶ if (!$self->need_to_generate_image($dir_state, $img_state,
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->need_to_generate_image($dir_state, $img_state, "check_image", $thumb_file)) 0 0 0
936 check_image=>$thumb_file))
937 {
938 0 return;
939 }
940 # make the thumbnail dir if it doesn't exist
941 0 ▶ if (!-d $dir_state->{abs_thumbdir})
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir_state->{"abs_thumbdir"}) 0 0 0
942 {
943 0 mkdir $dir_state->{abs_thumbdir};
944 }
945  
946 0 my $command = '';
947 0 ▶ if ($img_state->{cur_img} =~ /\.gif$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($img_state->{'cur_img'} =~ /\.gif$/) { } 0 0 0
948 {
949 # in case this is an animated gif, get the first frame only
950 $command = sprintf('magick %s -resize "%d@>" %s',
951 $img_state->{abs_img}[0],
952 $self->{pixelcount},
953 0 $thumb_file);
954 }
955 else
956 {
957 $command = sprintf('magick %s -resize "%d@>" %s',
958 $img_state->{abs_img},
959 $self->{pixelcount},
960 0 $thumb_file);
961 }
962 0 ▶ system($command) == 0
Branch: 1 branch
BranchTrueFalseTotal
system($command) == 0 or die "$command failed" 0 0 0
963 or die "$command failed";
964
965 } # make_thumbnail
966  
967 =head2 make_image_page
968  
969 Make HTML page for current image.
970  
971 =cut
972 0 ▶sub make_image_page {
Subroutine make_image_page: not called CC=6 SCAR=37.4
Pod documented
973 0 my $self = shift;
974 0 my $dir_state = shift;
975 0 my $img_state = shift;
976  
977 0 my $img_name = $img_state->{cur_img};
978 my $img_page_file = $self->get_image_pagename(dir_state=>$dir_state,
979 image=>$img_state->{cur_img},
980 0 type=>'file');
981 0 ▶ if (-f $img_page_file
Branch: 1 branch
BranchTrueFalseTotal
if (-f $img_page_file and not $self->{"force_html"} and not $dir_state->{"redo_html"}) 0 0 0
Condition0%0/6
-f $img_page_file and not $self->{"force_html"} and not $dir_state->{"redo_html"}
!ll&&!rl&&r
000
-f $img_page_file and not $self->{"force_html"}
!ll&&!rl&&r
000
MC/DC0%0/3
-f $img_page_file and not $self->{"force_html"} and not $dir_state->{"redo_html"}
-f $img_page_file not $self->{"force_html"} not $dir_state->{"redo_html"}
Truth table0%0/4
A and not(B) and not(C)
A= -f $img_page_fileB= not $self->{"force_html"}C= not $dir_state->{"redo_html"}
ABCresult
0XX0
10X0
1100
1111
982 and !$self->{force_html}
983 and !$dir_state->{redo_html})
984 {
985 0 return;
986 }
987 # make the thumbnail dir if it doesn't exist
988 0 ▶ if (!-d $dir_state->{abs_thumbdir})
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir_state->{"abs_thumbdir"}) 0 0 0
989 {
990 0 mkdir $dir_state->{abs_thumbdir};
991 }
992 0 my @content = ();
993 0 push @content, $self->start_image_page($dir_state, $img_state);
994 # add the image itself
995 0 push @content, $self->make_image_content($dir_state, $img_state);
996 0 push @content, $self->end_image_page($dir_state, $img_state);
997 0 my $content = join('', @content);
998  
999 # make the head stuff
1000 0 my $title = $self->make_image_title($dir_state, $img_state);
1001 0 my $style = $self->make_image_style($dir_state, $img_state);
1002  
1003 # put the page content in the template
1004 0 my $out = $self->get_template($self->{page_template});
1005 # save the content of the template in case we read it
1006 # from a file
1007 0 $self->{page_template} = $out;
1008 0 $out =~ s/<!--kg_title-->/$title/;
1009 0 $out =~ s/<!--kg_style-->/$style/;
1010 0 $out =~ s/<!--kg_content-->/$content/;
1011  
1012 # write the page to the file
1013 0 my $fh = undef;
1014 0 ▶ open($fh, ">", $img_page_file) or die "Could not open $img_page_file for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $img_page_file or die "Could not open $img_page_file for writing: $!" 0 0 0
1015 0 print $fh $out;
1016 0 close($fh);
1017 } # make_image_page
1018  
1019 =head2 image_tidy_up
1020  
1021 Clean up after the current image.
1022  
1023 =cut
1024 0 ▶sub image_tidy_up {
Subroutine image_tidy_up: not called CC=1 SCAR=6.9
Pod documented
1025 0 my $self = shift;
1026 0 my $dir_state = shift;
1027 0 my $img_state = shift;
1028  
1029 } # image_tidy_up
1030  
1031 =head1 Helper Methods
1032  
1033 Methods which can be called from within other methods.
1034  
1035 =head2 start_index_page
1036  
1037 push @content, $self->start_index_page($dir_state, $page);
1038  
1039 Create the start-of-page for an index page.
1040 This contains page content, not full <html> etc (that's expected
1041 to be in the full-page template).
1042 It contains the header, link to parent dirs and links to
1043 previous and next index-pages, and the album caption.
1044  
1045 =cut
1046 0 ▶sub start_index_page {
Subroutine start_index_page: not called CC=9 SCAR=45.0
Pod documented
1047 0 my $self = shift;
1048 0 my $dir_state = shift;
1049 0 my $page = shift;
1050  
1051 0 my @out = ();
1052 0 push @out, "<div class=\"kgindex\">\n";
1053  
1054 # Path array contains basenames from the top dir down to the current dir.
1055 0 my @path = split(/[\/\\]/, $dir_state->{dir});
1056  
1057 # Note that what we want is the top_out_base and not the top_base
1058 # because if they are not the same (because top_out_dir was set)
1059 # the salient info is the output directory and not the source directory.
1060 0 unshift @path, $self->{top_out_base};
1061  
1062 # we want to create relative links to all the dirs
1063 # above the current one, so work backwards
1064 0 my %uplinks = ();
1065 0 my $uplink = '';
1066 0 foreach my $dn (reverse @path)
1067 {
1068 0 $uplinks{$dn} = $uplink;
1069 0 ▶ if (!$uplink and $page > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (not $uplink and $page > 1) { } 0 0 0
Condition0%0/3
not $uplink and $page > 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $uplink and $page > 1
not $uplink $page > 1
Truth table0%0/3
not(A) and B
A= not $uplinkB= $page > 1
ABresult
0X0
100
111
1070 {
1071 0 $uplinks{$dn} = "index.html";
1072 }
1073 else
1074 {
1075 0 $uplink .= '../';
1076 }
1077 }
1078 0 my @header = ();
1079 0 foreach my $dn (@path)
1080 {
1081 0 my $pretty = $dn;
1082 0 $pretty =~ s/_/ /g;
1083 0 ▶ if ($uplinks{$dn})
Branch: 1 branch
BranchTrueFalseTotal
if ($uplinks{$dn}) { } 0 0 0
1084 {
1085 0 push @header, "<a href=\"$uplinks{$dn}\">$pretty</a>";
1086 }
1087 else
1088 {
1089 0 push @header, $pretty;
1090 }
1091 }
1092 0 push @out, '<h1>';
1093 0 push @out, join(' :: ', @header);
1094 0 push @out, "</h1>\n";
1095  
1096 # now for the prev, next links
1097 0 push @out, $self->make_index_prev_next($dir_state, $page);
1098  
1099 # and now for the album caption
1100 0 ▶ if (exists $dir_state->{captions})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dir_state->{"captions"}) 0 0 0
1101 {
1102 0 my $index_caption = 'index.html';
1103 0 ▶ if (exists $dir_state->{captions}->{$index_caption}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dir_state->{"captions"}{$index_caption} and defined $dir_state->{"captions"}{$index_caption}) 0 0 0
Condition0%0/3
exists $dir_state->{"captions"}{$index_caption} and defined $dir_state->{"captions"}{$index_caption}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $dir_state->{"captions"}{$index_caption} and defined $dir_state->{"captions"}{$index_caption}
exists $dir_state->{"captions"}{$index_caption} defined $dir_state->{"captions"}{$index_caption}
Truth table0%0/3
A and B
A= exists $dir_state->{"captions"}{$index_caption}B= defined $dir_state->{"captions"}{$index_caption}
ABresult
0X0
100
111
1104 and defined $dir_state->{captions}->{$index_caption})
1105 {
1106 0 push @out, '<div class="albumdesc">';
1107 0 push @out, $dir_state->{captions}->{$index_caption};
1108 0 push @out, "</div>\n";
1109 }
1110 }
1111  
1112 0 return join('', @out);
1113 } # start_index_page
1114  
1115 =head2 make_index_prev_next
1116  
1117 my $links = $self->start_index_page($dir_state, $page);
1118  
1119 Make the previous next other-index-pages links for the
1120 given index-page. Generally called for the top and bottom
1121 of the index page.
1122  
1123 =cut
1124 0 ▶sub make_index_prev_next {
Subroutine make_index_prev_next: not called CC=7 SCAR=40.3
Pod documented
1125 0 my $self = shift;
1126 0 my $dir_state = shift;
1127 0 my $page = shift;
1128  
1129 0 my @out = ();
1130 0 ▶ if ($dir_state->{pages} > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_state->{"pages"} > 1) 0 0 0
1131 {
1132 0 push @out, '<p class="prevnext">';
1133 # prev
1134 0 my $label = '&lt; - prev';
1135 0 ▶ if ($page > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($page > 1) 0 0 0
1136 {
1137 0 my $iurl = $self->get_index_pagename(dir_state=>$dir_state,
1138 page=>$page - 1, get_filename=>0);
1139 0 push @out, "<span class=\"pagelink prev\"><a href=\"${iurl}\">$label</a></span> ";
1140 }
1141  
1142 # pages, but only if more than two
1143 0 ▶ if ($dir_state->{pages} > 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_state->{"pages"} > 2) 0 0 0
1144 {
1145 0 ▶ for (my $i = 1; $i <= $dir_state->{pages}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $dir_state->{"pages"}) 0 0 0
1146 {
1147 0 ▶ if ($page == $i)
Branch: 1 branch
BranchTrueFalseTotal
if ($page == $i) { } 0 0 0
1148 {
1149 0 push @out, " <span class=\"pagelink curr\">[$i]</span> ";
1150 }
1151 else
1152 {
1153 0 my $iurl = $self->get_index_pagename(dir_state=>$dir_state,
1154 page=>$i, get_filename=>0);
1155 0 push @out, " <span class=\"pagelink pagenum\"><a href=\"${iurl}\">$i</a></span> ";
1156 }
1157 }
1158 }
1159 0 $label = 'next -&gt;';
1160 0 ▶ if (($page+1) <= $dir_state->{pages})
Branch: 1 branch
BranchTrueFalseTotal
if ($page + 1 <= $dir_state->{"pages"}) 0 0 0
1161 {
1162 0 my $iurl = $self->get_index_pagename(dir_state=>$dir_state,
1163 page=>$page + 1, get_filename=>0);
1164 0 push @out, " <span class=\"pagelink next\"><a href=\"${iurl}\">$label</a></span>";
1165 }
1166 0 push @out, "</p>\n";
1167 }
1168  
1169 0 return join('', @out);
1170 } # make_index_prev_next
1171  
1172 =head2 end_index_page
1173  
1174 push @content, $self->end_index_page($dir_state, $page);
1175  
1176 Create the end-of-page for an index page.
1177 This contains page content, not full <html> etc (that's expected
1178 to be in the full-page template).
1179  
1180 =cut
1181 0 ▶sub end_index_page {
Subroutine end_index_page: not called CC=1 SCAR=6.9
Pod documented
1182 0 my $self = shift;
1183 0 my $dir_state = shift;
1184 0 my $page = shift;
1185  
1186 0 my @out = ();
1187 0 push @out, "\n<hr style=\"clear:both;\"/>\n";
1188 0 push @out, $self->make_index_prev_next($dir_state, $page);
1189 0 push @out, "</div>\n";
1190 0 return join('', @out);
1191 } # end_index_page
1192  
1193 =head2 make_index_subdirs
1194  
1195 push @content, $self->make_index_subdirs($dir_state, $page);
1196  
1197 Create the subdirs section; this contains links to subdirs.
1198  
1199 =cut
1200 0 ▶sub make_index_subdirs {
Subroutine make_index_subdirs: not called CC=3 SCAR=24.8
Pod documented
1201 0 my $self = shift;
1202 0 my $dir_state = shift;
1203 0 my $page = shift;
1204  
1205 0 my @out = ();
1206  
1207 0 ▶ if (@{$dir_state->{subdirs}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$dir_state->{"subdirs"};}) 0 0 0
1208 {
1209 0 push @out, "\n<hr/>\n";
1210 0 push @out, "<div class=\"subdir\">\n";
1211 # subdirs
1212 0 foreach my $subdir (@{$dir_state->{subdirs}})
1213 {
1214 0 push @out, <<EOT;
1215 <div class="item">
1216 <a href="$subdir/">$subdir</a>
1217 </div>
1218 EOT
1219 }
1220 0 push @out, "</div>\n";
1221 }
1222 0 return join('', @out);
1223 } # make_index_subdirs
1224  
1225 =head2 make_image_index
1226  
1227 push @content, $self->make_image_index(dir_state=>$dir_state,
1228 page=>$page, images=>\@images);
1229  
1230 Create the images section; this contains links to image-pages, with thumbnails.
1231  
1232 =cut
1233 0 ▶sub make_image_index {
Subroutine make_image_index: not called CC=3 SCAR=24.8
Pod documented
1234 0 my $self = shift;
1235 0 my %args = (
1236 @_
1237 );
1238 0 my $dir_state = $args{dir_state};
1239  
1240 0 my @out = ();
1241  
1242 0 ▶ if (@{$args{images}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args{"images"};}) 0 0 0
1243 {
1244 0 push @out, "\n<hr style=\"clear:both;\"/>\n";
1245 0 push @out, "<div class=\"images\">\n";
1246 # subdirs
1247 0 foreach my $image (@{$args{images}})
1248 {
1249 0 my $image_link = $self->get_image_pagename(dir_state=>$dir_state,
1250 image=>$image, type=>'parent');
1251 0 my $thumbnail_link = $self->get_thumbnail_name(
1252 dir_state=>$dir_state,
1253 image=>$image, type=>'parent');
1254 0 my $image_name = $self->get_image_pagename(dir_state=>$dir_state,
1255 image=>$image, type=>'pretty');
1256 0 push @out, <<EOT;
1257 <div class="item">
1258 <div class="thumb">
1259 <a href="$image_link"><img src="$thumbnail_link" alt="$image"/></a><br/>
1260 <a href="$image_link">$image_name</a>
1261 </div>
1262 </div>
1263 EOT
1264 }
1265 0 push @out, "</div>\n";
1266 }
1267 0 return join('', @out);
1268 } # make_image_index
1269  
1270 =head2 make_index_title
1271  
1272 Make the title for the index page.
1273 This is expected to go inside a <title><!--kg_title--></title>
1274 in the page template.
1275  
1276 =cut
1277 0 ▶sub make_index_title {
Subroutine make_index_title: not called CC=2 SCAR=17.9
Pod documented
1278 0 my $self = shift;
1279 0 my $dir_state = shift;
1280 0 my $page = shift;
1281  
1282 0 my @out = ();
1283 # title
1284 0 push @out, $dir_state->{dirbase};
1285 0 ▶ push @out, " ($page)" if $page > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $page > 1 0 0 0
1286 0 return join('', @out);
1287 } # make_index_title
1288  
1289 =head2 make_index_style
1290  
1291 Make the style tags for the index page. This will be put in the
1292 <!--kg_style--> part of the template.
1293  
1294 =cut
1295 0 ▶sub make_index_style {
Subroutine make_index_style: not called CC=1 SCAR=6.9
Pod documented
1296 0 my $self = shift;
1297 0 my $dir_state = shift;
1298 0 my $page = shift;
1299  
1300 0 my @out = ();
1301 # style
1302 0 my $thumb_area_width = $self->{thumb_width} * 1.5;
1303 # 1.5 times the thumbnail, plus a fudge-factor for the words underneath
1304 0 my $thumb_area_height = ($self->{thumb_height} * 1.5) + 20;
1305 0 push @out, <<EOT;
1306 <style type="text/css">
1307 .subdir, .images, .prevnext {
1308 display: flex;
1309 flex-wrap: wrap;
1310 }
1311 .item {
1312 vertical-align: middle;
1313 text-align: center;
1314 margin: 10px;
1315 }
1316 .thumb {
1317 overflow: auto;
1318 font-size: small;
1319 }
1320 .albumdesc {
1321 width: 80%;
1322 border: solid 2px;
1323 margin-left: auto;
1324 margin-right: auto;
1325 background: #eeeeee;
1326 color: black;
1327 }
1328 .albumdesc p {
1329 margin: 0.75em;
1330 }
1331 </style>
1332 EOT
1333 0 return join('', @out);
1334 } # make_index_style
1335  
1336 =head2 get_index_pagename
1337  
1338 my $name = self->get_index_pagename(
1339 dir_state=>$dir_state,
1340 page=>$page,
1341 get_filename=>0);
1342  
1343 Get the name of the given index page; either the file name
1344 or the relative URL.
1345  
1346 =cut
1347 0 ▶sub get_index_pagename {
Subroutine get_index_pagename: not called CC=4 SCAR=30.0
Pod documented
1348 0 my $self = shift;
1349 0 my %args = (
1350 get_filename=>0,
1351 @_
1352 );
1353 0 my $dir_state = $args{dir_state};
1354 0 my $page = $args{page};
1355  
1356 0 my $pagename;
1357 0 ▶ if ($page == 1)
Branch: 2 branches
BranchTrueFalseTotal
if ($page == 1) { } 0 0 0
elsif ($dir_state->{'pages'} > 9) { } 0 0 0
1358 {
1359 0 $pagename = 'index.html';
1360 }
1361 elsif ($dir_state->{pages} > 9)
1362 {
1363 0 $pagename = sprintf("index%02d.html", $page);
1364 }
1365 else
1366 {
1367 0 $pagename = "index${page}.html";
1368 }
1369
1370 0 ▶ if ($args{get_filename})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'get_filename'}) { } 0 0 0
1371 {
1372 0 return File::Spec->catfile($dir_state->{abs_out_dir}, $pagename);
1373 }
1374 else # get URL
1375 {
1376 0 return $pagename;
1377 }
1378 } # get_index_pagename
1379  
1380 =head2 get_image_pagename
1381  
1382 my $name = self->get_image_pagename(
1383 dir_state=>$dir_state,
1384 image=>$image,
1385 type=>'file');
1386  
1387 Get the name of the image page; either the file name
1388 or the relative URL from above, or the relative URL
1389 from the sibling, or a 'pretty' name suitable for a title.
1390  
1391 The 'type' can be 'file', 'parent', 'sibling' or 'pretty'.
1392  
1393 =cut
1394 0 ▶sub get_image_pagename {
Subroutine get_image_pagename: not called CC=5 SCAR=34.0
Pod documented
1395 0 my $self = shift;
1396 0 my %args = (
1397 type=>'parent',
1398 @_
1399 );
1400 0 my $dir_state = $args{dir_state};
1401 0 my $image = $args{image};
1402
1403 0 my $thumbdir = $self->{thumbdir};
1404 0 my $img_page = $image;
1405 # change the last dot to underscore
1406 0 $img_page =~ s/\.(\w+)$/_$1/;
1407 0 $img_page .= ".html";
1408 0 ▶ if ($args{type} eq 'file')
Branch: 4 branches
BranchTrueFalseTotal
if ($args{'type'} eq 'file') { } 0 0 0
elsif ($args{'type'} eq 'parent') { } 0 0 0
elsif ($args{'type'} eq 'sibling') { } 0 0 0
elsif ($args{'type'} eq 'pretty') { } 0 0 0
1409 {
1410 0 return File::Spec->catfile($dir_state->{abs_out_dir}, $thumbdir, $img_page);
1411 }
1412 elsif ($args{type} eq 'parent')
1413 {
1414 0 return "${thumbdir}/${img_page}";
1415 }
1416 elsif ($args{type} eq 'sibling')
1417 {
1418 0 return ${img_page};
1419 }
1420 elsif ($args{type} eq 'pretty')
1421 {
1422 0 my $pretty = ${image};
1423 0 $pretty =~ s/\.(\w+)$//;
1424 0 $pretty =~ s/_/ /g;
1425 0 return $pretty;
1426 }
1427 0 return '';
1428 } # get_image_pagename
1429  
1430 =head2 get_thumbnail_name
1431  
1432 my $name = self->get_thumbnail_name(
1433 dir_state=>$dir_state,
1434 image=>$image,
1435 type=>'file');
1436  
1437 Get the name of the image thumbnail file; either the file name
1438 or the relative URL from above, or the relative URL
1439 from the sibling.
1440  
1441 The 'type' can be 'file', 'parent', 'sibling'.
1442  
1443 =cut
1444 0 ▶sub get_thumbnail_name {
Subroutine get_thumbnail_name: not called CC=4 SCAR=30.0
Pod documented
1445 0 my $self = shift;
1446 0 my %args = (
1447 type=>'parent',
1448 @_
1449 );
1450 0 my $dir_state = $args{dir_state};
1451 0 my $image = $args{image};
1452
1453 0 my $thumbdir = $self->{thumbdir};
1454 0 my $thumb = $image;
1455 # change the last dot to underscore
1456 0 $thumb =~ s/\.([\w]+)$/_$1/;
1457 0 $thumb .= ".jpg";
1458 0 ▶ if ($args{type} eq 'file')
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'type'} eq 'file') { } 0 0 0
elsif ($args{'type'} eq 'parent') { } 0 0 0
elsif ($args{'type'} eq 'sibling') { } 0 0 0
1459 {
1460 0 return File::Spec->catfile($dir_state->{abs_out_dir}, $thumbdir, $thumb);
1461 }
1462 elsif ($args{type} eq 'parent')
1463 {
1464 0 return "${thumbdir}/${thumb}";
1465 }
1466 elsif ($args{type} eq 'sibling')
1467 {
1468 0 return ${thumb};
1469 }
1470 0 return '';
1471 } # get_thumbnail_name
1472  
1473 =head2 get_caption
1474  
1475 my $name = self->get_caption(
1476 dir_state=>$dir_state,
1477 img_state->$img_state,
1478 image=>$image)
1479  
1480 Get the caption for this image.
1481 This also gets the meta-data if any is required.
1482  
1483 =cut
1484 0 ▶sub get_caption {
Subroutine get_caption: not called CC=11 SCAR=48.8
Pod documented
1485 0 my $self = shift;
1486 0 my %args = (
1487 @_
1488 );
1489 0 my $dir_state = $args{dir_state};
1490 0 my $img_state = $args{img_state};
1491 0 my $image = $args{image};
1492
1493 0 my @out = ();
1494 0 ▶ if (exists $dir_state->{captions})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dir_state->{"captions"}) 0 0 0
1495 {
1496 0 ▶ if (exists $dir_state->{captions}->{$image}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dir_state->{"captions"}{$image} and defined $dir_state->{"captions"}{$image}) 0 0 0
Condition0%0/3
exists $dir_state->{"captions"}{$image} and defined $dir_state->{"captions"}{$image}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $dir_state->{"captions"}{$image} and defined $dir_state->{"captions"}{$image}
exists $dir_state->{"captions"}{$image} defined $dir_state->{"captions"}{$image}
Truth table0%0/3
A and B
A= exists $dir_state->{"captions"}{$image}B= defined $dir_state->{"captions"}{$image}
ABresult
0X0
100
111
1497 and defined $dir_state->{captions}->{$image})
1498 {
1499 0 push @out, $dir_state->{captions}->{$image};
1500 }
1501 }
1502 0 ▶ if ($img_state and defined $self->{meta} and @{$self->{meta}})
Branch: 1 branch
BranchTrueFalseTotal
if ($img_state and defined $self->{"meta"} and @{$self->{"meta"};}) 0 0 0
Condition0%0/6
$img_state and defined $self->{"meta"} and @{$self->{"meta"};}
!ll&&!rl&&r
000
$img_state and defined $self->{"meta"}
!ll&&!rl&&r
000
MC/DC0%0/3
$img_state and defined $self->{"meta"} and @{$self->{"meta"};}
$img_state defined $self->{"meta"} @{$self->{"meta"};}
Truth table0%0/4
A and B and C
A= $img_stateB= defined $self->{"meta"}C= @{$self->{"meta"};}
ABCresult
0XX0
10X0
1100
1111
1503 {
1504 # only add the meta data if it's there
1505 0 foreach my $fieldspec (@{$self->{meta}})
1506 {
1507 0 $fieldspec =~ /%([\w\s]+)%/;
1508 0 my $field = $1;
1509 0 ▶ if (exists $img_state->{info}->{$field}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $img_state->{"info"}{$field} and defined $img_state->{"info"}{$field} and $img_state->{"info"}{$field}) 0 0 0
Condition0%0/6
exists $img_state->{"info"}{$field} and defined $img_state->{"info"}{$field} and $img_state->{"info"}{$field}
!ll&&!rl&&r
000
exists $img_state->{"info"}{$field} and defined $img_state->{"info"}{$field}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $img_state->{"info"}{$field} and defined $img_state->{"info"}{$field} and $img_state->{"info"}{$field}
exists $img_state->{"info"}{$field} defined $img_state->{"info"}{$field} $img_state->{"info"}{$field}
Truth table0%0/4
A and B and C
A= exists $img_state->{"info"}{$field}B= defined $img_state->{"info"}{$field}C= $img_state->{"info"}{$field}
ABCresult
0XX0
10X0
1100
1111
1510 and defined $img_state->{info}->{$field}
1511 and $img_state->{info}->{$field})
1512 {
1513 0 my $val = $fieldspec;
1514 0 my $fieldval = $img_state->{info}->{$field};
1515 # make the fieldval HTML-safe
1516 0 $fieldval =~ s/&/&amp;/g;
1517 0 $fieldval =~ s/</&lt;/g;
1518 0 $fieldval =~ s/>/&gt;/g;
1519 0 $val =~ s/%${field}%/$fieldval/g;
1520 0 push @out, $val;
1521 }
1522 }
1523 }
1524 0 return join("\n", @out);
1525 } # get_caption
1526  
1527 =head2 get_template
1528  
1529 my $templ = $self->get_template($template);
1530  
1531 Get the given template (read if it's from a file)
1532  
1533 =cut
1534 0 ▶sub get_template {
Subroutine get_template: not called CC=4 SCAR=30.0
Pod documented
1535 0 my $self = shift;
1536 0 my $template = shift;
1537  
1538 0 ▶ if ($template !~ /\n/
Branch: 1 branch
BranchTrueFalseTotal
if (not $template =~ /\n/ and -r $template) 0 0 0
Condition0%0/3
not $template =~ /\n/ and -r $template
!ll&&!rl&&r
000
MC/DC0%0/2
not $template =~ /\n/ and -r $template
not $template =~ /\n/ -r $template
Truth table0%0/3
not(A) and B
A= not $template =~ /\n/B= -r $template
ABresult
0X0
100
111
1539 && -r $template)
1540 {
1541 0 local $/ = undef;
1542 0 my $fh;
1543 0 ▶ open($fh, $template)
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $template or die "Could not open ", $template 0 0 0
1544 or die "Could not open ", $template;
1545 0 $template = <$fh>;
1546 0 close($fh);
1547 }
1548 0 return $template;
1549 } # get_template
1550  
1551 =head2 start_image_page
1552  
1553 push @content, $self->start_image_page($dir_state, $img_state);
1554  
1555 Create the start-of-page for an image page.
1556 This contains page content, not full <html> etc (that's expected
1557 to be in the full-page template).
1558 It contains the header, link to parent dirs and links to
1559 previous and next image-pages.
1560  
1561 =cut
1562 0 ▶sub start_image_page {
Subroutine start_image_page: not called CC=4 SCAR=30.0
Pod documented
1563 0 my $self = shift;
1564 0 my $dir_state = shift;
1565 0 my $img_state = shift;
1566  
1567 0 my @out = ();
1568 0 push @out, "<div class=\"kgimage\">\n";
1569  
1570 # Path array contains basenames from the top dir
1571 # down to the current dir.
1572 0 my @path = split(/[\/\\]/, $dir_state->{dir});
1573 0 unshift @path, $self->{top_out_base};
1574 # we want to create relative links to all the dirs
1575 # including the current one, so work backwards
1576 0 my %uplinks = ();
1577 0 my $uplink = '';
1578 0 foreach my $dn (reverse @path)
1579 {
1580 0 $uplink .= '../';
1581 0 $uplinks{$dn} = $uplink;
1582 }
1583 0 my @breadcrumb = ();
1584 0 foreach my $dn (@path)
1585 {
1586 0 ▶ if ($uplinks{$dn})
Branch: 1 branch
BranchTrueFalseTotal
if ($uplinks{$dn}) { } 0 0 0
1587 {
1588 0 push @breadcrumb, "<a href=\"$uplinks{$dn}\">$dn</a>";
1589 }
1590 else
1591 {
1592 0 push @breadcrumb, $dn;
1593 }
1594 }
1595 0 push @out, '<h1>';
1596 0 push @out, $img_state->{cur_img};
1597 0 push @out, "</h1>\n";
1598 0 push @out, '<p class="breadcrumb">';
1599 0 push @out, join(' > ', @breadcrumb);
1600 0 push @out, "</p>\n";
1601  
1602 # now for the prev, next links
1603 0 push @out, $self->make_image_prev_next(dir_state=>$dir_state,
1604 img_state=>$img_state);
1605  
1606 0 return join('', @out);
1607 } # start_image_page
1608  
1609 =head2 end_image_page
1610  
1611 push @content, $self->end_image_page($dir_state, $img_state);
1612  
1613 Create the end-of-page for an image page.
1614 This contains page content, not full <html> etc (that's expected
1615 to be in the full-page template).
1616  
1617 =cut
1618 0 ▶sub end_image_page {
Subroutine end_image_page: not called CC=1 SCAR=6.9
Pod documented
1619 0 my $self = shift;
1620 0 my $dir_state = shift;
1621 0 my $img_state = shift;
1622  
1623 0 my @out = ();
1624  
1625 # now for the prev, next links
1626 0 push @out, $self->make_image_prev_next(dir_state=>$dir_state,
1627 img_state=>$img_state,
1628 use_thumb=>1);
1629 0 push @out, "\n</div>\n";
1630  
1631 0 return join('', @out);
1632 } # end_image_page
1633  
1634 =head2 make_image_prev_next
1635  
1636 my $links = $self->make_image_prev_next(
1637 dir_state=>$dir_state,
1638 img_state=>$img_state);
1639  
1640 Make the previous next other-image-pages links for the
1641 given image-page. Generally called for the top and bottom
1642 of the image page.
1643  
1644 =cut
1645 0 ▶sub make_image_prev_next {
Subroutine make_image_prev_next: not called CC=6 SCAR=37.4
Pod documented
1646 0 my $self = shift;
1647 0 my %args = (
1648 use_thumb=>0,
1649 @_
1650 );
1651 0 my $dir_state = $args{dir_state};
1652 0 my $img_state = $args{img_state};
1653  
1654 0 my $img_num = $img_state->{num};
1655 0 my @out = ();
1656 0 ▶ if ($dir_state->{files} > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_state->{"files"} > 1) 0 0 0
1657 {
1658 0 push @out, '<div class="prevnext">';
1659 # prev
1660 0 push @out, "<span class=\"prev\">";
1661 0 my $label = '&lt; - prev';
1662 0 my $iurl;
1663 my $turl;
1664 0 ▶ if ($img_num > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($img_num > 0) { } 0 0 0
1665 {
1666 $iurl = $self->get_image_pagename(dir_state=>$dir_state,
1667 0 image=>$img_state->{images}->[$img_num - 1],
1668 type=>'sibling');
1669 $turl = $self->get_thumbnail_name(dir_state=>$dir_state,
1670 0 image=>$img_state->{images}->[$img_num - 1],
1671 type=>'sibling');
1672 }
1673 else
1674 {
1675 # loop to the last image
1676 $iurl = $self->get_image_pagename(dir_state=>$dir_state,
1677 0 image=>$img_state->{images}->[$#{$img_state->{images}}],
1678 type=>'sibling');
1679 $turl = $self->get_thumbnail_name(dir_state=>$dir_state,
1680 0 image=>$img_state->{images}->[$#{$img_state->{images}}],
1681 type=>'sibling');
1682 }
1683 0 push @out, "<span class=\"pagelink\"><a href=\"${iurl}\">$label</a></span> ";
1684 0 ▶ if ($args{use_thumb})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"use_thumb"}) 0 0 0
1685 {
1686 0 push @out, "<span class=\"thumb\"><a href=\"${iurl}\"><img src=\"$turl\" alt=\"$label\"/></a></span> ";
1687 }
1688 0 push @out, "</span>";
1689  
1690 0 push @out, "<span class=\"next\">";
1691 0 $label = 'next -&gt;';
1692 0 ▶ if (($img_num+1) < @{$img_state->{images}})
Branch: 1 branch
BranchTrueFalseTotal
if ($img_num + 1 < @{$img_state->{'images'};}) { } 0 0 0
1693 {
1694 $iurl = $self->get_image_pagename(dir_state=>$dir_state,
1695 0 image=>$img_state->{images}->[$img_num + 1],
1696 type=>'sibling');
1697 $turl = $self->get_thumbnail_name(dir_state=>$dir_state,
1698 0 image=>$img_state->{images}->[$img_num + 1],
1699 type=>'sibling');
1700 }
1701 else
1702 {
1703 # loop to the first image
1704 $iurl = $self->get_image_pagename(dir_state=>$dir_state,
1705 0 image=>$img_state->{images}->[0],
1706 type=>'sibling');
1707 $turl = $self->get_thumbnail_name(dir_state=>$dir_state,
1708 0 image=>$img_state->{images}->[0],
1709 type=>'sibling');
1710 }
1711 0 ▶ if ($args{use_thumb})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"use_thumb"}) 0 0 0
1712 {
1713 0 push @out, "<span class=\"thumb\"><a href=\"${iurl}\"><img src=\"$turl\" alt=\"$label\"/></a></span> ";
1714 }
1715 0 push @out, " <span class=\"pagelink\"><a href=\"${iurl}\">$label</a></span>";
1716 0 push @out, "</span>";
1717 0 push @out, "</div>\n";
1718 }
1719  
1720 0 return join('', @out);
1721 } # make_image_prev_next
1722  
1723 =head2 make_image_content
1724  
1725 Make the content of the image page, the image itself.
1726  
1727 =cut
1728 0 ▶sub make_image_content {
Subroutine make_image_content: not called CC=4 SCAR=30.0
Pod documented
1729 0 my $self = shift;
1730 0 my $dir_state = shift;
1731 0 my $img_state = shift;
1732  
1733 0 my $img_name = $img_state->{cur_img};
1734 0 my $caption = $self->get_caption(dir_state=>$dir_state,
1735 img_state=>$img_state,
1736 image=>$img_name);
1737 0 my $img_url = "../$img_name";
1738 0 ▶ if ($self->{top_dir} ne $self->{top_out_dir})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"top_dir"} ne $self->{"top_out_dir"}) 0 0 0
1739 {
1740 0 $img_url = $dir_state->{dir_url} . '/' . $img_name;
1741 }
1742 0 my @out = ();
1743 0 push @out, "<div class=\"image\" id=\"image\">\n";
1744 0 my $width = $img_state->{info}->{ImageWidth};
1745 0 my $height = $img_state->{info}->{ImageHeight};
1746 my $alt_text = ($img_state->{info}->{AltTextAccessibility}
1747 ? $img_state->{info}->{AltTextAccessibility}
1748 : ($img_state->{info}->{Title}
1749 ? $img_state->{info}->{Title}
1750 0 ▶ : $img_name)
Branch: 2 branches
BranchTrueFalseTotal
$img_state->{'info'}{'AltTextAccessibility'} ? : 0 0 0
$img_state->{'info'}{'Title'} ? : 0 0 0
1751 );
1752 0 push @out, "<img src=\"$img_url\" title=\"$alt_text\" alt=\"$alt_text\" style=\"width: ${width}px; height: ${height}px;\"/>\n";
1753 0 push @out, "<p class=\"caption\" id=\"caption\">$caption</p>\n";
1754 0 push @out, "</div>\n";
1755 0 return join('', @out);
1756 } # make_image_content
1757  
1758 =head2 make_image_title
1759  
1760 Make the title for the image page.
1761 This is expected to go inside a <title><!--kg_title--></title>
1762 in the page template.
1763  
1764 =cut
1765 0 ▶sub make_image_title {
Subroutine make_image_title: not called CC=1 SCAR=6.9
Pod documented
1766 0 my $self = shift;
1767 0 my $dir_state = shift;
1768 0 my $img_state = shift;
1769  
1770 0 my @out = ();
1771 # title
1772 0 push @out, $img_state->{cur_img};
1773 0 return join('', @out);
1774 } # make_image_title
1775  
1776 =head2 make_image_style
1777  
1778 Make the style tags for the image page. This will be put in the
1779 <!--kg_style--> part of the template.
1780  
1781 =cut
1782 0 ▶sub make_image_style {
Subroutine make_image_style: not called CC=1 SCAR=6.9
Pod documented
1783 0 my $self = shift;
1784 0 my $dir_state = shift;
1785 0 my $img_state = shift;
1786  
1787 0 my @out = ();
1788 # style
1789 0 push @out, <<EOT;
1790 <style type="text/css">
1791 .image {
1792 text-align: center;
1793 margin-left: auto;
1794 margin-right: auto;
1795 }
1796 .prevnext {
1797 display: flex;
1798 width: 100%;
1799 justify-content: space-between;
1800 }
1801 .caption {
1802 white-space: pre-line;
1803 }
1804 </style>
1805 EOT
1806 0 return join('', @out);
1807 } # make_image_style
1808  
1809 =head2 need_to_generate_image
1810  
1811 Check if a thumbnail needs to be made (or rebuilt).
1812  
1813 =cut
1814 0 ▶sub need_to_generate_image {
Subroutine need_to_generate_image: not called CC=3 SCAR=24.8
Pod documented
1815 0 my $self = shift;
1816 0 my $dir_state = shift;
1817 0 my $img_state = shift;
1818 0 my %args = @_;
1819  
1820 0 ▶ if (!-f $args{check_image} or $self->{force_images})
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $args{"check_image"} or $self->{"force_images"}) 0 0 0
Condition0%0/3
not -f $args{"check_image"} or $self->{"force_images"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not -f $args{"check_image"} or $self->{"force_images"}
not -f $args{"check_image"} $self->{"force_images"}
Truth table0%0/3
not(A) or B
A= not -f $args{"check_image"}B= $self->{"force_images"}
ABresult
1X1
011
000
1821 {
1822 0 return 1;
1823 }
1824 0 return 0;
1825 } # need_to_generate_image
1826  
1827 =head2 index_needs_rebuilding
1828  
1829 Check to see if there are any new (or deleted) images or directories
1830 in this directory.
1831  
1832 =cut
1833 0 ▶sub index_needs_rebuilding {
Subroutine index_needs_rebuilding: not called CC=20 SCAR=60.4
Pod documented
1834 0 my $self = shift;
1835 0 my $dir_state = shift;
1836  
1837 # ------- Subdirs -------------
1838 # Need to check if any of the subdirs are new or deleted
1839
1840 0 my @subdirs = @{$dir_state->{subdirs}};
1841 0 my @dest_subdirs = ();
1842 0 my $dirh;
1843 0 opendir($dirh,$dir_state->{abs_out_dir});
1844 0 ▶ while (my $fn = readdir($dirh))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dirh)) 0 0 0
1845 {
1846 0 my $abs_fn = File::Spec->catfile($dir_state->{abs_out_dir}, $fn);
1847 0 ▶ if ($fn =~ /^\./ or $fn eq $self->{thumbdir})
Branch: 2 branches
BranchTrueFalseTotal
if ($fn =~ /^\./ or $fn eq $self->{'thumbdir'}) { } 0 0 0
elsif (-d $abs_fn) { } 0 0 0
Condition0%0/3
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fn =~ /^\./ or $fn eq $self->{"thumbdir"}
$fn =~ /^\./ $fn eq $self->{"thumbdir"}
Truth table0%0/3
A or B
A= $fn =~ /^\./B= $fn eq $self->{"thumbdir"}
ABresult
1X1
011
000
1848 {
1849 # skip
1850 }
1851 elsif (-d $abs_fn)
1852 {
1853 0 push @dest_subdirs, $fn;
1854 }
1855 }
1856 0 closedir($dirh);
1857  
1858 0 my %destdir_has_src = ();
1859 0 my %srcdir_has_dest = ();
1860 # initialise to false
1861 0 foreach my $sd ( @subdirs )
1862 {
1863 0 $srcdir_has_dest{$sd} = 0;
1864 }
1865 # Are there dest-dirs without src-dirs?
1866 0 foreach my $dsd ( @dest_subdirs )
1867 {
1868 0 ▶ if (exists $srcdir_has_dest{$dsd})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srcdir_has_dest{$dsd}) { } 0 0 0
1869 {
1870 0 $srcdir_has_dest{$dsd} = 1;
1871 0 $destdir_has_src{$dsd} = 1;
1872 }
1873 else
1874 {
1875 0 $self->debug(1, "GONE DIR: $dsd");
1876 0 $destdir_has_src{$dsd} = 0;
1877 0 return 1;
1878 }
1879 }
1880 # Are there src-dirs without dest-dirs?
1881 0 ▶ while (my ($key, $dir_exists) = each(%srcdir_has_dest))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %srcdir_has_dest) 0 0 0
1882 {
1883 0 ▶ if (!$dir_exists)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir_exists) 0 0 0
1884 {
1885 0 $self->debug(1, "NEW DIR: $key");
1886 0 return 1;
1887 }
1888 }
1889  
1890 # --------- Thumbnail Directory ----------
1891 0 my $thumb_dir = File::Spec->catdir($dir_state->{abs_out_dir}, $self->{thumbdir});
1892 0 my @pics = @{$dir_state->{files}};
1893 0 $self->debug(2, "dir: $thumb_dir");
1894  
1895 # if the thumbnail directory doesn't exist, then either all images
1896 # are new, or we don't have any images in this directory
1897 0 ▶ if (!-d $thumb_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $thumb_dir) 0 0 0
1898 {
1899 0 ▶ return (@pics ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
@pics ? : 0 0 0
1900 }
1901  
1902 # Read the thumbnail directory
1903 0 opendir($dirh,$thumb_dir);
1904 0 my @files = grep(!/^\.{1,2}$/, readdir($dirh));
1905 0 closedir($dirh);
1906  
1907 # check whether a picture has a thumbnail, and a thumbnail has a picture
1908 0 my %pic_has_tn = ();
1909 0 my %tn_has_pic = ();
1910  
1911 # initialize to false
1912 0 foreach my $pic ( @pics )
1913 {
1914 0 $pic_has_tn{$pic} = 0;
1915 }
1916  
1917 # Check each file to make sure it's a currently used thumbnail or image_page
1918 0 foreach my $file ( @files )
1919 {
1920 0 my $name = $file;
1921 0 ▶ if ($name =~ s/\.html$//)
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ s/\.html$//) { } 0 0 0
elsif ($name =~ /(.+)\.jpg$/i) { } 0 0 0
1922 {
1923 # change the last underscore to a dot
1924 0 $name =~ s/_([a-zA-Z0-9]+)$/.$1/;
1925 0 ▶ if (exists $pic_has_tn{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pic_has_tn{$name}) { } 0 0 0
1926 {
1927 0 $pic_has_tn{$name} = 1;
1928 0 $tn_has_pic{$name} = 1;
1929 }
1930 else
1931 {
1932 0 $tn_has_pic{$name} = 0;
1933 0 return 1;
1934 }
1935 }
1936 elsif ($name =~ /(.+)\.jpg$/i) {
1937 # Thumbnail?
1938 0 $name = $1;
1939 # change the last underscore to a dot
1940 0 $name =~ s/_([a-zA-Z0-9]+)$/.$1/;
1941 0 $self->debug(2, "thumb: $name");
1942 0 ▶ if (exists $pic_has_tn{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pic_has_tn{$name}) { } 0 0 0
1943 {
1944 0 $pic_has_tn{$name} = 1;
1945 0 $tn_has_pic{$name} = 1;
1946 }
1947 else
1948 {
1949 0 $tn_has_pic{$name} = 0;
1950 0 return 1;
1951 }
1952 }
1953 } # for each file
1954  
1955 # now check if there are pics without thumbnails
1956 0 ▶ while (my ($key, $tn_exists) = each(%pic_has_tn))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pic_has_tn) 0 0 0
1957 {
1958 0 ▶ if (!$tn_exists)
Branch: 1 branch
BranchTrueFalseTotal
unless ($tn_exists) 0 0 0
1959 {
1960 0 return 1;
1961 }
1962 }
1963  
1964 0 return 0;
1965 } # index_needs_rebuilding
1966  
1967 =head2 get_image_info
1968  
1969 Get the image information for an image. Returns a hash of
1970 information.
1971  
1972 %info = $self->get_image_info($image_file);
1973  
1974 =cut
1975 0 ▶sub get_image_info {
Subroutine get_image_info: not called CC=1 SCAR=6.9
Pod documented
1976 0 my $self = shift;
1977 0 my $img_file = shift;
1978  
1979 0 my $info = Image::ExifTool::ImageInfo($img_file);
1980 # add the basename
1981 0 my ($basename, $path, $suffix) = fileparse($img_file, qr/\.[^.]*/);
1982 0 $info->{file_basename} = $basename;
1983 0 return $info;
1984 } # get_image_info
1985  
1986 =head2 debug
1987  
1988 $self->debug($level, $message);
1989  
1990 Print a debug message (for debugging).
1991 Checks $self->{'debug_level'} to see if the message should be printed or
1992 not.
1993  
1994 =cut
1995 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
1996 0 my $self = shift;
1997 0 my $level = shift;
1998 0 my $message = shift;
1999  
2000 0 ▶ if ($level <= $self->{'debug_level'})
Branch: 1 branch
BranchTrueFalseTotal
if ($level <= $self->{"debug_level"}) 0 0 0
2001 {
2002 0 my $oh = \*STDERR;
2003 0 print $oh $message, "\n";
2004 }
2005 } # debug
2006  
2007 =head1 Private Methods
2008  
2009 Methods which may or may not be here in future.
2010  
2011 =head2 _whowasi
2012  
2013 For debugging: say who called this
2014  
2015 =cut
2016 0 ▶sub _whowasi { (caller(1))[3] . '()' }
Subroutine _whowasi: not called CC=1 SCAR=6.9
2017  
2018 =head1 REQUIRES
2019  
2020 Test::More
2021  
2022 =head1 INSTALLATION
2023  
2024 To install this module, run the following commands:
2025  
2026 perl Build.PL
2027 ./Build
2028 ./Build test
2029 ./Build install
2030  
2031 Or, if you're on a platform (like DOS or Windows) that doesn't like the
2032 "./" notation, you can do this:
2033  
2034 perl Build.PL
2035 perl Build
2036 perl Build test
2037 perl Build install
2038  
2039 In order to install somewhere other than the default, such as
2040 in a directory under your home directory, like "/home/fred/perl"
2041 go
2042  
2043 perl Build.PL --install_base /home/fred/perl
2044  
2045 as the first step instead.
2046  
2047 This will install the files underneath /home/fred/perl.
2048  
2049 You will then need to make sure that you alter the PERL5LIB variable to
2050 find the modules, and the PATH variable to find the script.
2051  
2052 Therefore you will need to change:
2053 your path, to include /home/fred/perl/script (where the script will be)
2054  
2055 PATH=/home/fred/perl/script:${PATH}
2056  
2057 the PERL5LIB variable to add /home/fred/perl/lib
2058  
2059 PERL5LIB=/home/fred/perl/lib:${PERL5LIB}
2060  
2061  
2062 =head1 SEE ALSO
2063  
2064 perl(1).
2065  
2066 =head1 BUGS
2067  
2068 Please report any bugs or feature requests to the author.
2069  
2070 =head1 AUTHOR
2071  
2072 Kathryn Andersen (RUBYKAT)
2073 perlkat AT katspace dot com
2074 http://www.katspace.org/tools
2075  
2076 =head1 COPYRIGHT AND LICENCE
2077  
2078 Copyright (c) 2006 by Kathryn Andersen
2079  
2080 This program is free software; you can redistribute it and/or modify it
2081 under the same terms as Perl itself.
2082  
2083  
2084 =cut
2085  
2086 4 1; # End of HTML::KhatGallery::Core
2087 __END__
↑ Summary KhatGallery.pm »