#!/usr/bin/perl -w
#
# $Id: ls2htm,v 1.10 2009/06/15 17:39:49 vogelke Exp $
#
# read index.wn file from wndex, write decent table of contents
# similar to autoindex generated by Apache.
use Fcntl ':mode';
use File::Basename;
use Cwd;
use strict;
use subs qw/header trailer modtime slurp/;
my @names = (
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
);
my $kbyte = 1024;
my $mbyte = $kbyte * $kbyte;
# Where are the icons?
my $icondir = '/icons/';
my $iback = $icondir . "back.gif";
my $iblank = $icondir . "blank.gif";
my $ifolder = $icondir . "folder.gif";
my $itext = $icondir . "text.gif";
my $iunk = $icondir . "unknown.gif";
# Current directory:
# Account for / and \ on Win32 and non-Win32 systems
my $pwd;
my $docroot = '/home/userweb/vogelke/public_html';
($^O =~ /Win32/) ? ($pwd = getcwd) =~ s/\//\\/g : ($pwd = getcwd);
$pwd =~ s!$docroot!!;
# HTML header.
header();
my ($mode, $bytes, $mt); # returned from stat();
$mt = (stat(".."))[9];
my $str = modtime($mt);
print ' Parent Directory ';
print "$str - \n";
# How wide should the filename column be?
my $width = 24;
my $spaces = ' ' x $width;
my $blanks;
my ($f, $fname); # short and long form of filename.
my $img; # Icon link showing what type of file we have.
my $size; # Human-readable filesize (kb, Mb, etc).
my $t; # Title for each file.
while (<>) {
chomp;
if (/File=(.*)/) {
$f = $1;
}
elsif (/Title=(.*)/) {
$t = $1;
}
elsif (length($_) == 0) {
# Already do this line?
next unless length($f) + length($t) > 0;
# Get file size.
($mode, $bytes, $mt) = (stat($f))[2, 7, 9];
unless (defined($mode)) {
$mode = 0100644;
$bytes = 1;
$mt = time();
$t = "NOT FOUND -- $t";
}
if ($bytes >= $mbyte) {
$size = sprintf("%.1f", $bytes / $mbyte) . 'M';
}
elsif ($bytes >= $kbyte && $bytes < $mbyte) {
$size = int($bytes / $kbyte) . 'k';
}
else {
$size = '1k';
}
$size = sprintf("%4s", $size);
# Get file type.
$str = modtime($mt);
$img = filetype($mode);
# Align the columns. The output string looks like this:
#
CCC 17-Jan-2008 13:51 22k DDD$
#
# The lengths of CCC (html link) plus the date and size should
# be a constant.
my $k = length($f);
if ($k < $width - 4) {
$blanks = substr($spaces, 0, $width - 1 - $k);
$fname = $f;
}
else {
$blanks = ' ';
$fname = substr($f, 0, $width - 5) . '...';
}
print "$img $fname";
print "$blanks $str $size $t\n";
$f = $t = '';
}
}
trailer();
exit(0);
#---------------------------------------------------------------------
# Print header and start of TOC.
sub header {
my $str = "
$str\n"; } elsif ($str = slurp("HEADER.txt")) { $str = "\n
$str\n"; } elsif ($str = slurp("HEADER.htm")) { 1; } elsif ($str = slurp("HEADER.html")) { 1; } print <<"EOH";
$str\n"; } elsif ($str = slurp("FOOTER.txt")) { $str = "\n
$str\n"; } elsif ($str = slurp("FOOTER.htm")) { 1; } elsif ($str = slurp("FOOTER.html")) { 1; } print <<"EOT";