#!/usr/bin/perl # display.pl - Script to display entry fragments # Set the root directory for archive files. $ROOT_DIR = "archives"; # And the root URL the world will see. # (Normally set to "/") # Should be used in all links in place of hard coded references. $URL_ROOT = "/"; # Get the time, format the couple of variables I'll actually use. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); ++$mon; $year += 1900; # Handy for turning numeric dates into English... # "Null" is there so that $month_name[1] corresponds to January, etc. # (perl starts index numbers at 0, not 1) @month_name = qw(Null January February March April May June July August September October November December); # Grab the command line options. @options = @ARGV; # We're sending some HTML down the pipe... print "Content-type: text/html\n\n"; # Unless this is already in an HTML document or calling for # the newest updates, spit out some default HTML... $doc_uri = $ENV{'DOCUMENT_URI'}; $print_footer = 0; unless (($doc_uri =~ m/html$/) || ($options[0] eq "new")) { fragment_print ("header.html"); $print_footer = 1; print "
"; foreach $update_file (@update_files) { print "" . "$update_file \n"; } print "
\n\n"; } print "($update_count entries)
"; } elsif (-T "$ROOT_DIR/$option") { entry_print ($option, "index"); } } # month_print: print the entries in a given month (nnnn/nn) # calls dir_list sub month_print { # If a directory exists for $month, use dir_list to grab # the entry files it contains into @entry_files, sorted # numerically. Then send each entry to entry_print. my ($month) = @_; if (-d "$ROOT_DIR/$month") { if (-T "$ROOT_DIR/$month/index") { entry_print ($month, "index"); print "\n" . $ico_markup . "
\n\n"; } fragment_print ("$entry_loc"); } elsif (-d $entry_loc) { @sub_entries = dir_list ($entry_loc, "alpha", "^[a-z_]+\$"); if (($ico_markup = icon_markup($entry, ""))) { print "\n" . $ico_markup . "
\n\n"; } fragment_print ("$entry_loc/index"); if (($level eq "index") && (@sub_entries > 1)) { # if we're just supposed to print an index # and there're extra files # spit out icons or text links for them print ""
. "*
\n";
foreach $sub_entry (@sub_entries) {
next if ($sub_entry eq "index");
if ( ($ico_markup = icon_markup("$entry/$sub_entry",
"[ $sub_entry ]")) ) {
print "$ico_markup\n";
} else {
print "[ $sub_entry ]\n";
}
}
print "
— "; $markup_end = "
\n\n"; } if ($entry =~ m/(^[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2})/) { my ($entry_year, $entry_month, $entry_day) = split (/\//, $1); # return a fancy datestamp. $stamp = qq{$entry_year / $month_name[$entry_month] / $entry_day}; return ($markup_start . $stamp . $markup_end); } else { return ""; } } # fragment_print: print a text fragment - a header, footer, update, etc. # called by main routines, entry_print, etc. # calls line_parse to take care of special markup # returns 1 on successful completion, 0 otherwise sub fragment_print { my ($file) = @_; my ($line, @lines); if ((-T $file)) { open (FRAGMENT, $file) or print "Can't read {$file} as text file.\n"; @lines =/;
$line =~ s/(\s?)--(\s?)/$1—$2/g;
}
$line =~ s/ /;
$line =~ s/<\/freeverse>/<\/p>\n\n/;
if ($dash) {
$line =~ s/(\s?)$dash(\s?)/$1—$2/g;
}
return $line;
}
# image_size : returns (width, height) of a PNG or JPEG file.
# munged together from pngsize and jpegsize
# in wwwis, by Alex Knowles and Andrew Tong
# see http://www.bloodyeck.com/wwwis/
# any weirdness here is probably my fault, not theirs.
# called by icon_markup
sub image_size {
my ($image_file) = @_;
my ($size);
if ( !open(IMAGE, "<$image_file") ) {
print "can't open IMG $image_file";
$size = "";
} else {
if ($image_file =~ m/\.png$/) { # it's a PNG
my ($a, $b, $c, $d, $e, $f, $g, $h) = 0;
if (defined($image_file)
&& read(IMAGE, $head, 8) == 8
&& ($head eq "\x8a\x4d\x4e\x47\x0d\x0a\x1a\x0a" ||
$head eq "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a")
&& read(IMAGE, $head, 4) == 4
&& read(IMAGE, $head, 4) == 4
&& ($head eq "MHDR" || $head eq "IHDR")
&& read(IMAGE, $head, 8) == 8) {
# ($x, $y) = unpack("I"x2, $head);
# doesn't work on little-endian machines
# return ($x,$y);
($a,$b,$c,$d,$e,$f,$g,$h) = unpack ("C"x8, $head);
return ($a<<24|$b<<16|$c<<8|$d, $e<<24|$f<<16|$g<<8|$h);
}
} elsif ($image_file =~ m/\.jpe?g$/) { # it's a JPEG
my($done) = 0;
my($c1,$c2,$ch,$s,$length, $dummy) = (0,0,0,0,0,0);
my($a,$b,$c,$d);
if (defined($image_file)
&& read(IMAGE, $c1, 1)
&& read(IMAGE, $c2, 1)
&& ord($c1) == 0xFF
&& ord($c2) == 0xD8) {
while (ord($ch) != 0xDA && !$done) {
# Find next marker (JPEG markers begin with 0xFF)
# This can hang the program!!
while (ord($ch) != 0xFF) {
return(0,0) unless read(IMAGE, $ch, 1);
}
# JPEG markers can be padded with unlimited 0xFF's
while (ord($ch) == 0xFF) {
return(0,0) unless read(IMAGE, $ch, 1);
}
# Now, $ch contains the value of the marker.
if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) {
return(0,0) unless read (IMAGE, $dummy, 3);
return(0,0) unless read(IMAGE, $s, 4);
($a,$b,$c,$d)=unpack("C"x4,$s);
return ($c<<8|$d, $a<<8|$b );
} else {
# We **MUST** skip variables, since FF's within
# variable names are NOT valid JPEG markers
return(0,0) unless read (IMAGE, $s, 2);
($c1, $c2) = unpack("C"x2,$s);
$length = $c1<<8|$c2;
last if (!defined($length) || $length < 2);
read(IMAGE, $dummy, $length-2);
}
}
}
}
return (0,0);
}
}