#!/usr/bin/perl -w # # genindexhtml - generate index.html from README and readdir() # AYM 2009-03-06 # # This file is copyright André Majorel 2009. # # This program is free software; you can redistribute it and/or modify it under # the terms of version 2 of the GNU General Public License as published by the # Free Software Foundation. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. use strict; use Fcntl ':mode'; sub htq ($); sub htqq ($); foreach my $readmepathname (@ARGV) { $readmepathname =~ m{[^/]/README$} or die "need arguments like /README\n"; local $/ = undef; open README, $readmepathname or die "$readmepathname: $!\n"; my $text = ; close README; my $readmepath = $readmepathname; $readmepath =~ s{/README$}{/} or die "$readmepathname: does not end with \"/README\"\n"; my $indexpathname = $readmepath . "index.html"; # Don't overwrite index.html if it's a manually written one if (-e $indexpathname) { open INDEX, $indexpathname or die "$indexpathname: $!\n"; local $/ = undef; my $indexcontent = ; close INDEX; if (index ($indexcontent, '') < 0) { print STDERR "$indexpathname: already exists\n"; next; } } open INDEX, ">$indexpathname" or die "$indexpathname: $!\n"; print "$indexpathname\n"; print INDEX < There should be a title here EOF $text =~ s/\n+$/\n/g; print INDEX "
", htq ($text), "
\n\n"; print INDEX "
\n"; print INDEX "
";
  opendir DIR, $readmepath or die "$readmepath: $!\n";
  my @de = grep $_ !~ /^(\..*|index\.html)$/, readdir DIR;
  closedir DIR;
  foreach my $de (sort @de)
  {
    my $fpathname = $readmepath . $de;
    my @fstat = stat $fpathname;
    if (@fstat == 0)
    {
      die "$fpathname: $!\n";
    }
    my @fmtime = gmtime $fstat[9];
    my $fy = 1900 + $fmtime[5];
    my $fm =    1 + $fmtime[4];
    my $fd =        $fmtime[3];
    my $fsz = S_ISREG ($fstat[2]) ? sprintf '%9d', $fstat[7] : ' ' x 9;
    $de .= '/' if S_ISDIR $fstat[2];
    $fsz =~ s/(..)(.)(..)(.)(...)$/$1 . $2 . ($2 eq ' ' ? $2 : '_')
				 . $3 . $4 . ($4 eq ' ' ? $4 : '_')
				 . $5/e;
    print  INDEX $fsz;
    printf INDEX "  %04d-%02d-%02d", $fy, $fm, $fd;
    printf INDEX "  %s", htqq ($de), htq ($de);
    print  INDEX "\n";
  }
  print INDEX "    
\n"; print INDEX < EOF } # # htq - HTML-quote a string # sub htq ($) { my ($text) = @_; $text =~ s/&/\&/g; $text =~ s//\>/g; return $text; } # # htqq - HTML-quote a string for use as an attribute value # sub htqq ($) { my ($text) = @_; $text = htq ($text); $text =~ s/"/\"/g; return $text; }