#!/usr/bin/perl
# This is the index for <dir>.
use strict;
use warnings FATAL => qw( all );
use CGI::Carp qw(fatalsToBrowser);
use CGI::Minimal;
use HTML::Entities qw(encode_entities);
use lib '../files/lib';
use Base::Page qw(page story);
use HTML::Elements qw(list anchor);
use Util::Convert qw(searchify);
use Util::Data qw(file_directory file_list);
use Util::Menu qw(file_menu);
use Util::Sort qw(article_sort);
my $cgi = CGI::Minimal->new;
my $page = $cgi->param('page') ? encode_entities($cgi->param('pa
+ge'),'/<>"') : undef;
my $pages_dir = file_directory(<dir>, 'text');
my @pages_list = file_list($pages_dir);
my @pages = sort { article_sort($a, $b) }
map { $_ =~ s/\.txt//; $_ =~ s/_/ /g; $_ }
grep { -f "$pages_dir/$_" && /^\p{uppercase}/ }
@pages_list;
my $heading = q(<the heading for the index>);
my $page_file = "$pages_dir/index.txt";
if ( $page && grep { $_ eq $page } @pages ) {
$heading = $page;
$page_file = "$pages_dir/$page.txt";
$page_file =~ s/ /_/g;
}
open(my $page_fh, '<', $page_file) || die "Can't open $page_file. $!";
my $magic;
$magic->{'pages'} = sub {
my $file_menu = file_menu('page', \@pages, $page);
list(4, 'u', $file_menu);
};
page(
'heading' => $heading,
'selected' => $page,
'code' => sub {
story($page_fh, { 'doc magic' => $magic, 'line magic' => $magic })
+;
}
);
sub base_menu {
my %opt = @_;
my $directory = $opt{'directory'};
my $root_path = $opt{'root path'};
my $curr_cwd = cwd;
my @contents = file_list($directory);
@contents = grep {/^\p{uppercase}/} @contents if (!$opt{'full'} || $
+opt{'full'} !~ /^[yt1]/); # Thank you [tye]!
# Thank you [davido]!
my $sub = $directory =~ /(Other_poets|Player_characters|Spellbooks)$
+/ ? \&name_sort : \&article_sort;
@contents = sort { $sub->($a, $b, { 'misc' => $opt{'misc'} }) } @con
+tents;
my (@files, @directories);
if (!$opt{'full'} || $opt{'full'} !~ /^[yt1]/) {
my $text_dir = $directory;
$text_dir =~ s|(\Q$root_path\E)|$1/files/text|;
my @text_files = sort { $sub->($a, $b, { 'misc' => $opt{'misc'} })
+ } file_list($text_dir, { 'uppercase' => 1 }) if -e $text_dir;
for my $text_file (@text_files) {
my $text = textify($text_file);
my $select = searchify($text_file);
my $link = File::Spec->abs2rel("$directory/index.pl?page=$sele
+ct");
my $class = $opt{'selected'} && $opt{'selected'} eq $text ? 'ac
+tive' : 'inactive';
my $anchor = anchor($text, { 'href' => $link, 'title' => $text})
+;
push @files, [$anchor, { 'class' => $class }] if -f "$text_dir/$
+text_file";
}
}
for my $content (@contents) {
my $long_content = "$directory/$content";
my $link = File::Spec->abs2rel($long_content);
my $text = $content !~ /^\./ ? textify($content) : $content;
if (-f $long_content) {
my $active = realpath($0) eq $long_content ? 'active' : 'inactiv
+e';
my $color = $opt{'color'} == 1 ? link_color($content,1) : undef
+;
my $inlist = $active eq 'active' && $opt{'file menu'} ? ['u', $o
+pt{'file menu'}, { 'class' => 'sub_menu' }] : undef;
$active .= $active eq 'active' && $opt{'file menu'} ? ' open'
+: '';
if (-M $long_content < 3) {
$active .= ' updated';
}
my $anchor = anchor($text, { 'href' => $link, 'title' => $text,
+'style' => $color });
push @files, [$anchor, { 'class' => $active, 'inlist' => $inlist
+ }];
}
if (-d $long_content) {
my $active = $curr_cwd =~ /$long_content/ ? 'open active' : 'clo
+sed inactive';
my $index = "$long_content/index.pl";
my $file_list;
my $anchor;
if (-e $index && (!$opt{'full'} || $opt{'full'} !~ /^[yt1]/)) {
$link .= "/index.pl";
my $color = $opt{'color'} == 1 ? link_color($link,1) : undef;
$anchor = anchor($text, { 'href' => $link, 'title' => $text, '
+style' => $color });
$file_list = "$curr_cwd/$0" =~ /$index/ && $active =~ / active
+$/ && $opt{'file menu'} ? $opt{'file menu'} : undef;
}
my $next_list = base_menu(
'directory' => $long_content,
'color' => $opt{'color'},
'full' => $opt{'full'},
'misc' => $opt{'misc'},
'selected' => $opt{'selected'},
'file menu' => $opt{'file menu'},
'root path' => $opt{'root path'}
);
push @$next_list, @$file_list if $file_list;
my $inlist = $next_list ? ['u', $next_list] : undef;
$active =~ s/^(?:open|closed) // if !$inlist;
push @directories, [$anchor ? $anchor : $text, { 'class' => $act
+ive, 'inlist' => $inlist}];
}
}
my @file_lines = (@files, @directories);
return @file_lines > 0 ? \@file_lines : undef;
}