#!/usr/bin/perl -w # # genpicpages - Generates HTML web pages for displaying pictures, # given an input file with the base picture filenames # and captions for them. # # Usage: genpicpages # # Nadim Nakhleh 11/24/00 # # # Start customization section # $NROWS = 4; # Number of rows per page $NCOLS = 4; # Number of columns per page $F_EXT = "_l"; # File extension for full-size pic (set to "" for most cases) $T_EXT = "_t"; # File extension for thumbnail pic $BACK_PAGE = "/photos.shtml"; # Link back to main page $SEC_DEL = "~SECTION~"; # Input file section delimiter # # End customization section # # Get input filename if (@ARGV == 1) { $infile = shift @ARGV; } else { UsageError(); } # Read input file # NOTE - Input file format (be careful, error checking is minimal): # # # # # # HTML code # for header # of each page # ~SECTION~ # HTML code for # footer of each page # ~SECTION~ # Any additional HTML intro # for first page only # ~SECTION~ # <image file> <caption> # <image file> <caption> open(INFILE, $infile) or die "$0: Can't open input file \"$infile\": $!\n"; # Get output HTML file basename chomp($basename = <INFILE>); # Get image directory chomp($idir = <INFILE>); # Get HTML directory chomp($hdir = <INFILE>); # Get Title page chomp($title = <INFILE>); # Get HTML header $header = ""; while (defined ($line = <INFILE>)) { chomp($line); last if ($line eq $SEC_DEL); $header .= $line . "\n"; } die "$0: Invalid input file, can't find 1st section delimiter!\n" unless (defined $line); # Get HTML footer $footer = ""; while (defined ($line = <INFILE>)) { chomp($line); last if ($line eq $SEC_DEL); $footer .= $line . "\n"; } die "$0: Invalid input file, can't find 2nd section delimiter!\n" unless (defined $line); # Get HTML intro $intro = ""; while (defined ($line = <INFILE>)) { chomp($line); last if ($line eq $SEC_DEL); $intro .= $line . "\n"; } die "$0: Invalid input file, can't find 3rd section delimiter!\n" unless (defined $line); # Get images and captions (allow line commenting with #) while (defined ($line = <INFILE>)) { chomp($line); # Skip comments next if (substr($line, 0, 1) eq "#"); # Split line if ($line =~ /(\S+)\s+(.*)/) { $name = $1; $caption = $2; # Store line $name =~ /(.*)(\..*)/; push(@images, { "fname" => "$idir/" . $1 . $F_EXT . $2, "tname" => "$idir/" . $1 . $T_EXT . $2, "caption" => $caption }); } } close(INFILE); # # Generate pages # # Determine number of pages $numpages = int(@images / ($NROWS * $NCOLS) + .99999); # Write each page foreach $pnum (1 .. $numpages) { $file = "$hdir/${basename}_$pnum.html"; open(OFILE, "> $file") or die "$0: Can't open output file \"$file\": $!\n"; # Write header print OFILE $header . "\n"; # Write intro if ($pnum == 1) { print OFILE $intro . "\n"; } # Write table print OFILE "<table rows=\"$NROWS\" cols=\"$NCOLS\">\n"; stop_table: for ($i = 0; $i < $NROWS; $i++) { print OFILE "<tr>\n"; for ($j = 0; $j < $NCOLS; $j++) { $img = shift(@images); unless ($img) { print OFILE "</tr>\n"; last stop_table; } print OFILE "<td> " . "<a href=\"$img->{fname}\">" . "<img src=\"$img->{tname}\" " . "alt=\"click to see $img->{fname}\"></a>\n"; print OFILE "<br>\n$img->{caption}\n</td>\n"; } print OFILE "</tr>\n"; } print OFILE "</table>\n"; # Write nav section if ($numpages > 1) { print OFILE "<hr>\n<center>\n"; if ($title) { print OFILE "<a href=\"$title\">\<title\></a> "; } if ($pnum != 1) { $n = $pnum - 1; if ($title) { print OFILE " - "; } print OFILE "<a href=\"${basename}_$n.html\">\<prev\></a>"; } foreach $n (1 .. $numpages) { if ($n == $pnum) { print OFILE " - $n"; } else { print OFILE " - <a href=\"${basename}_$n.html\">$n</a>"; } } if ($pnum != $numpages) { $n = $pnum + 1; print OFILE " - <a href=\"${basename}_$n.html\">\<next\></a>"; } print OFILE "</center>\n"; } print OFILE "Go back to the main <a href=\"$BACK_PAGE\">photo</a> page.\n"; # Write footer print OFILE $footer . "\n"; close(OFILE); } # Prints the script header to STDOUT (useful when someone needs to be # reminded of the proper script invocation syntax) sub UsageError { open(SOURCECODE, $0) or die "Can't open script file \"$0\": $!"; <SOURCECODE>; while (<SOURCECODE>) { last unless (/^\#/); print substr($_, 1); } close(SOURCECODE); exit(0); } # End of UsageError()