File Coverage

blib/lib/Snig/Collection.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 31 31 100.0


line stmt bran cond sub pod time code
1 2     2   174060 use v5.40;
  2         9  
2 2     2   14 use feature 'class';
  2         4  
  2         408  
3 2     2   21 no warnings "experimental::class";
  2         8  
  2         314  
4              
5             class Snig::Collection {
6              
7             our $VERSION = '1.001'; # VERSION
8             # ABSTRACT: Snig Collection Class
9             # PODNAME: Snig::Collection
10              
11 2     2   1191 use Path::Tiny qw(path);
  2         18682  
  2         191  
12 2     2   733 use Log::Any qw($log);
  2         11471  
  2         16  
13 2     2   5882 use Template;
  2         51221  
  2         111  
14 2     2   1250 use File::Copy qw(copy);
  2         16527  
  2         175  
15 2     2   1243 use File::Share qw(dist_file);
  2         83738  
  2         3291  
16              
17             field $sort_by :reader :param = 'created'; # TODO
18             field $input :reader :param;
19             field $name :reader :param;
20              
21             field %images;
22             field @sorted :reader;
23             field $zip_size;
24              
25             field $tt;
26              
27             ADJUST {
28             $input = path($input) if !ref($input);
29             $tt = Template->new(ABSOLUTE=>1, WRAPPER=>dist_file('Snig','wrapper.tt'));
30              
31             for my $file ( $self->input->children ) {
32             next unless $file->basename =~ /\.jpe?g$/i; # TODO allow other image types?
33             $log->debugf("Reading image %s", $file->basename);
34             my $image = Snig::Image->new(file => $file);
35             $images{$file->basename} = $image;
36             }
37              
38             @sorted = map { $_->[1] }
39             sort { $a->[0] cmp $b->[0] }
40             map { [ $_->$sort_by, $_ ] }
41             values %images;
42              
43             while (my ($i, $image) = each @sorted) {
44             my $prev = $i-1;
45             my $next = $i == $#sorted ? 0 : $i+1;
46             $image->set_chain($i+1, $sorted[$prev], $sorted[$next]);
47             }
48              
49             }
50              
51             method resize_images($output, $th_size, $detail_size, $force = 0) {
52             $log->infof("Resizing %i images", scalar @sorted);
53             for my $img (@sorted) {
54             $img->resize($output, $th_size, $detail_size, $force);
55             }
56             print "\n";
57             }
58              
59             method write_html_pages( $output ) {
60             $log->infof("Writing %i html pages", scalar @sorted);
61             for my $img (@sorted) {
62             $img->write_html_page($tt, $output, $self);
63             }
64             }
65              
66             method write_index( $output, $force_zip = 0 ) {
67             my $zip_file = $self->zip_file($output);
68             $log->infof("Generating zip archive %s and index", $zip_file);
69             copy(dist_file('Snig','snig.css'), $output);
70              
71             if (!$zip_file->is_file || $force_zip ) {
72             system('zip', '-r', $zip_file->stringify, $input->stringify);
73             }
74              
75             $tt->process(dist_file('Snig','index.tt'), {
76             collection=> {
77             name => $name,
78             images => \@sorted,
79             zip_file => $zip_file->basename,
80             zip_size => $zip_file->size_human( {format => "si"} ),
81             },
82             version => Snig->VERSION,
83             }, $output->child('index.html')->stringify) || die $tt->error;
84             }
85              
86             method zip_file($output) {
87             my $dir = lc($output->basename);
88             return $output->child($dir.'.zip');
89             }
90              
91             }
92              
93             __END__