attempt to support utf8 better
[mkgallery.git] / mkgallery.pl
index 7a4b681a5aacb29f8be85d668c839e4bd4761122..8bed35cb1d594efd3fdb17cb7703751757ddb198 100755 (executable)
@@ -6,7 +6,7 @@ my $version='$Id$';
 # Makes use of modified "slideshow" javascript by Samuel Birch
 # http://www.phatfusion.net/slideshow/
 
-# Copyright (c) 2006-2008 Eugene G. Crosser
+# Copyright (c) 2006-2013 Eugene G. Crosser
 
 #  This software is provided 'as-is', without any express or implied
 #  warranty.  In no event will the authors be held liable for any damages
@@ -36,15 +36,12 @@ use Term::ReadLine;
 use Getopt::Long;
 use Encode;
 use UUID;
-#use encoding 'utf-8';
+use utf8;
 binmode(STDOUT, ":utf8");
 
 my $haveimagick = eval { require Image::Magick; };
 { package Image::Magick; }     # to make perl compiler happy
 
-my $havefeed = eval { require XML::FeedPP; };
-{ package XML::FeedPP; }       # to make perl compiler happy
-
 my $havegeoloc = eval { require Image::ExifTool::Location; };
 { package Image::ExifTool::Location; } # to make perl compiler happy
 
@@ -54,11 +51,9 @@ my $incdir = ".gallery2";
 ######################################################################
 
 my $incpath;
-my $feedobj;
 my $debug = 0;
 my $asktitle = 0;
 my $noasktitle = 0;
-my $feed = "";
 
 charset("utf-8");
 
@@ -67,23 +62,14 @@ unless (GetOptions(
                'incpath'=>\$incpath,
                'asktitle'=>\$asktitle,
                'noasktitle'=>\$noasktitle,
-               'feed=s'=>\$feed,
                'debug'=>\$debug)) {
        &help;
 }
 
-if ($feed && !$havefeed) {
-       print STDERR "You need to install XML::FeedPP to use --feed\n";
-       exit 1;
-}
-
 my $term = new Term::ReadLine "Edit Title";
+binmode($term->IN, ':utf8');
 
 FsObj->new(getcwd)->iterate;
-if ($feedobj) {
-       $feedobj->{-feed}->pubDate(time);
-       $feedobj->{-feed}->to_file($feedobj->{-savepath});
-}
 
 sub help {
 
@@ -96,11 +82,6 @@ usage: $0 [options]
  --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.
- --feed=...:   build Atom feed for newly added "albums",
-               enter filename, base URL, and optionally PuSH hub url,
-               separated by commas. (Note: PuSH obviously does not work
-               "out of the box" for static tree! You need a separate
-               "watcher" script to do the publishing for you.)
 __END__
 
        exit 1;
@@ -131,7 +112,7 @@ sub new {
                                -root=>$root,
                                -fullpath=>$root,
                        };
-               # fill in -inc, -feed, -relpath
+               # fill in -inc, -relpath
                initpaths($self); # we are not blessed yet, so cheat.
        }
        bless $self, $class;
@@ -180,68 +161,13 @@ sub initpaths {
                $self->{-relpath} = $relpath;
                $self->{-toppath} = substr($fullpath,0,$pos);
                #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
-               initfeed($self);
        } else {
                $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
-               $self->{-feed} = '';
                $self->{-relpath} = '';
                $self->{-depth} = 0;
        }
 }
 
-sub initfeed {
-       my $self=shift;         # this is not a method but we cheat
-       my $fullpath=$self->{-fullpath};
-       my $toppath=$self->{-toppath};
-       my $inc=$self->{-inc}.$incdir.'/';
-       my $conffile=$toppath.'/'.$incdir.'/feed.conf';
-       my $CONF;
-
-       if (! $incpath) {
-               if ($feed) {
-                       if (open($CONF,">".$conffile)) {
-                               print $CONF $feed,"\n";
-                               close($CONF);
-                       } else {
-                               print STDERR "could not open $conffile: $!\n";
-                       }
-               } else {
-                       if (open($CONF,$conffile)) {
-                               $feed=<$CONF>;
-                               close($CONF);
-                               chop $feed;
-                       }
-               }
-       }
-
-       return unless ($feed);
-
-       my ($feedfile, $feedbase, $feedhub) = split(',', $feed);
-       $feedbase .= '/' unless ($feedbase =~ /\/$/);
-       print "($feedfile, $feedbase, $feedhub)\n";
-
-       $feedobj->{-savepath} = $self->{-toppath}.'/'.$feedfile;
-       $feedobj->{-file} = $feedfile;
-       $feedobj->{-base} = $feedbase;
-       $feedobj->{-hub} = $feedhub;
-       if ( -f $feedobj->{-file} ) {
-               $feedobj->{-feed} = XML::FeedPP::Atom::Atom10->new(
-                                                       $feedobj->{-file});
-               $feedobj->{-feed}->limit_item(15);
-       } else {
-               $feedobj->{-feed} = XML::FeedPP::Atom::Atom10->new;
-               $feedobj->{-feed}->title("Gallery");
-               $feedobj->{-feed}->description("generated by ".
-                       "<a href=\"http://www.average.org/mkgallery/\">".
-                       "mkgallery</a>");
-               $feedobj->{-feed}->link($feedbase);
-               #$feedobj->{-feed}->copyright("");
-               #$feedobj->{-feed}->language("en");
-               #$feedobj->{-feed}->image($url, $tit, $link, $desc, $w, $h);
-       }
-       $self->{-feed} = $feedobj->{-feed};
-}
-
 sub iterate {
        my $self = shift;
        my $fullpath .= $self->{-fullpath};
@@ -431,9 +357,10 @@ sub edittitle {
        my $self = shift;
        my $fullpath = $self->{-fullpath};
        my $title;
+       my $titleimage;
        my $T;
-       if (open($T,'<'.$fullpath.'/.title')) {
-               binmode($T, ":utf8");
+       my $TI;
+       if (open($T,'<:encoding(utf8)', $fullpath.'/.title')) {
                $title = <$T>;
                $title =~ s/[\r\n]*$//;
                close($T);
@@ -445,7 +372,7 @@ sub edittitle {
                print $OUT "Enter title for $fullpath\n";
                $title = $term->readline($prompt.' >',$title);
                $term->addhistory($title) if ($title);
-               if (open($T,'>'.$fullpath.'/.title')) {
+               if (open($T,'>:encoding(utf8)', $fullpath.'/.title')) {
                        print $T $title,"\n";
                        close($T);
                }
@@ -454,6 +381,13 @@ sub edittitle {
                $title=$self->{-relpath};
        }
        $self->{-title}=$title;
+       if (open($TI,'<:encoding(utf8)', $fullpath.'/.titleimage')) {
+               $titleimage = <$TI>;
+               $titleimage =~ s/[\r\n]*$//;
+               close($TI);
+               #print STDERR "found title image \"",$titleimage,"\"\n";
+               $self->{-titleimage}=$titleimage;
+       }
        print "title in $fullpath is $title\n" if ($debug);
 }
 
@@ -551,11 +485,10 @@ sub makeaux {
                                $toggletext = 'Play-&gt;';
                        }
                        my $F;
-                       unless (open($F,'>'.$fn)) {
+                       unless (open($F,'>:encoding(utf8)', $fn)) {
                                warn "cannot open \"$fn\": $!";
                                next;
                        }
-                       binmode($F, ":utf8");
                        if ($refresh eq 'slide') {
                                print $F start_html(
                                        -encoding=>"utf-8",
@@ -596,11 +529,10 @@ sub makeaux {
        my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
        if (isnewer($self->{-fullpath},$fn)) {
                my $F;
-               unless (open($F,'>'.$fn)) {
+               unless (open($F,'>:encoding(utf8)', $fn)) {
                        warn "cannot open \"$fn\": $!";
                        return;
                }
-               binmode($F, ":utf8");
                my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
                print $F start_html(-title=>$title,
                                -encoding=>"utf-8",
@@ -632,25 +564,17 @@ sub startindex {
        my $block = $self->{-fullpath}.'/.noindex';
        $fn = '/dev/null' if ( -f $block );
        my $IND;
-       unless (open($IND,'>'.$fn)) {
+       unless (open($IND,'>:encoding(utf8)', $fn)) {
                warn "cannot open $fn: $!";
                return;
        }
-       binmode($IND, ":utf8");
        $self->{-IND} = $IND;
 
        my $inc = $self->{-inc}.$incdir.'/';
        my $title = $self->{-title};
-       my $feedlink="";
-       if ($feedobj) {
-               $feedlink=Link({-rel=>'alternate',
-                               -type=>'application/atom+xml',
-                               -title=>'Gallery Feed',
-                               -href=>$feedobj->{-base}.$feedobj->{-file}});
-       }
+       my $titleimage = $self->{-titleimage};  
        print $IND start_html(-title => $title,
                        -encoding=>"utf-8",
-                       -head=>$feedlink,
                        -style=>[
                                {-src=>$inc."gallery.css"},
                                {-src=>$inc."custom.css"},
@@ -670,7 +594,7 @@ sub startindex {
                                -id => 'indexContainer'}),
                "\n";
        my $EVL;
-       if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
+       if (open($EVL, '<:encoding(utf8)', $self->{-toppath}.'/'.$incdir.'/header.pl')) {
                my $prm;
                while (<$EVL>) {
                        $prm .= $_;
@@ -680,6 +604,7 @@ sub startindex {
                        -version        => $version,
                        -depth          => $self->{-depth},
                        -title          => $title,
+                       -titleimage     => $titleimage,
                        -path           => $self->{-fullpath},
                        -breadcrumbs    => "breadcrumbs unimplemented",
                );
@@ -688,8 +613,14 @@ sub startindex {
                print STDERR "could not open ",
                        $self->{-toppath}.'/'.$incdir.'/header.pl',
                        " ($!), reverting to default header";
-               print $IND a({-href=>"../index.html"},"UP"),"\n",
-                       h1({-class=>'title'},$title),"\n",
+               print $IND a({-href=>"../index.html"},"UP"),"\n";
+               if ($titleimage) {
+                       print $IND img({-src=>$titleimage,
+                                       -class=>'titleimage',
+                                       -alt=>'Title Image'}),"\n";
+               }
+               print $IND h1({-class=>'title'},$title),
+                       br({-clear=>'all'}),"\n";
        }
 }
 
@@ -699,7 +630,7 @@ sub endindex {
 
        print $IND end_div;
        my $EVL;
-       if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
+       if (open($EVL, '<:encoding(utf8)', $self->{-toppath}.'/'.$incdir.'/footer.pl')) {
                my $prm;
                while (<$EVL>) {
                        $prm .= $_;
@@ -709,6 +640,7 @@ sub endindex {
                        -version        => $version,
                        -depth          => $self->{-depth},
                        -title          => $self->{-title},
+                       -titleimage     => $self->{-titleimage},
                        -breadcrumbs    => "breadcrumbs unimplemented",
                );
                print $IND eval $prm,"\n";
@@ -721,25 +653,6 @@ sub endindex {
 
        close($IND) if ($IND);
        undef $self->{-IND};
-       if ($feedobj) {
-               my $feedtitle=sprintf "%s [%d images, %d subalbums]",
-                               $self->{-title},
-                               $self->{-numofimgs},
-                               $self->{-numofsubs};
-               my $feedlink=$feedobj->{-feed}->link.
-                       $self->{-relpath}."index.html";
-               my $uu;
-               my $us;
-               UUID::generate($uu);
-               UUID::unparse($uu, $us);
-               $feedobj->{-feed}->add_item(
-                       title           => $self->{-title},
-                       link            => $feedlink,
-                       description     => $feedtitle,
-                       pubDate         => time,
-                       guid            => $us,
-               );
-       }
 }
 
 sub startsublist {