Last active
August 17, 2020 16:21
-
-
Save LadyAleena/0944f7fb34f0d59ac1588a81b5fcf294 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Page::File; # temporary name | |
use v5.8.8; | |
use strict; | |
use warnings; | |
use Exporter qw(import); | |
our @EXPORT_OK = qw(file_directory file_list file_menu); | |
use File::Spec; | |
use Page::Path qw(base_path); | |
use HTML::Elements qw(anchor); | |
use Util::Convert qw(textify searchify); | |
use Util::Sort qw(article_sort name_sort); | |
# file_directory returns the directory by type of data wanted. | |
## The default data directory is 'data'. | |
## Other options for type are: | |
### text returns the text file for various pages. | |
### audio, images, and css return urls. | |
### imagesd returns the images directory, but not in url format. | |
sub file_directory { | |
my ($dir, $type) = @_; | |
$dir =~ s/ /_/g; | |
$type = $type ? $type : 'data'; | |
return base_path($type)."/$dir"; | |
} | |
# file_list returns a list of the contents in a directory but is not recursive. | |
## There are options: | |
### 'type' returns only the wanted type files or directories. | |
### 'uppercase' returns only files that begin with an initial uppercase letter. | |
### 'sort' returns a the list sorted. The options are 'article' or 'name'. | |
### 'full path' returns the list with the files' full paths. | |
sub file_list { | |
my ($directory, $opt) = @_; | |
opendir(my $dir, $directory) || die "Can't open $directory. $!"; | |
my @files = File::Spec->no_upwards(readdir($dir)); | |
closedir($dir); | |
chomp @files; | |
@files = grep { -f "$directory/$_" } @files if $opt->{'type'} && $opt->{'type'} =~ /^f/; | |
@files = grep { -d "$directory/$_" } @files if $opt->{'type'} && $opt->{'type'} =~ /^d/; | |
@files = grep { /^\p{uppercase}/ } @files if $opt->{'uppercase'} && $opt->{'uppercase'} =~ /^[yt1]/; # Thank you [tye]! | |
if ($opt->{'sort'}) { | |
my $sort = $opt->{'sort'}; | |
my $sort_sub = $sort eq 'name' ? \&name_sort : | |
$sort eq 'article' ? \&article_sort : | |
undef; | |
if ($sort_sub) { | |
@files = sort { $sort_sub->($a, $b) } @files; | |
} | |
else { | |
@files = sort @files; | |
} | |
} | |
@files = map { "$directory/$_" } @files if $opt->{'full path'} && $opt->{'full path'} =~ /^[yt1]/; | |
return @files | |
} | |
# file_menu returns a list of links for any sub pages generated by cgi param. | |
## The first parameter is the name of the parameter being used by cgi param. | |
## The second parameter is the array ref for the list of parsed files to remove the | |
## file extensions and any underscores. | |
## The third parameter is the selected option of the files to give it the class "active". | |
sub file_menu { | |
my($param, $list, $select) = @_; | |
my @params = map { | |
[ | |
anchor(textify($_, { 'parens' => 'yes' }), { 'href' => '?'.searchify($param).'='.searchify($_), 'title' => textify($_) }), | |
{ 'class' => $select && $select eq $_ ? 'active' : 'inactive' } | |
] | |
} @$list; | |
return \@params; | |
} | |
=pod | |
=encoding utf8 | |
=head1 AUTHOR | |
Lady Aleena | |
=head1 LICENSE AND COPYRIGHT | |
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<perlartistic>. | |
Copyright © 2020, Lady Aleena C<([email protected])>. All rights reserved. | |
=cut | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment