From 06345572c2129c1ded3e3890e44adee804d4e99b Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 21 May 2011 11:39:26 +0200 Subject: Code cleanup --- bin/dthumb | 23 +-- lib/App/Dthumb.pm | 438 ++++++++++++++++++++++------------------------ lib/App/Dthumb/Data.pm.PL | 6 +- t/20-app-dthumb.t | 4 +- 4 files changed, 226 insertions(+), 245 deletions(-) diff --git a/bin/dthumb b/bin/dthumb index 60eba92..04dbd1c 100755 --- a/bin/dthumb +++ b/bin/dthumb @@ -15,8 +15,11 @@ use Time::Progress; STDERR->autoflush(1); my $opt = {}; -my $id = 1; +my $id = 1; +our $VERSION = '0.2'; + +#<<< GetOptions( $opt, qw{ @@ -31,20 +34,19 @@ GetOptions( version|v }, ) or die("Please see perldoc -F $0\n"); - -my $dthumb = App::Dthumb->new(%{$opt}); +#>>> +my $dthumb = App::Dthumb->new( %{$opt} ); my $timer = Time::Progress->new(); -if ($opt->{version}) { - say "dthumb version " . $dthumb->version(); +if ( $opt->{version} ) { + say "dthumb version $VERSION"; exit 0; } -if ($opt->{help}) { - exec('perldoc', '-F', $0); +if ( $opt->{help} ) { + exec( 'perldoc', '-F', $0 ); die("Please see 'perldoc -F $0' or 'man dthumb'\n"); } - $dthumb->read_directories(); $dthumb->create_files(); $dthumb->delete_old_thumbnails(); @@ -54,10 +56,9 @@ $timer->attr( max => scalar $dthumb->get_files(), ); -for my $file ($dthumb->get_files()) { +for my $file ( $dthumb->get_files() ) { print STDERR $timer->report( - "\r\e[KCreating Thumbnails: %p done, %L elapsed, %E remaining", - $id++, + "\r\e[KCreating Thumbnails: %p done, %L elapsed, %E remaining", $id++, ); $dthumb->create_thumbnail_html($file); diff --git a/lib/App/Dthumb.pm b/lib/App/Dthumb.pm index f7b6d86..a287f32 100755 --- a/lib/App/Dthumb.pm +++ b/lib/App/Dthumb.pm @@ -1,140 +1,36 @@ package App::Dthumb; - -=head1 NAME - -App::Dthumb - Generate thumbnail index for a set of images - -=head1 SYNOPSIS - - use App::Dthumb; - use Getopt::Long qw(:config no_ignore_case); - - my $opt = {}; - - GetOptions( - $opt, - qw{ - help|h - size|d=i - spacing|s=f - no-lightbox|L - no-names|n - quality|q=i - version|v - }, - ); - - my $dthumb = App::Dthumb->new($opt); - $dthumb->run(); - -=head1 VERSION - -This manual documents App::Dthumb version 0.2 - -=cut - - use strict; use warnings; use autodie; use 5.010; -use base 'Exporter'; - use App::Dthumb::Data; use Cwd; use Image::Imlib2; -our @EXPORT_OK = (); our $VERSION = '0.2'; - -=head1 METHODS - -=head2 new($conf) - -Returns a new B object. As you can see in the SYNOPSIS, $conf is -designed so that it can be directly fed by B. - -Valid hash keys are: - -=over - -=item B => I - -Set base directory for image reading, data creation etc. - -Default: F<.> (current working directory) - -=item B => I - -Set name of the html index file - -Default: F - -=item B => I - -Include and use javascript lightbox code - -Default: true - -=item B => I - -If true, unconditionally recreate all thumbnails. - -Default: false - -=item B => I - -Maximum image size in pixels, either width or height (depending on image -orientation) - -Default: 200 - -=item B => I - -Spacing between image boxes. 1.0 means each box is exactly as wide as the -maximum image width (see B), 1.1 means slightly larger, et cetera - -Default: 1.1 - -=item B => I - -Show image name below thumbnail - -Default: true - -=item B => I<0 .. 100> - -Thumbnail image quality - -Default: 75 - -=back - -=cut - - sub new { - my ($obj, %conf) = @_; + my ( $obj, %conf ) = @_; + my $ref = {}; - $conf{quality} //= 75; - $conf{recreate} //= 0; - $conf{size} //= 200; - $conf{spacing} //= 1.1; - $conf{title} //= (split(qr{/}, cwd()))[-1]; + $conf{quality} //= 75; + $conf{recreate} //= 0; + $conf{size} //= 200; + $conf{spacing} //= 1.1; + $conf{title} //= ( split( qr{/}, cwd() ) )[-1]; $conf{file_index} //= 'index.xhtml'; - $conf{dir_images} //= '.'; + $conf{dir_images} //= q{.}; $conf{dir_data} = "$conf{dir_images}/.dthumb"; $conf{dir_thumbs} = "$conf{dir_images}/.thumbs"; # helpers to directly pass GetOptions results - $conf{lightbox} //= ( $conf{'no-lightbox'} ? 0 : 1 ); - $conf{names} //= ( $conf{'no-names'} ? 0 : 1 ); + $conf{lightbox} //= ( $conf{'no-lightbox'} ? 0 : 1 ); + $conf{names} //= ( $conf{'no-names'} ? 0 : 1 ); $ref->{config} = \%conf; @@ -146,80 +42,66 @@ sub new { height => $conf{size} * $conf{spacing} . 'px', ); - if ($conf{lightbox}) { - $ref->{data}->set_vars( - lightbox => $ref->{data}->get('html_lightbox.dthumb'), - ); + if ( $conf{lightbox} ) { + $ref->{data} + ->set_vars( lightbox => $ref->{data}->get('html_lightbox.dthumb'), ); } $ref->{html} = $ref->{data}->get('html_start.dthumb'); - return bless($ref, $obj); + return bless( $ref, $obj ); } -=head2 read_directories - -Read in a list of all image files in the current directory and all files in -F<.thumbs> which do not have a corresponding full-size image. - -=cut - - sub read_directories { my ($self) = @_; + my $thumbdir = $self->{config}->{dir_thumbs}; my $imgdir = $self->{config}->{dir_images}; my $dh; - my (@files, @old_thumbs); + my ( @files, @old_thumbs ); - opendir($dh, $imgdir); + opendir( $dh, $imgdir ); - for my $file (readdir($dh)) { - if (-f "${imgdir}/${file}" and $file =~ qr{ \. (png | jp e? g) $ }iox) { - push(@files, $file); + for my $file ( readdir($dh) ) { + if ( -f "${imgdir}/${file}" + and $file =~ qr{ [.] (png | jp e? g) $ }iox ) + { + push( @files, $file ); } } closedir($dh); - if (-d $thumbdir) { - opendir($dh, $thumbdir); - for my $file (readdir($dh)) { - if ($file =~ qr{^ [^.] }ox and not -f "${imgdir}/${file}") { - push(@old_thumbs, $file); + if ( -d $thumbdir ) { + opendir( $dh, $thumbdir ); + for my $file ( readdir($dh) ) { + if ( $file =~ qr{^ [^.] }ox and not -f "${imgdir}/${file}" ) { + push( @old_thumbs, $file ); } } closedir($dh); } - @{$self->{files}} = sort { lc($a) cmp lc($b) } @files; - @{$self->{old_thumbnails}} = @old_thumbs; -} - - -=head2 create_files - -Makes sure the F<.thumbs> directory exists. - -Also, if lightbox is enabled (which is the default), creates the F<.dthumb> -directory and fills it with all required files. - -=cut + @{ $self->{files} } = sort { lc($a) cmp lc($b) } @files; + @{ $self->{old_thumbnails} } = @old_thumbs; + return; +} sub create_files { my ($self) = @_; + my $thumbdir = $self->{config}->{dir_thumbs}; my $datadir = $self->{config}->{dir_data}; my @files; - if (not -d $thumbdir) { + if ( not -d $thumbdir ) { mkdir($thumbdir); } - if (not -d $datadir) { + if ( not -d $datadir ) { mkdir($datadir); } - if ($self->{config}->{lightbox}) { + if ( $self->{config}->{lightbox} ) { @files = $self->{data}->list_archived(); } else { @@ -227,152 +109,249 @@ sub create_files { } for my $file (@files) { - open(my $fh, '>', "${datadir}/${file}"); + open( my $fh, '>', "${datadir}/${file}" ); print {$fh} $self->{data}->get($file); close($fh); } -} - - -=head2 delete_old_thumbnails - -Unlink all no longer required thumbnails (as previously found by -B). - -=cut + return; +} sub delete_old_thumbnails { my ($self) = @_; + my $thumbdir = $self->{config}->{dir_thumbs}; - for my $file (@{$self->{old_thumbnails}}) { + for my $file ( @{ $self->{old_thumbnails} } ) { unlink("${thumbdir}/${file}"); } -} - - -=head2 get_files - -Returns an array of all image files found by B. - -=cut + return; +} sub get_files { my ($self) = @_; - return @{$self->{files}}; + return @{ $self->{files} }; } - -=head2 create_thumbnail_html($file) - -Append the necessary lines for $file to the HTML. - -=cut - - sub create_thumbnail_html { - my ($self, $file) = @_; + my ( $self, $file ) = @_; + my $div_width = $self->{config}->{size} * $self->{config}->{spacing}; - my $div_height = $div_width + ($self->{config}->{names} ? 10 : 0); + my $div_height = $div_width + ( $self->{config}->{names} ? 10 : 0 ); $self->{html} .= "
\n"; $self->{html} .= sprintf( "\t\n" - . "\t\t\"%s\"\n", + . "\t\t\"%s\"\n", ($file) x 2, $self->{config}->{dir_thumbs}, ($file) x 2, ); - if ($self->{config}->{names}) { + if ( $self->{config}->{names} ) { $self->{html} .= sprintf( - "\t
\n" - . "\t%s\n", + "\t
\n" . "\t%s\n", 'text-decoration: none', ($file) x 2, ); } $self->{html} .= "
\n"; -} - - -=head2 create_thumbnail_image($file) - -Load F<$file> and save a resized version in F<.thumbs/$file>. Skips thumbnail -generation if the thumbnail already exists and has a more recent mtime than -the original file. - -=cut + return; +} sub create_thumbnail_image { - my ($self, $file) = @_; - my $thumbdir = $self->{config}->{dir_thumbs}; + my ( $self, $file ) = @_; + + my $thumbdir = $self->{config}->{dir_thumbs}; my $thumb_dim = $self->{config}->{size}; - if ( - -e "${thumbdir}/${file}" - and not $self->{config}->{recreate} - and (stat($file))[9] <= (stat("${thumbdir}/${file}"))[9] - ) { + if ( -e "${thumbdir}/${file}" + and not $self->{config}->{recreate} + and ( stat($file) )[9] <= ( stat("${thumbdir}/${file}") )[9] ) + { return; } my $image = Image::Imlib2->load($file); - my ($dx, $dy) = ($image->width(), $image->height()); + my ( $dx, $dy ) = ( $image->width(), $image->height() ); my $thumb = $image; - if ($dx > $thumb_dim or $dy > $thumb_dim) { - if ($dx > $dy) { - $thumb = $image->create_scaled_image($thumb_dim, 0); + if ( $dx > $thumb_dim or $dy > $thumb_dim ) { + if ( $dx > $dy ) { + $thumb = $image->create_scaled_image( $thumb_dim, 0 ); } else { - $thumb = $image->create_scaled_image(0, $thumb_dim); + $thumb = $image->create_scaled_image( 0, $thumb_dim ); } } - $thumb->set_quality($self->{config}->{quality}); + $thumb->set_quality( $self->{config}->{quality} ); $thumb->save("${thumbdir}/${file}"); -} - - -=head2 write_out_html - -Write the cached HTML data to F. - -=cut + return; +} sub write_out_html { my ($self) = @_; $self->{html} .= $self->{data}->get('html_end.dthumb'); - open(my $fh, '>', $self->{config}->{file_index}); + open( my $fh, '>', $self->{config}->{file_index} ); print {$fh} $self->{html}; close($fh); + + return; } +1; -=head2 version +__END__ -Return B version string. +=head1 NAME -=cut +App::Dthumb - Generate thumbnail index for a set of images +=head1 SYNOPSIS -sub version { - return $VERSION; -} + use App::Dthumb; + use Getopt::Long qw(:config no_ignore_case); + + my $opt = {}; + + GetOptions( + $opt, + qw{ + help|h + size|d=i + spacing|s=f + no-lightbox|L + no-names|n + quality|q=i + version|v + }, + ); + + my $dthumb = App::Dthumb->new($opt); + $dthumb->run(); -1; +=head1 VERSION -__END__ +This manual documents App::Dthumb version 0.2 + +=head1 DESCRIPTION + +App::Dthumb does all the backend work for dthumb(1). + +=head1 METHODS + +=over + +=item $dthumb = App::Dthumb->new(I<%conf>) + +Returns a new B object. As you can see in the SYNOPSIS, I<%conf> is +designed so that it can be directly passed from B. + +Valid hash keys are: + +=over + +=item B => I + +Set base directory for image reading, data creation etc. + +Default: F<.> (current working directory) + +=item B => I + +Set name of the html index file + +Default: F + +=item B => I + +Include and use javascript lightbox code + +Default: true + +=item B => I + +If true, unconditionally recreate all thumbnails. + +Default: false + +=item B => I + +Maximum image size in pixels, either width or height (depending on image +orientation) + +Default: 200 + +=item B => I + +Spacing between image boxes. 1.0 means each box is exactly as wide as the +maximum image width (see B), 1.1 means slightly larger, et cetera + +Default: 1.1 + +=item B => I + +Show image name below thumbnail + +Default: true + +=item B => I<0 .. 100> + +Thumbnail image quality + +Default: 75 + +=back + +=item $dthumb->read_directories() + +Read in a list of all image files in the current directory and all files in +F<.thumbs> which do not have a corresponding full-size image. + +=item $dthumb->create_files() + +Makes sure the F<.thumbs> directory exists. + +Also, if lightbox is enabled (which is the default), creates the F<.dthumb> +directory and fills it with all required files. + +=item $dthumb->delete_old_thumbnails() + +Unlink all no longer required thumbnails (as previously found by +B). + +=item $dthumb->get_files() + +Returns an array of all image files found by B. + +=item $dthumb->create_thumbnail_html($file) + +Append the necessary lines for $file to the HTML. + +=item $dthumb->create_thumbnail_image($file) + +Load F<$file> and save a resized version in F<.thumbs/$file>. Skips thumbnail +generation if the thumbnail already exists and has a more recent mtime than +the original file. + +=item $dthumb->write_out_html() + +Write the cached HTML data to F. + +=back + +=head1 DIAGNOSTICS + +None yet. =head1 DEPENDENCIES @@ -384,6 +363,11 @@ __END__ =back +=head1 BUGS AND LIMITATIONS + +So far, this module is pretty focused on working with dthumb(1). It is +planned to make it somewhat more universal. + =head1 AUTHOR Copyright (C) 2009-2011 by Daniel Friesel Ederf@chaosdorf.deE diff --git a/lib/App/Dthumb/Data.pm.PL b/lib/App/Dthumb/Data.pm.PL index a0fb4f5..13b244b 100644 --- a/lib/App/Dthumb/Data.pm.PL +++ b/lib/App/Dthumb/Data.pm.PL @@ -13,7 +13,7 @@ opendir(my $share_dh, 'share'); print {$out_fh} ; for my $file (readdir($share_dh)) { - if (substr($file, 0, 1) eq '.') { + if (substr($file, 0, 1) eq q{.}) { next; } @@ -21,7 +21,7 @@ for my $file (readdir($share_dh)) { my $content = do { local $/ = undef; <$fh> }; close($fh); - if ($file =~ qr{ \. (png | gif) $ }ox) { + if ($file =~ qr{ [.] (png | gif) $ }ox) { $content = encode_base64($content); } @@ -67,12 +67,10 @@ This manual documents B version 0.2 use strict; use warnings; -use base 'Exporter'; use Data::Section -setup; use MIME::Base64 qw(decode_base64); -our @EXPORT_OK = (); our $VERSION = '0.2'; diff --git a/t/20-app-dthumb.t b/t/20-app-dthumb.t index 5f3479d..ec9eb5a 100755 --- a/t/20-app-dthumb.t +++ b/t/20-app-dthumb.t @@ -4,7 +4,7 @@ use warnings; use 5.010; use autodie; -use Test::More tests => 17; +use Test::More tests => 16; use_ok('App::Dthumb'); @@ -33,8 +33,6 @@ is($dthumb->{config}->{lightbox}, 0, 'Lightbox disabled'); $dthumb = App::Dthumb->new('no-names' => 1); is($dthumb->{config}->{names}, 0, 'Image names disabled'); -is($dthumb->version(), $App::Dthumb::VERSION, '$dthumb->version()'); - $dthumb = App::Dthumb->new(); @{$dthumb->{files}} = qw(a.png b.png c.png d.jpg); -- cgit v1.2.3