App-htidx

 view release on metacpan or  search on metacpan

lib/App/htidx.pm  view on Meta::CPAN

sub mkindex {
    my ($dir, $toplevel) = @_;

    printf STDERR "htidx: generating directory listing for %s...\n", $dir;

    my $index = File::Spec->catfile($dir, INDEX_FILE);

    opendir(my $dh, $dir);

    my @entries = sort grep { 0 != index($_, '.') } readdir($dh);

    closedir($dh);

    my (@dirs, @files);
    foreach my $entry (sort(@entries)) {
        my $path = File::Spec->catfile($dir, $entry);

        if (-d $path || (-l $path && -d readlink($path))) {
            push(@dirs, $entry);

        } elsif (lc($entry) ne lc(INDEX_FILE)) {
            push(@files, $entry);

        }
    }

    my $mkhtml = scalar(grep { /^index\./i } @files) < 1;

    if (-e $index) {
        open(my $fh, $index);

        while (!$fh->eof) {
            if (index($fh->getline, SIGNATURE) >= 0) {
                $mkhtml = 1;
                last;
            }
        }

        $fh->close;
    }

    mkhtml($index, $dir, $toplevel, \@dirs, \@files) if ($mkhtml);

    map { mkindex(File::Spec->catfile($dir, $_)) } @dirs;
}

sub mkhtml {
    my ($index, $dir, $toplevel, $dref, $fref) = @_;

    my $tmpfile = File::Temp::tempnam($dir, '.mkindex');

    open(my $fh, '>', $index) || die("$index: $!");

    my $title = sprintf('Directory listing of %s/', basename($dir));

    $fh->say('<!doctype html>');
    $fh->say(sprintf('<!-- %s -->', he(SIGNATURE)));
    $fh->say($H->open('html', {lang => 'en'}));

    $fh->say($H->head([
    $H->meta({charset => 'UTF-8'}),
        $H->title(he($title)),
        $H->meta({name => 'generator', content => sprintf('%s v%s', __PACKAGE__, $VERSION)}),
        $H->meta({name => 'viewport', content => 'width=device-width'}),
        $H->style(he($CSS)),
    ]));

    $fh->say($H->open('body', { class => 'htidx-body' }));

    $fh->say($H->h1($title));

    $fh->say($H->open('table'));

    $fh->say($H->thead($H->tr([map { $H->th($_) } ('Name', 'Last Modified', 'Size') ])));

    $fh->say($H->open('tbody'));

    $fh->say($H->tr(
        { class => 'htidx-directory' },
        [
            $H->td($H->a(
                { href => '../'},
                'Parent Directory'
            )),
            $H->td(strftime(TIMEFMT, localtime(stat(File::Spec->catfile(dirname($dir)))->mtime))),
            $H->td('-')
        ]
    )) unless ($toplevel);

    foreach my $entry (@{$dref}) {
        $fh->say($H->tr(
            { class => 'htidx-directory' },
            [ map { $H->td($_) } (
                $H->a(
                    { href => uri_escape($entry).'/'},
                    he($entry.'/')
                ),
                strftime(TIMEFMT, localtime(stat(File::Spec->catfile($dir, $entry))->mtime)),
                '-',
            ) ]
        ));
    }

    foreach my $entry (@{$fref}) {
        my $stat = stat(File::Spec->catfile($dir, $entry));

        $fh->say($H->tr(
            { class => 'htidx-file' },
            [ map { $H->td($_) } (
                $H->a(
                    { href => uri_escape($entry) },
                    he($entry)
                ),
                strftime(TIMEFMT, localtime($stat->mtime)),
                fsize($stat->size),
            ) ]
        ));
    }

    map { $fh->say($H->close($_)) } qw(tbody table body html);



( run in 2.325 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )