0d649c05f194872a85e745a37fb7b7ca4ce32578
[mkgallery.git] / mkgallery.pl
1 #!/usr/bin/perl
2
3 my $version='$Id$';
4
5 # Recursively create image gallery index and slideshow wrappings.
6 # Makes use of modified "slideshow" javascript by Samuel Birch
7 # http://www.phatfusion.net/slideshow/
8
9 # Copyright (c) 2006-2008 Eugene G. Crosser
10
11 #  This software is provided 'as-is', without any express or implied
12 #  warranty.  In no event will the authors be held liable for any damages
13 #  arising from the use of this software.
14 #
15 #  Permission is granted to anyone to use this software for any purpose,
16 #  including commercial applications, and to alter it and redistribute it
17 #  freely, subject to the following restrictions:
18 #
19 #  1. The origin of this software must not be misrepresented; you must not
20 #     claim that you wrote the original software. If you use this software
21 #     in a product, an acknowledgment in the product documentation would be
22 #     appreciated but is not required.
23 #  2. Altered source versions must be plainly marked as such, and must not be
24 #     misrepresented as being the original software.
25 #  3. This notice may not be removed or altered from any source distribution.
26
27 package FsObj;
28
29 use strict;
30 use Carp;
31 use POSIX qw/getcwd strftime/;
32 use HTTP::Date;
33 use CGI qw/:html *table *Tr *td *center *div *Link/;
34 use Image::Info qw/image_info dim/;
35 use Term::ReadLine;
36 use Getopt::Long;
37 use Encode;
38 #use encoding 'utf-8';
39 binmode(STDOUT, ":utf8");
40
41 my $haveimagick = eval { require Image::Magick; };
42 { package Image::Magick; }      # to make perl compiler happy
43
44 my $haverssxml = eval { require XML::RSS; };
45 { package XML::RSS; }           # to make perl compiler happy
46
47 my $havegeoloc = eval { require Image::ExifTool::Location; };
48 { package Image::ExifTool::Location; }  # to make perl compiler happy
49
50 my @sizes = (160, 640, 1600);
51 my $incdir = ".gallery2";
52
53 ######################################################################
54
55 my $incpath;
56 my $rssobj;
57 my $debug = 0;
58 my $asktitle = 0;
59 my $noasktitle = 0;
60 my $rssfile = "";
61
62 charset("utf-8");
63
64 unless (GetOptions(
65                 'help'=>\&help,
66                 'incpath'=>\$incpath,
67                 'asktitle'=>\$asktitle,
68                 'noasktitle'=>\$noasktitle,
69                 'rssfile=s'=>\$rssfile,
70                 'debug'=>\$debug)) {
71         &help;
72 }
73
74 if ($rssfile && ! $haverssxml) {
75         print STDERR "You need to get XML::RSS from CPAN to use --rssfile\n";
76         exit 1;
77 }
78
79 my $term = new Term::ReadLine "Edit Title";
80
81 FsObj->new(getcwd)->iterate;
82 if ($rssobj) { $rssobj->{'rss'}->save($rssobj->{'file'}); }
83
84 sub help {
85
86         print STDERR <<__END__;
87 usage: $0 [options]
88  --help:        print help message and exit
89  --incpath:     do not try to find .gallery2 diretory upstream, use
90                 specified path (absolute or relavive).  Use with causion.
91  --debug:       print a lot of debugging info to stdout as you run
92  --asktitle:    ask to edit album titles even if there are ".title" files
93  --noasktitle:  don't ask to enter album titles even where ".title"
94                 files are absent.  Use partial directory names as titles.
95  --rssfile=...: build RSS feed for newly added "albums", give name of rss file
96 __END__
97
98         exit 1;
99 }
100
101 sub new {
102         my $this = shift;
103         my $class;
104         my $self;
105         if (ref($this)) {
106                 $class = ref($this);
107                 my $parent = $this;
108                 my $name = shift;
109                 $self = {
110                                 -parent=>$parent,
111                                 -root=>$parent->{-root},
112                                 -toppath=>$parent->{-toppath},
113                                 -depth=>$parent->{-depth}+1,
114                                 -base=>$name,
115                                 -fullpath=>$parent->{-fullpath}.'/'.$name,
116                                 -relpath=>$parent->{-relpath}.$name.'/',
117                                 -inc=>'../'.$parent->{-inc},
118                         };
119         } else {
120                 $class = $this;
121                 my $root=shift;
122                 $self = {
123                                 -root=>$root,
124                                 -fullpath=>$root,
125                         };
126                 # fill in -inc, -rss, -relpath
127                 initpaths($self); # we are not blessed yet, so cheat.
128         }
129         bless $self, $class;
130         if ($debug) {
131                 print "new $class:\n";
132                 foreach my $k(keys %$self) {
133                         print "\t$k\t=\t$self->{$k}\n";
134                 }
135         }
136         return $self;
137 }
138
139 sub initpaths {
140         my $self=shift;         # this is not a method but we cheat
141         my $depth=20;           # arbitrary max depth
142         my $fullpath=$self->{-fullpath};
143         my $inc;
144         my $relpath;
145
146         if ($incpath) {
147                 $inc = $incpath;
148                 $inc .= '/' unless ($inc =~ m%/$%);
149         } else {
150                 $inc="";
151                 while ( ! -d $fullpath."/".$inc."/".$incdir ) {
152                         $inc = "../".$inc;
153                         last unless ($depth-- > 0);
154                 }
155         }
156         if ($depth > 0) {
157                 $self->{-inc} = $inc;
158                 my $dp=0;
159                 my $pos;
160                 for ($pos=index($inc,'/');$pos>=0;
161                                         $pos=index($inc,'/',$pos+1)) {
162                         $dp++;
163                 }
164                 $self->{-depth} = $dp;
165                 for ($pos=length($fullpath);$dp>0 && $pos>0;
166                                         $pos=rindex($fullpath,'/',$pos-1)) {
167                         $dp--;
168                 }
169                 my $relpath = substr($fullpath,$pos);
170                 $relpath =~ s%^/%%;
171                 $relpath .= '/' if ($relpath);
172                 $self->{-relpath} = $relpath;
173                 $self->{-toppath} = substr($fullpath,0,$pos);
174                 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
175                 initrss($self);
176         } else {
177                 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
178                 $self->{-rss} = '';
179                 $self->{-relpath} = '';
180                 $self->{-depth} = 0;
181         }
182 }
183
184 sub initrss {
185         my $self=shift;         # this is not a method but we cheat
186         my $fullpath=$self->{-fullpath};
187         my $toppath=$self->{-toppath};
188         my $inc=$self->{-inc}.$incdir.'/';
189         my $conffile=$toppath.'/'.$incdir.'/rss.conf';
190         my $CONF;
191
192         if ($rssfile) {
193                 if (open($CONF,">".$conffile)) {
194                         print $CONF "file: ",$rssfile,"\n";
195                         close($CONF);
196                 } else {
197                         print STDERR "could not open $conffile: $!\n";
198                 }
199         } else {
200                 if (open($CONF,$conffile)) {
201                         my $ln=<$CONF>;
202                         close($CONF);
203                         chop $ln;
204                         my ($k,$v)=split(':', $ln);
205                         $k =~ s/^\s*//;
206                         $k =~ s/\s*$//;
207                         $v =~ s/^\s*//;
208                         $v =~ s/\s*$//;
209                         if ($k eq 'file') {
210                                 $rssfile=$v;
211                         }
212                 }
213         }
214
215         return unless ($rssfile);
216
217         $rssobj->{'file'} = $self->{-toppath}.'/'.$rssfile;
218         $rssobj->{'rss'} = new XML::RSS (version=>'2.0');
219         if ( -f $rssobj->{'file'} ) {
220                 $rssobj->{'rss'}->parsefile($rssobj->{'file'});
221                 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
222                 while ($itemstodel-- > 0) {
223                         pop(@{$rssobj->{'rss'}->{'items'}})
224                 }
225                 $rssobj->{'rss'}->save($rssobj->{'file'});
226         } else {
227                 my $link;
228                 my $p1;
229                 my $p2;
230                 for ($p1=0,$p2=length($toppath);
231                                 substr($rssfile,$p1,3) eq '../' && $p2>0;
232                                 $p1+=3,$p2=rindex($toppath,'/',$p2-1)) {;}
233                 $link=substr($toppath,$p2);
234                 $link =~ s%^/%%;
235                 $link .= '/' if ($link);
236                 while (($p1=index($rssfile,'/',$p1+1)) >= 0) {
237                         $link = '../'.$link;
238                 }
239                 
240                 $rssobj->{'rss'}->channel(
241                         title=>'Gallery',
242                         link=>$link,
243                         description=>'Gallery Feed',
244                         #language=>$language,
245                         #rating=>$rating,
246                         #copyright=>$copyright,
247                         #pubDate=>$pubDate,
248                         #lastBuildDate=>$lastBuild,
249                         #docs=>$docs,
250                         #managingEditor=>$editor,
251                         #webMaster=>$webMaster
252                 );
253                 $rssobj->{'rss'}->save($rssobj->{'file'});
254         }
255         $self->{-rss} = $rssobj->{'rss'};
256 }
257
258 sub iterate {
259         my $self = shift;
260         my $fullpath .= $self->{-fullpath};
261         print "iterate in dir $fullpath\n" if ($debug);
262
263         my $youngest=0;
264         my @rdirlist;
265         my @rimglist;
266         my $D;
267         unless (opendir($D,$fullpath)) {
268                 warn "cannot opendir $fullpath: $!";
269                 return;
270         }
271         while (my $de = readdir($D)) {
272                 next if ($de =~ /^\./);
273                 my $child = $self->new($de);
274                 my @stat = stat($child->{-fullpath});
275                 $youngest = $stat[9] if ($youngest < $stat[9]);
276                 if ($child->isdir) {
277                         push(@rdirlist,$child);
278                 } elsif ($child->isimg) {
279                         push(@rimglist,$child);
280                 }
281         }
282         closedir($D);
283         my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
284         undef @rdirlist; # inplace sorting would be handy here
285         my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
286         undef @rimglist; # optimize away unsorted versions
287         $self->{-firstimg} = $imglist[0];
288
289         print "Dir: $self->{-fullpath}\n" if ($debug);
290
291 # 1. first of all, fill title for this directory and create hidden subdirs
292
293         $self->initdir;
294
295 # 2. recurse into subdirectories to get their titles filled
296 #    before we start writing out subalbum list
297
298         foreach my $dir(@dirlist) {
299                 $dir->iterate;
300         }
301
302 # 3. iterate through images to build cross-links,
303
304         my $previmg = undef;
305         foreach my $img(@imglist) {
306                 # list-linking must be done before generating
307                 # aux html because aux pages rely on prev/next refs
308                 if ($previmg) {
309                         $previmg->{-nextimg} = $img;
310                         $img->{-previmg} = $previmg;
311                 }
312                 $previmg=$img;
313         }
314
315 # 4. create scaled versions and aux html pages
316
317         foreach my $img(@imglist) {
318                 # scaled versions must be generated before aux html
319                 # and main image index because they both rely on
320                 # refs to scaled images and they may be just original
321                 # images, this is not known before we try scaling.
322                 $img->makescaled;
323                 # finally, make aux html pages
324                 $img->makeaux;
325         }
326
327 # no need to go beyond this point if the directory timestamp did not
328 # change since we built index.html file last time.
329
330         my @istat = stat($self->{-fullpath}.'/index.html');
331         return unless ($youngest > $istat[9]);
332
333 # 5. start building index.html for the directory
334
335         $self->startindex;
336
337 # 6. iterate through subdirectories to build subalbums list
338
339         if (@dirlist) {
340                 $self->startsublist;
341                 foreach my $dir(@dirlist) {
342                         $dir->sub_entry;
343                 }
344                 $self->endsublist;
345         }
346
347 # 7. iterate through images to build thumb list
348
349         if (@imglist) {
350                 $self->startimglist;
351                 foreach my $img(@imglist) {
352                         print "Img: $img->{-fullpath}\n" if ($debug);
353                         $img->img_entry;
354                 }
355                 $self->endimglist;
356         }
357
358 # 8. comlplete building index.html for the directory
359
360         $self->endindex;
361 }
362
363 sub isdir {
364         my $self = shift;
365         return ( -d $self->{-fullpath} );
366 }
367
368 sub isimg {
369         my $self = shift;
370         my $fullpath = $self->{-fullpath};
371         return 0 unless ( -f $fullpath );
372
373         if ($havegeoloc) {
374                 my $exif = new Image::ExifTool;
375                 $exif->ExtractInfo($fullpath);
376                 my ($la,$lo) = $exif->GetLocation();
377                 if ($la && $lo) {
378                         $self->{-geoloc} = [$la,$lo];
379                 }
380         }
381
382         my $info = image_info($fullpath);
383         if (my $error = $info->{error}) {
384                 if (($error !~ "Unrecognized file format") &&
385                     ($error !~ "Can't read head")) {
386                         warn "File \"$fullpath\": $error\n";
387                 }
388                 return 0;
389         }
390
391         tryapp12($info) unless ($info->{'ExifVersion'});
392
393         $self->{-isimg} = 1;
394         $self->{-info} = $info;
395         return 1;
396 }
397
398 sub tryapp12 {
399         my $info = shift;       # this is not a method
400         my $app12;
401         # dirty hack to take care of Image::Info parser strangeness
402         foreach my $k(keys %$info) {
403                 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
404         }
405         return unless ($app12); # bad luck
406         my $seenfirstline=0;
407         foreach my $ln(split /[\r\n]+/,$app12) {
408                 $ln =~ s/[[:^print:]\000]/ /g;
409                 unless ($seenfirstline) {
410                         $seenfirstline=1;
411                         $info->{'Make'}=$ln;
412                         next;
413                 }
414                 my ($k,$v)=split /=/,$ln,2;
415                 if ($k eq 'TimeDate') {
416                         $info->{'DateTime'} =
417                                 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
418                                                         unless ($v < 0);
419                 } elsif ($k eq 'Shutter') {
420                         $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
421                 } elsif ($k eq 'Flash') {
422                         $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
423                 } elsif ($k eq 'Type') {
424                         $info->{'Model'} = $v;
425                 } elsif ($k eq 'Version') {
426                         $info->{'Software'} = $v;
427                 } elsif ($k eq 'Fnumber') {
428                         $info->{'FNumber'} = $v;
429                 }
430         }
431 }
432
433 sub initdir {
434         my $self = shift;
435         my $fullpath = $self->{-fullpath};
436         for my $subdir(@sizes, 'html') {
437                 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
438                 mkdir($tdir,0755) unless ( -d $tdir );
439         }
440         $self->edittitle;
441 }
442
443 sub edittitle {
444         my $self = shift;
445         my $fullpath = $self->{-fullpath};
446         my $title;
447         my $T;
448         if (open($T,'<'.$fullpath.'/.title')) {
449                 binmode($T, ":utf8");
450                 $title = <$T>;
451                 $title =~ s/[\r\n]*$//;
452                 close($T);
453         }
454         if ($asktitle || (!$title && !$noasktitle)) {
455                 my $prompt = $self->{-relpath};
456                 $prompt = '/' unless ($prompt);
457                 my $OUT = $term->OUT || \*STDOUT;
458                 print $OUT "Enter title for $fullpath\n";
459                 $title = $term->readline($prompt.' >',$title);
460                 $term->addhistory($title) if ($title);
461                 if (open($T,'>'.$fullpath.'/.title')) {
462                         print $T $title,"\n";
463                         close($T);
464                 }
465         }
466         unless ($title) {
467                 $title=$self->{-relpath};
468         }
469         $self->{-title}=$title;
470         print "title in $fullpath is $title\n" if ($debug);
471 }
472
473 sub makescaled {
474         my $self = shift;
475         my $fn = $self->{-fullpath};
476         my $name = $self->{-base};
477         my $dn = $self->{-parent}->{-fullpath};
478         my ($w, $h) = dim($self->{-info});
479         my $max = ($w > $h)?$w:$h;
480
481         foreach my $size(@sizes) {
482                 my $nref = '.'.$size.'/'.$name;
483                 my $nfn = $dn.'/'.$nref;
484                 my $factor=$size/$max;
485                 if ($factor >= 1) {
486                         $self->{$size}->{'url'} = $name; # unscaled version
487                         $self->{$size}->{'dim'} = [$w, $h];
488                 } else {
489                         $self->{$size}->{'url'} = $nref;
490                         $self->{$size}->{'dim'} = [int($w*$factor+.5),
491                                                         int($h*$factor+.5)];
492                         if (isnewer($fn,$nfn)) {
493                                 doscaling($fn,$nfn,$factor,$w,$h);
494                         }
495                 }
496         }
497 }
498
499 sub isnewer {
500         my ($fn1,$fn2) = @_;                    # this is not a method
501         my @stat1=stat($fn1);
502         my @stat2=stat($fn2);
503         return (!@stat2 || ($stat1[9] > $stat2[9]));
504         # true if $fn2 is absent or is older than $fn1
505 }
506
507 sub doscaling {
508         my ($src,$dest,$factor,$w,$h) = @_;     # this is not a method
509
510         my $err=1;
511         if ($haveimagick) {
512                 my $im = new Image::Magick;
513                 print "doscaling $src -> $dest by $factor\n" if ($debug);
514                 if ($err = $im->Read($src)) {
515                         warn "ImageMagick: read \"$src\": $err";
516                 } else {
517                         $im->Scale(width=>$w*$factor,height=>$h*$factor);
518                         $err=$im->Write($dest);
519                         warn "ImageMagick: write \"$dest\": $err" if ($err);
520                 }
521                 undef $im;
522         }
523         if ($err) {     # fallback to command-line tools
524                 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
525         }
526 }
527
528 sub makeaux {
529         my $self = shift;
530         my $name = $self->{-base};
531         my $dn = $self->{-parent}->{-fullpath};
532         my $pref = $self->{-previmg}->{-base};
533         my $nref = $self->{-nextimg}->{-base};
534         my $inc = $self->{-inc}.$incdir.'/';
535         my $title = $self->{-info}->{'Comment'};
536         $title = $name unless ($title);
537
538         print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
539
540         # slideshow
541         for my $refresh('static', 'slide') {
542                 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
543                 if (isnewer($self->{-fullpath},$fn)) {
544                         my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
545                         my $fwdref;
546                         my $bakref;
547                         if ($nref) {
548                                 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
549                         } else {
550                                 $fwdref = '../index.html';
551                         }
552                         if ($pref) {
553                                 $bakref = sprintf("%s-%s.html",$pref,$refresh);
554                         } else {
555                                 $bakref = '../index.html';
556                         }
557                         my $toggleref;
558                         my $toggletext;
559                         if ($refresh eq 'slide') {
560                                 $toggleref=sprintf("%s-static.html",$name);
561                                 $toggletext = 'Stop!';
562                         } else {
563                                 $toggleref=sprintf("%s-slide.html",$name);
564                                 $toggletext = 'Play-&gt;';
565                         }
566                         my $F;
567                         unless (open($F,'>'.$fn)) {
568                                 warn "cannot open \"$fn\": $!";
569                                 next;
570                         }
571                         binmode($F, ":utf8");
572                         if ($refresh eq 'slide') {
573                                 print $F start_html(
574                                         -encoding=>"utf-8",
575                                         -title=>$title,
576                                         -bgcolor=>"#808080",
577                                         -head=>meta({-http_equiv=>'Refresh',
578                                                 -content=>"3; url=$fwdref"}),
579                                         -style=>{-src=>$inc."gallery.css"},
580                                         ),"\n",
581                                         comment("Created by ".$version),"\n";
582                                                 
583                         } else {
584                                 print $F start_html(-title=>$title,
585                                         -encoding=>"utf-8",
586                                         -bgcolor=>"#808080",
587                                         -style=>{-src=>$inc."gallery.css"},
588                                         ),"\n",
589                                         comment("Created by ".$version),"\n";
590                         }
591                         print $F start_table({-class=>'navi'}),start_Tr,"\n",
592                                 td(a({-href=>"../index.html"},"Index")),"\n",
593                                 td(a({-href=>$bakref},"&lt;&lt;Prev")),"\n",
594                                 td(a({-href=>$toggleref},$toggletext)),"\n",
595                                 td(a({-href=>$fwdref},"Next&gt;&gt;")),"\n",
596                                 td({-class=>'title'},$title),"\n",
597                                 end_Tr,
598                                 end_table,"\n",
599                                 center(table({-class=>'picframe'},
600                                         Tr(td(img({-src=>$imgsrc,
601                                                    -class=>'standalone',
602                                                    -alt=>$title}))))),"\n",
603                                 end_html,"\n";
604                         close($F);
605                 }
606         }
607
608         # info html
609         my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
610         if (isnewer($self->{-fullpath},$fn)) {
611                 my $F;
612                 unless (open($F,'>'.$fn)) {
613                         warn "cannot open \"$fn\": $!";
614                         return;
615                 }
616                 binmode($F, ":utf8");
617                 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
618                 print $F start_html(-title=>$title,
619                                 -encoding=>"utf-8",
620                                 -style=>{-src=>$inc."gallery.css"},
621                                 -script=>[
622                                         {-src=>$inc."mootools.js"},
623                                         {-src=>$inc."urlparser.js"},
624                                         {-src=>$inc."infopage.js"},
625                                 ]),"\n",
626                         comment("Created by ".$version),"\n",
627                         start_center,"\n",
628                         h1($title),"\n",
629                         table({-class=>'ipage'},
630                                 Tr(td(img({-src=>$imgsrc,
631                                            -class=>'thumbnail',
632                                            -alt=>$title})),
633                                         td($self->infotable))),
634                         a({-href=>'../index.html',-class=>'conceal'},
635                                 'Index'),"\n",
636                         end_center,"\n",
637                         end_html,"\n";
638                 close($F);
639         }
640 }
641
642 sub startindex {
643         my $self = shift;
644         my $fn = $self->{-fullpath}.'/index.html';
645         my $block = $self->{-fullpath}.'/.noindex';
646         $fn = '/dev/null' if ( -f $block );
647         my $IND;
648         unless (open($IND,'>'.$fn)) {
649                 warn "cannot open $fn: $!";
650                 return;
651         }
652         binmode($IND, ":utf8");
653         $self->{-IND} = $IND;
654
655         my $inc = $self->{-inc}.$incdir.'/';
656         my $title = $self->{-title};
657         my $rsslink="";
658         if ($rssobj) {
659                 $rsslink=Link({-rel=>'alternate',
660                                 -type=>'application/rss+xml',
661                                 -title=>'RSS',
662                                 -href=>$self->{-inc}.$rssfile});
663         }
664         print $IND start_html(-title => $title,
665                         -encoding=>"utf-8",
666                         -head=>$rsslink,
667                         -style=>[
668                                 {-src=>$inc."gallery.css"},
669                                 {-src=>$inc."custom.css"},
670                         ],
671                         -script=>[
672                                 {-src=>$inc."mootools.js"},
673                                 {-src=>$inc."overlay.js"},
674                                 {-src=>$inc."urlparser.js"},
675                                 {-src=>$inc."multibox.js"},
676                                 {-src=>$inc."showwin.js"},
677                                 {-src=>$inc."controls.js"},
678                                 {-src=>$inc."show.js"},
679                                 {-src=>$inc."gallery.js"},
680                         ]),"\n",
681                 comment("Created by ".$version),"\n",
682                 start_div({-class => 'indexContainer',
683                                 -id => 'indexContainer'}),
684                 "\n";
685         my $EVL;
686         if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
687                 my $prm;
688                 while (<$EVL>) {
689                         $prm .= $_;
690                 }
691                 close($EVL);
692                 %_ = (
693                         -version        => $version,
694                         -depth          => $self->{-depth},
695                         -title          => $title,
696                         -breadcrumbs    => "breadcrumbs unimplemented",
697                 );
698                 print $IND eval $prm,"\n";
699         } else {
700                 print STDERR "could not open ",
701                         $self->{-toppath}.'/'.$incdir.'/header.pl',
702                         " ($!), reverting to default header";
703                 print $IND a({-href=>"../index.html"},"UP"),"\n",
704                         h1({-class=>'title'},$title),"\n",
705         }
706 }
707
708 sub endindex {
709         my $self = shift;
710         my $IND = $self->{-IND};
711
712         print $IND end_div;
713         my $EVL;
714         if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
715                 my $prm;
716                 while (<$EVL>) {
717                         $prm .= $_;
718                 }
719                 close($EVL);
720                 %_ = (
721                         -version        => $version,
722                         -depth          => $self->{-depth},
723                         -title          => $self->{-title},
724                         -breadcrumbs    => "breadcrumbs unimplemented",
725                 );
726                 print $IND eval $prm,"\n";
727         } else {
728                 print STDERR "could not open ",
729                         $self->{-toppath}.'/'.$incdir.'/footer.pl',
730                         " ($!), reverting to default empty footer";
731         }
732         print $IND end_html,"\n";
733
734         close($IND) if ($IND);
735         undef $self->{-IND};
736         if ($rssobj) {
737                 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
738                                 $self->{-title},
739                                 $self->{-numofimgs},
740                                 $self->{-numofsubs};
741                 my $rsslink=$rssobj->{'rss'}->channel('link').
742                         $self->{-relpath}."index.html";
743                 $rssobj->{'rss'}->add_item(
744                         title           => $self->{-title},
745                         link            => $rsslink,
746                         description     => $rsstitle,
747                         pubDate         => time2str(time),
748                 );
749         }
750 }
751
752 sub startsublist {
753         my $self = shift;
754         my $IND = $self->{-IND};
755
756         print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
757 }
758
759 sub sub_entry {
760         my $self = shift;
761         my $IND = $self->{-parent}->{-IND};
762         my $name = $self->{-base};
763         my $title = $self->{-title};
764
765         $self->{-parent}->{-numofsubs}++;
766         print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
767                         td(a({-href=>$name.'/index.html'},$title))),"\n";
768 }
769
770 sub endsublist {
771         my $self = shift;
772         my $IND = $self->{-IND};
773
774         print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
775 }
776
777 sub startimglist {
778         my $self = shift;
779         my $IND = $self->{-IND};
780         my $first = $self->{-firstimg}->{-base};
781         my $slideref = sprintf(".html/%s-slide.html",$first);
782
783         print $IND h2({-class=>"ititle"},"Images ",
784                 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
785                         '&gt; slideshow')),"\n";
786 }
787
788 sub img_entry {
789         my $self = shift;
790         my $IND = $self->{-parent}->{-IND};
791         my $name = $self->{-base};
792         my $title = $self->{-info}->{'Comment'};
793         $title = $name unless ($title);
794         my $thumb = $self->{$sizes[0]}->{'url'};
795         my $info = $self->{-info};
796         my ($w, $h) = dim($info);
797
798         my $i=0+$self->{-parent}->{-numofimgs};
799         $self->{-parent}->{-numofimgs}++;
800
801         print $IND a({-name=>$name}),"\n",
802                 start_table({-class=>'slide'}),start_Tr,start_td,"\n";
803         print $IND div({-class=>'slidetitle'},
804                         "\n ",a({-href=>".html/$name-info.html",
805                                 -title=>'Image Info: '.$name,
806                                 -class=>'infoBox'},
807                                 $title),"\n"),"\n",
808                 start_div({-class=>'slideimage'});
809         if ($self->{-geoloc}) {
810                 my ($la,$lo) = @{$self->{-geoloc}};
811                 print $IND a({-href=>"http://maps.google.com/".
812                                                 "?q=$la,$lo&ll=$la,$lo",
813                                 -title=>"$la,$lo",
814                                 -class=>'geoloc'},
815                                 div({-class=>'geoloc'},"")),"\n";
816         }
817         print $IND a({-href=>".html/$name-static.html",
818                                 -title=>$title,
819                                 -class=>'showImage',
820                                 -rel=>'i'.$name},
821                                 img({-src=>$thumb,
822                                      -class=>'thumbnail',
823                                      -alt=>$title})),"\n",end_div,
824                 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
825         foreach my $sz(@sizes) {
826                 my $src=$self->{$sz}->{'url'};
827                 my $w=$self->{$sz}->{'dim'}->[0];
828                 my $h=$self->{$sz}->{'dim'}->[1];
829                 print $IND "  ",a({-href=>$src,
830                         -class=>"conceal",
831                         -rel=>$w."x".$h,
832                         -title=>"Reduced to ".$w."x".$h},
833                         $w."x".$h)," \n";
834         }
835         print $IND "  ",a({-href=>$name,
836                                 -rel=>$w."x".$h,
837                                 -title=>'Original'},$w."x".$h),
838                 "\n",end_div,"\n",
839                 end_td,end_Tr,end_table,"\n";
840 }
841
842 sub endimglist {
843         my $self = shift;
844         my $IND = $self->{-IND};
845
846         print $IND br({-clear=>'all'}),hr,"\n\n";
847 }
848
849 sub infotable {
850         my $self = shift;
851         my $info = $self->{-info};
852         my $msg='';
853
854         my @infokeys=(
855                 'DateTime',
856                 'ExposureTime',
857                 'FNumber',
858                 'Flash',
859                 'ISOSpeedRatings',
860                 'MeteringMode',
861                 'ExposureProgram',
862                 'FocalLength',
863                 'FileSource',
864                 'Make',
865                 'Model',
866                 'Software',
867         );
868         $msg.=start_table({-class=>'infotable'})."\n";
869         foreach my $k(@infokeys) {
870                 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
871         }
872         $msg.=end_table."\n";
873 }
874