Skip to content

Instantly share code, notes, and snippets.

@koyachi
Created November 21, 2008 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koyachi/27375 to your computer and use it in GitHub Desktop.
Save koyachi/27375 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use DateTime;
use Data::Dumper;
my $dir = $ARGV[0] || '/tmp';
my $dir_name = (split '/', $dir)[-1];
my $url = 'http://buffr.org';
my @dir_list = ();
my $longest_name = '';
finddepth(sub {
my $last_dir_name = (split '/', $File::Find::name)[-1];
if ($dir . '/'. $last_dir_name eq $File::Find::name &&
-d $File::Find::name &&
-e $File::Find::name. '/index.html') {
if (length($last_dir_name) > length($longest_name)) {
$longest_name = $last_dir_name;
}
push @dir_list, {
project_name => $last_dir_name,
link => "./$last_dir_name/index.html",
path => $File::Find::name,
last_modified => DateTime->from_epoch( epoch => (stat($File::Find::name))[9]),
};
}
}, ($dir));
my $project_name_width = length($longest_name);
my $fmt = '%-' . length($longest_name) . "s";
my $pad_after_icon = 1;
my $pad_after_name = 4;
my $pad_after_lastmodified = 3;
my $pad_after_size = 1;
my $entries_html = '';
foreach my $d (@dir_list) {
$entries_html .= join "", (
' ' x $pad_after_icon,
'<a href="' . $d->{link} . '">' . $d->{project_name} . '</a>',
' ' x ($project_name_width - length($d->{project_name}) + $pad_after_name),
$d->{last_modified}->ymd . ' ' . $d->{last_modified}->hms,
"\n"
);
}
my $header = join '', (
' ' x $pad_after_icon,
(sprintf $fmt, 'Name'),
' ' x $pad_after_name,
'Last modified Size Description'
);
my $template = <<TEMPLATE;
<HTML>
<HEAD>
<TITLE>Index of $dir_name</TITLE>
<META NAME="generator", CONTENT="mod_autoindex">
</HEAD>
<BODY bgcolor="#ffffff" text="#000000">
<TABLE>
<TR><TD bgcolor="#ffffff" class="title">
<FONT size="+3" face="Helvetica,Arial,sans-serif">
<B>Index of /$dir_name</B>
</FONT>
</TD></TR>
</TABLE>
<PRE>$header
<HR noshade align="left" width="80%">
$entries_html
</PRE>
<HR noshade align="left" width="80%">
<ADDRESS>$url</ADDRESS>
</BODY></HTML>
TEMPLATE
print $template;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment