Re: http listing
Jeff King <[email protected]> Sat, 26 Mar 2005 21:41:46 -0500 (EST)
| Newsgroups | gmane.comp.djb.publicfile |
|---|---|
| Message-ID | <[email protected]> |
On Sat, 26 Mar 2005, richard lucassen wrote:
> Someone made a (perl?)script a few years ago to create a dirlisting into
> an index.html. Does someone have a link for me?
There's one by George Georgalis here:
http://galis.org/scripts/html/mkindex.html.sh
I've also written a similar script in perl, which I've included below.
It was written many years ago and I don't use it anymore, but I believe
it to be relatively bug-free. :)
-Peff
---- CUT HERE ----
#!/usr/bin/perl
#
# Make Apache-style indices of the current directory. For use with
# publicfile.
use File::Basename;
my $magic = '<!--Generated automatically by make-index -->';
my $index = 'index.html';
my $dir = shift;
$dir = '.' unless $dir;
examine_dir($dir);
exit 0;
sub examine_dir {
my $dir = shift;
if(-e "$dir/$index") {
if(-z "$dir/$index") {
print "Replacing zero-length file in $dir\n";
print_dir($dir);
}
elsif(`grep '$magic' $dir/$index`) {
print "Replacing existing file in $dir\n";
print_dir($dir);
}
else {
print "Ignoring real index in $dir\n";
}
}
else {
print "Generating missing file in $dir\n";
print_dir($dir);
}
foreach my $file (glob "$dir/*") {
$file =~ /\/\./ && next;
if(-d $file) {
examine_dir($file);
}
}
}
sub print_dir {
my $dir = shift;
my $pretty_dir = $dir;
$pretty_dir =~ s/^\.\//\//;
my $parent = (fileparse($dir))[1];
$parent =~ s/^\.\//\//;
open(OUT, ">$dir/$index") || die "Unable to open $dir/$index: $!";
print OUT <<EOF;
<html>
$magic
<head>
<title>Index of $pretty_dir</title>
</head>
<body>
<h1>Index of $pretty_dir</h1>
<table cellpadding=3>
<tr>
<th>Name</th>
<th>Last Modified</th>
<th>Size</th>
</tr>
<tr>
<td><a href="$parent">Parent Directory</a></td>
<td></td>
<td></td>
EOF
foreach my $file (glob "$dir/*") {
my $base = basename($file);
$base =~ /^\./ && next;
$base =~ /$index/ && next;
my ($size, $mtime) = (stat($file))[7,9];
$mtime = localtime($mtime);
if(-d $file) {
$base .= '/';
$size = "";
}
else {
$size .= " bytes";
}
print OUT "<tr>\n";
print OUT " <td><a href=\"$base\">$base</a></td>\n";
print OUT " <td>$mtime</td>\n";
print OUT " <td>$size</td>\n";
print OUT "</tr>\n";
print OUT "\n";
}
print OUT <<EOF;
</table>
</body>
</html>
EOF
close(OUT);
}