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