]> www.average.org Git - mkgallery.git/blobdiff - mkgallery.pl
release 1.01
[mkgallery.git] / mkgallery.pl
index 5927e144567130db3092ed6ff3d9f5ea600465f3..5c02e0b83995e8fbbe765854689865f2e67603d2 100755 (executable)
@@ -33,25 +33,53 @@ use CGI qw/:html *table *Tr *center *div/;
 use Image::Info qw/image_info dim/;
 use Term::ReadLine;
 use Getopt::Long;
+use Encode;
+use encoding 'utf-8';
+binmode(STDOUT, ":utf8");
 
-use Image::Magick;
+my $haveimagick = eval { require Image::Magick; };
+{ package Image::Magick; }     # to make perl compiler happy
 
 my @sizes = (160, 640);
 
 ######################################################################
 
+my $incpath;
 my $debug = 0;
 my $asktitle = 0;
 my $noasktitle = 0;
 
-GetOptions('asktitle'=>\$asktitle,
+charset("utf-8");
+
+unless (GetOptions(
+               'help'=>\&help,
+               'incpath'=>\$incpath,
+               'asktitle'=>\$asktitle,
                'noasktitle'=>\$noasktitle,
-               'debug'=>\$debug);
+               'debug'=>\$debug)) {
+       &help;
+}
 
 my $term = new Term::ReadLine "Edit Title";
 
 FsObj->new(getcwd)->iterate;
 
+sub help {
+
+       print STDERR <<__END__;
+usage: $0 [options]
+ --help:        print help message and exit
+ --incpath:     do not try to find .include diretory upstream, use
+                specified path (absolute or relavive).  Use with causion.
+ --debug:       print a lot of debugging info to stdout as you run
+ --asktitle:    ask to edit album titles even if there are ".title" files
+ --noasktitle:  don't ask to enter album titles even where ".title"
+                files are absent.  Use partial directory names as titles.
+__END__
+
+       exit 1;
+}
+
 sub new {
        my $this = shift;
        my $class;
@@ -91,6 +119,10 @@ sub getinc {
        my $fullpath=shift;     # this is not a method
        my $depth=20;           # arbitrary max depth
 
+       if ($incpath) {
+               return $incpath."/.include";
+       }
+
        my $inc=".include";
        while ( ! -d $fullpath."/".$inc ) {
                $inc = "../".$inc;
@@ -108,6 +140,7 @@ sub iterate {
        my $fullpath .= $self->{-fullpath};
        print "iterate in dir $fullpath\n" if ($debug);
 
+       my $youngest=0;
        my @rdirlist;
        my @rimglist;
        my $D;
@@ -118,6 +151,8 @@ sub iterate {
        while (my $de = readdir($D)) {
                next if ($de =~ /^\./);
                my $child = $self->new($de);
+               my @stat = stat($child->{-fullpath});
+               $youngest = $stat[9] if ($youngest < $stat[9]);
                if ($child->isdir) {
                        push(@rdirlist,$child);
                } elsif ($child->isimg) {
@@ -169,6 +204,12 @@ sub iterate {
                $img->makeaux;
        }
 
+# no need to go beyond this point if the directory timestamp did not
+# change since we built index.html file last time.
+
+       my @istat = stat($self->{-fullpath}.'/index.html');
+       return unless ($youngest > $istat[9]);
+
 # 5. start building index.html for the directory
 
        $self->startindex;
@@ -331,19 +372,23 @@ sub isnewer {
 
 sub doscaling {
        my ($src,$dest,$factor,$w,$h) = @_;     # this is not a method
-       my $im = new Image::Magick;
-       my $err;
-       print "doscaling $src -> $dest by $factor\n" if ($debug);
-       $err = $im->Read($src);
-       unless ($err) {
-               $im->Scale(width=>$w*$factor,height=>$h*$factor);
-               $err=$im->Write($dest);
-               warn "ImageMagick: write \"$dest\": $err" if ($err);
-       } else {        # fallback to command-line tools
-               warn "ImageMagick: read \"$src\": $err";
+
+       my $err=1;
+       if ($haveimagick) {
+               my $im = new Image::Magick;
+               print "doscaling $src -> $dest by $factor\n" if ($debug);
+               if ($err = $im->Read($src)) {
+                       warn "ImageMagick: read \"$src\": $err";
+               } else {
+                       $im->Scale(width=>$w*$factor,height=>$h*$factor);
+                       $err=$im->Write($dest);
+                       warn "ImageMagick: write \"$dest\": $err" if ($err);
+               }
+               undef $im;
+       }
+       if ($err) {     # fallback to command-line tools
                system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
        }
-       undef $im;
 }
 
 sub makeaux {
@@ -356,100 +401,115 @@ sub makeaux {
        my $title = $self->{-info}->{'Comment'};
        $title = $name unless ($title);
 
-       print "slide: \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
+       print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
 
        # slideshow
        for my $refresh('static', 'slide') {
                my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
-               my $imgsrc = '../'.$self->{$sizes[1]};
-               my $fwdref;
-               my $bakref;
-               if ($nref) {
-                       $fwdref = sprintf("%s-%s.html",$nref,$refresh);
-               } else {
-                       $fwdref = '../index.html';
-               }
-               if ($pref) {
-                       $bakref = sprintf("%s-%s.html",$pref,$refresh);
-               } else {
-                       $bakref = '../index.html';
-               }
-               my $toggleref;
-               my $toggletext;
-               if ($refresh eq 'slide') {
-                       $toggleref=sprintf("%s-static.html",$name);
-                       $toggletext = 'Stop!';
-               } else {
-                       $toggleref=sprintf("%s-slide.html",$name);
-                       $toggletext = 'Play-&gt;';
-               }
-               my $F;
-               unless (open($F,'>'.$fn)) {
-                       warn "cannot open \"$fn\": $!";
-                       next;
-               }
-               if ($refresh eq 'slide') {
-                       print $F start_html(-title=>$title,
+               if (isnewer($self->{-fullpath},$fn)) {
+                       my $imgsrc = '../'.$self->{$sizes[1]};
+                       my $fwdref;
+                       my $bakref;
+                       if ($nref) {
+                               $fwdref = sprintf("%s-%s.html",$nref,$refresh);
+                       } else {
+                               $fwdref = '../index.html';
+                       }
+                       if ($pref) {
+                               $bakref = sprintf("%s-%s.html",$pref,$refresh);
+                       } else {
+                               $bakref = '../index.html';
+                       }
+                       my $toggleref;
+                       my $toggletext;
+                       if ($refresh eq 'slide') {
+                               $toggleref=sprintf("%s-static.html",$name);
+                               $toggletext = 'Stop!';
+                       } else {
+                               $toggleref=sprintf("%s-slide.html",$name);
+                               $toggletext = 'Play-&gt;';
+                       }
+                       my $F;
+                       unless (open($F,'>'.$fn)) {
+                               warn "cannot open \"$fn\": $!";
+                               next;
+                       }
+                       binmode($F, ":utf8");
+                       if ($refresh eq 'slide') {
+                               print $F start_html(
+                                       -encoding=>"utf-8",
+                                       -title=>$title,
                                        -bgcolor=>"#808080",
                                        -head=>meta({-http_equiv=>'Refresh',
                                                -content=>"3; url=$fwdref"}),
                                        -style=>{-src=>$inc."gallery.css"},
-                               ),"\n";
-                                       
-               } else {
-                       print $F start_html(-title=>$title,
+                                       ),"\n";
+                                               
+                       } else {
+                               print $F start_html(-title=>$title,
+                                       -encoding=>"utf-8",
                                        -bgcolor=>"#808080",
                                        -style=>{-src=>$inc."gallery.css"},
-                               ),"\n";
+                                       ),"\n";
+                       }
+                       print $F start_center,"\n",
+                               h1($title),"\n",
+                               start_table({-class=>'navi'}),start_Tr,"\n",
+                               td(a({-href=>"../index.html"},"Index")),"\n",
+                               td(a({-href=>$bakref},"&lt;&lt;Prev")),"\n",
+                               td(a({-href=>$toggleref},$toggletext)),"\n",
+                               td(a({-href=>$fwdref},"Next&gt;&gt;")),"\n",
+                               end_Tr,
+                               end_table,"\n",
+                               table({-class=>'picframe'},
+                                       Tr(td(img({-src=>$imgsrc})))),"\n",
+                               end_center,"\n",
+                               end_html,"\n";
+                       close($F);
+               }
+       }
+
+       # info html
+       my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
+       if (isnewer($self->{-fullpath},$fn)) {
+               my $F;
+               unless (open($F,'>'.$fn)) {
+                       warn "cannot open \"$fn\": $!";
+                       return;
                }
-               print $F start_center,"\n",
+               my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
+               print $F start_html(-title=>$title,
+                               -encoding=>"utf-8",
+                               -style=>{-src=>$inc."gallery.css"},),"\n",
+                       start_center,"\n",
                        h1($title),"\n",
-                       start_table({-class=>'navi'}),start_Tr,"\n",
-                       td(a({-href=>"../index.html"},"Index")),"\n",
-                       td(a({-href=>$bakref},"&lt;&lt;Prev")),"\n",
-                       td(a({-href=>$toggleref},$toggletext)),"\n",
-                       td(a({-href=>$fwdref},"Next&gt;&gt;")),"\n",
-                       end_Tr,
-                       end_table,"\n",
-                       table({-class=>'picframe'},
-                               Tr(td(img({-src=>$imgsrc})))),"\n",
+                       table({-class=>'ipage'},
+                               Tr(td(img({-src=>$imgsrc})),
+                                       td($self->infotable))),
+                       a({-href=>'../index.html'},'Index'),"\n",
                        end_center,"\n",
                        end_html,"\n";
                close($F);
        }
-       my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
-       my $F;
-       unless (open($F,'>'.$fn)) {
-               warn "cannot open \"$fn\": $!";
-               return;
-       }
-
-       # info html
-       my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
-       print $F start_html(-title=>$title,
-                               -style=>{-src=>$inc."gallery.css"},),"\n",
-               start_center,"\n",
-               h1($title),"\n",
-               table({-class=>'ipage'},Tr(td(img({-src=>$imgsrc})),td($self->infotable))),
-               a({-href=>'../index.html'},'Index'),"\n",
-               end_center,"\n",
-               end_html,"\n";
-       close($F);
 }
 
 sub startindex {
        my $self = shift;
        my $fn = $self->{-fullpath}.'/index.html';
+       my $block = $self->{-fullpath}.'/.noindex';
+       $fn = '/dev/null' if ( -f $block );
        my $IND;
        unless (open($IND,'>'.$fn)) {
                warn "cannot open $fn: $!";
                return;
        }
+       binmode($IND, ":utf8");
        $self->{-IND} = $IND;
 
        my $inc = $self->{-inc};
        my $title = $self->{-title};
        print $IND start_html(-title => $title,
+                       -encoding=>"utf-8",
                        -style=>{-src=>[$inc."gallery.css",
                                        $inc."lightbox.css"]},
                        -script=>[{-code=>"var incPrefix='$inc';"},
@@ -529,13 +589,13 @@ sub img_entry {
                end_div,"\n";
 
        print $IND table({-class=>'slide'},Tr(td(
-               a({-href=>".html/$name-info.html",
+               a({-href=>".html/$name-info.html",-title=>'Image Info',
                        -onClick=>"return showIbox('$name');"},$title),
                br,
                a({-href=>$medium,-rel=>"lightbox",-title=>$title},
                        img({-src=>$thumb})),
                br,
-               a({-href=>$name},"($w x $h)"),
+               a({-href=>$name,-title=>'Original Image'},"($w x $h)"),
                br))),"\n";
 }