#! /usr/bin/env perl # -*- perl -*- ### Time-stamp: <2009-06-16 20:00:58 MEST> ### M.C. Widerkrantz, mc at hack.org ### Create a static HTML file containing a directory listing with ### descriptions of each file, somewhat like the bulletin boards of ### old. ### The description file must be named 'files.lst' (but see ### $descriptions below) and should be located in the same ### directory. The entries in the file should begin with a filename ### and a double colon "::" followed by the description. ### The description can span many lines and paragraphs, separated by ### double newlines. ### ### Here's an example: ### ### foo:: ### Description of foo. The first line. ### Second line. ### ### Another paragraph in the description of foo. ### ### A third paragraph. ### bar:: ### Another file description. use strict; use warnings; use POSIX; # Set this to the URL for your stylesheet, if you have one, as in #my $stylesheet = "/mc/default.css"; my $stylesheet = ""; # Name of the file containing the descriptions of each file in the # directory. my $descriptions = "files.lst"; # Name of the file to generate. my $index = "index.html"; # Default HTML Title, settable with -t title. my $title = "File Listing"; if ($#ARGV == 1 && $ARGV[0] eq '-t') { $title = $ARGV[1]; } open(my $filesfile, $descriptions) or die "Can't open $descriptions: $!"; open(my $indexfile, ">$index") or die "Can't open $index: $!"; # This is inserted at the top of each generated index. print $indexfile ' '; if ($stylesheet) { print $indexfile "\n"; } print $indexfile "${title}

${title}

"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(); # Make the date readable $year += 1900; $mon += 1; printf $indexfile "

List generated %d-%02d-%02d %02d:%02d:%02d (UTC)\n

\n", $year, $mon, $mday, $hour, $min, $sec; my $filename; while (<$filesfile>) { if ($_ eq "\n") { print $indexfile "

"; } if ($_ =~ /^.*::/) { # This is the start of a new file description. $filename = $_; chop($filename); # get rid of newline chop($filename); # get rid of ending ":" chop($filename); # get rid of ending ":" # Get data about this file. my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename); if ($size) { my $prefix; my $prefixstr; # Get the time. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($mtime); # Make the date readable $year += 1900; $mon += 1; # Make size human readable my $giga = 1073741824; my $mega = 1048576; my $kilo = 1024; if ($size > $giga) { $prefix = $giga; $prefixstr = "Gi"; } elsif ($size > $mega) { $prefix = $mega; $prefixstr = "Mi"; } else { $prefix = $kilo; $prefixstr = "Ki"; } printf $indexfile "

%s %.2f %sB %d-%02d-%02d %02d:%02d:%02d (UTC)
\n", $filename, $filename, $size / $prefix, $prefixstr, $year, $mon, $mday, $hour, $min, $sec; } else { print $indexfile "
${filename}: missing file.
\n"; } } else { # We're in the middle of a description. Just add the line # we're reading. print $indexfile $_; } } print $indexfile "
\n\n\n"; close $indexfile;