| 1 | package Mojolicious::Plugin::Directory::Stylish; | ||||||||||||||||||||||||||||||
| 2 | 10 | $Mojolicious::Plugin::Directory::Stylish::VERSION = '1.006'; | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Serve static files from document root with directory index using Mojolicious templates | ||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Encode (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use DirHandle; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Mojo::Base qw{ Mojolicious::Plugin }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use Mojolicious::Types; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use Mojo::Asset::File; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use Mojo::File; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 10 | my $types = Mojolicious::Types->new; | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 9 | ▶ | sub register { | ||||||||||||||||||||||||||||
|
Subroutine
register: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 18 | 9 | my ( $self, $app, $args ) = @_; | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | 9 | ▶ | my $root = Mojo::File->new( $args->{root} || Cwd::getcwd ); | ||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{'root'} || Cwd::getcwd()
MC/DC0%0/2
$args->{'root'} || Cwd::getcwd()
$args->{'root'}
Cwd::getcwd()
Truth table66%2/3
A || B
A= $args->{'root'}B= Cwd::getcwd()
| |||||||||||||||||||||||||||||||
| 21 | 9 | my $handler = $args->{handler}; | |||||||||||||||||||||||||||||
| 22 | 9 | my $index = $args->{dir_index}; | |||||||||||||||||||||||||||||
| 23 | 9 | my $enable_json = $args->{enable_json}; | |||||||||||||||||||||||||||||
| 24 | 9 | ▶ | my $auto_index = $args->{auto_index} // 1; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"auto_index"} // 1
MC/DC100%1/1
$args->{"auto_index"} // 1
$args->{"auto_index"}
Truth table100%2/2
A
A= $args->{"auto_index"}
| |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 9 | ▶ | my $css = $args->{css} || 'style'; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'css'} || 'style'
MC/DC100%1/1
$args->{'css'} || 'style'
$args->{'css'}
Truth table100%2/2
A
A= $args->{'css'}
| |||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | my $render_opts = $args->{render_opts} || {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'render_opts'} || {}
MC/DC0%0/1
$args->{'render_opts'} || {}
$args->{'render_opts'}
Truth table50%1/2
A
A= $args->{'render_opts'}
| |||||||||||||||||||||||||||||||
| 28 | 9 | ▶ | $render_opts->{template} = $args->{dir_template} || 'list'; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'dir_template'} || 'list'
MC/DC100%1/1
$args->{'dir_template'} || 'list'
$args->{'dir_template'}
Truth table100%2/2
A
A= $args->{'dir_template'}
| |||||||||||||||||||||||||||||||
| 29 | 9 | push @{ $app->renderer->classes }, __PACKAGE__; | |||||||||||||||||||||||||||||
| 30 | 9 | push @{ $app->static->classes }, __PACKAGE__; | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||
| 32 | $app->hook( | ||||||||||||||||||||||||||||||
| 33 | 26 | ▶ | before_dispatch => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||||
| 34 | 26 | my $c = shift; | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | 26 | ▶ | return render_file( $c, $root ) if ( -f $root->to_string() ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||
| 38 | 24 | my $child = Mojo::Util::url_unescape( $c->req->url->path ); | |||||||||||||||||||||||||||||
| 39 | 24 | $child =~ s!^/!!g; | |||||||||||||||||||||||||||||
| 40 | 24 | my $path = $root->child( $child ); | |||||||||||||||||||||||||||||
| 41 | 24 | ▶ | $handler->( $c, $path ) if ( ref $handler eq 'CODE' ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | 24 | ▶ | if ( -f $path ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 44 | 11 | ▶ | render_file( $c, $path ) unless ( $c->tx->res->code ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||
| 46 | elsif ( -d $path ) { | ||||||||||||||||||||||||||||||
| 47 | 13 | ▶ | if ( $index && ( my $file = locate_index( $index, $path ) ) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$index and my $file = locate_index($index, $path)
MC/DC50%1/2
$index and my $file = locate_index($index, $path)
$index
my $file = locate_index($index, $path)
Truth table66%2/3
A and B
A= $indexB= my $file = locate_index($index, $path)
| |||||||||||||||||||||||||||||||
| 48 | 1 | return render_file( $c, $file ); | |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||
| 50 | 12 | ▶ | if ( $auto_index ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 51 | 11 | ▶ | $c->stash(css => $css), | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 52 | render_indexes( $c, $path, $render_opts, $enable_json ) | ||||||||||||||||||||||||||||||
| 53 | unless ( $c->tx->res->code ); | ||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||
| 57 | 9 | ); | |||||||||||||||||||||||||||||
| 58 | 9 | return $app; | |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub locate_index { | ||||||||||||||||||||||||||||
|
Subroutine
locate_index: called CC=6 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | my $index = shift || return; | ||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | my $dir = shift || Cwd::getcwd; | ||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || Cwd::getcwd()
MC/DC0%0/2
shift() || Cwd::getcwd()
shift()
Cwd::getcwd()
Truth table33%1/3
A || B
A= shift()B= Cwd::getcwd()
| |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 1 | my $root = Mojo::Home->new($dir); | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $index = ( ref $index eq 'ARRAY' ) ? $index : ["$index"]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 68 | 1 | for (@$index) { | |||||||||||||||||||||||||||||
| 69 | 1 | my $path = $root->rel_file($_); | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | return $path if ( -e $path ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | sub render_file { | ||||||||||||||||||||||||||||
|
Subroutine
render_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 75 | 3 | my ( $c, $file ) = @_; | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | 3 | my $asset = Mojo::Asset::File->new(path => $file); | |||||||||||||||||||||||||||||
| 78 | 3 | $c->reply->asset($asset); | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 11 | ▶ | sub render_indexes { | ||||||||||||||||||||||||||||
|
Subroutine
render_indexes: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 82 | 11 | my ( $c, $dir, $render_opts, $enable_json ) = @_; | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 11 | ▶ | my @files = | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 85 | ( $c->req->url eq '/' ) | ||||||||||||||||||||||||||||||
| 86 | ? () | ||||||||||||||||||||||||||||||
| 87 | : ( { url => '../', name => 'Parent Directory', size => '', type => '', mtime => '' } ); | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | 11 | my ( $current, $list ) = list_files( $c, $dir ); | |||||||||||||||||||||||||||||
| 90 | 11 | push @files, @$list; | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | 11 | $c->stash( files => \@files ); | |||||||||||||||||||||||||||||
| 93 | 11 | $c->stash( current => $current ); | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | 11 | my %respond = ( any => $render_opts ); | |||||||||||||||||||||||||||||
| 96 | 11 | ▶ | $respond{json} = { json => { files => \@files, current => $current } } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 97 | if ($enable_json); | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | 11 | $c->respond_to(%respond); | |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | 11 | ▶ | sub list_files { | ||||||||||||||||||||||||||||
|
Subroutine
list_files: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 103 | 11 | my ( $c, $dir ) = @_; | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | 11 | my $current = Encode::decode_utf8( Mojo::Util::url_unescape( $c->req->url->path ) ); | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | 11 | ▶ | return ( $current, [] ) unless $dir; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | 11 | my $dh = DirHandle->new($dir); | |||||||||||||||||||||||||||||
| 110 | 11 | my @children; | |||||||||||||||||||||||||||||
| 111 | 11 | ▶ | while ( defined( my $ent = $dh->read ) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 112 | 150 | ▶ | next if $ent eq '.' or $ent eq '..'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ent eq "." or $ent eq ".."
MC/DC100%2/2
$ent eq "." or $ent eq ".."
$ent eq "."
$ent eq ".."
Truth table100%3/3
A or B
A= $ent eq "."B= $ent eq ".."
| |||||||||||||||||||||||||||||||
| 113 | 128 | push @children, Encode::decode_utf8($ent); | |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | 11 | my @files; | |||||||||||||||||||||||||||||
| 117 | 11 | for my $basename ( sort { $a cmp $b } @children ) { | |||||||||||||||||||||||||||||
| 118 | 128 | my $file = "$dir/$basename"; | |||||||||||||||||||||||||||||
| 119 | 128 | my $url = Mojo::Path->new($current)->trailing_slash(0); | |||||||||||||||||||||||||||||
| 120 | 128 | push @{ $url->parts }, $basename; | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | 128 | my $is_dir = -d $file; | |||||||||||||||||||||||||||||
| 123 | 128 | my @stat = stat _; | |||||||||||||||||||||||||||||
| 124 | 128 | ▶ | if ($is_dir) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 125 | 14 | $basename .= '/'; | |||||||||||||||||||||||||||||
| 126 | 14 | $url->trailing_slash(1); | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | 128 | ▶ | my $mime_type = | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$types->type(get_ext($file) || "txt") or "text/plain"
get_ext($file) || 'txt'
MC/DC100%1/1
$types->type(get_ext($file) || "txt") or "text/plain"
$types->type(get_ext($file) || "txt")
MC/DC100%1/1
get_ext($file) || 'txt'
get_ext($file)
Truth table100%2/2
A
A= $types->type(get_ext($file) || "txt")
Truth table100%2/2
A
A= get_ext($file)
| |||||||||||||||||||||||||||||||
| 130 | ($is_dir) | ||||||||||||||||||||||||||||||
| 131 | ? 'directory' | ||||||||||||||||||||||||||||||
| 132 | : ( $types->type( get_ext($file) || 'txt' ) || 'text/plain' ); | ||||||||||||||||||||||||||||||
| 133 | 128 | my $mtime = Mojo::Date->new( $stat[9] )->to_string(); | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 128 | ▶ | push @files, { | ||||||||||||||||||||||||||||
|
Condition100%2/2
$stat[7] || 0
MC/DC100%1/1
$stat[7] || 0
$stat[7]
Truth table100%2/2
A
A= $stat[7]
| |||||||||||||||||||||||||||||||
| 136 | url => $url, | ||||||||||||||||||||||||||||||
| 137 | name => $basename, | ||||||||||||||||||||||||||||||
| 138 | size => $stat[7] || 0, | ||||||||||||||||||||||||||||||
| 139 | type => $mime_type, | ||||||||||||||||||||||||||||||
| 140 | mtime => $mtime, | ||||||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | 11 | return ( $current, \@files ); | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | 114 | ▶ | sub get_ext { | ||||||||||||||||||||||||||||
|
Subroutine
get_ext: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 148 | 114 | ▶ | $_[0] =~ /\.([0-9a-zA-Z]+)$/ || return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 149 | 108 | return lc $1; | |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||
| 152 | 10 | 1; | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | =pod | ||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||
| 156 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||
| 158 | =head1 NAME | ||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | Mojolicious::Plugin::Directory::Stylish - Serve static files from document root with directory index using Mojolicious templates | ||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | version 1.006 | ||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||
| 168 | use Mojolicious::Lite; | ||||||||||||||||||||||||||||||
| 169 | plugin 'Directory::Stylish'; | ||||||||||||||||||||||||||||||
| 170 | app->start; | ||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | or | ||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||
| 174 | > perl -Mojo -E 'a->plugin("Directory::Stylish")->start' daemon | ||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||
| 178 | L<Mojolicious::Plugin::Directory::Stylish> is a static file server directory index a la Apache's mod_autoindex. | ||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | =head1 METHODS | ||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||
| 182 | L<Mojolicious::Plugin::Directory::Stylish> inherits all methods from L<Mojolicious::Plugin>. | ||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||
| 184 | =head1 OPTIONS | ||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||
| 186 | L<Mojolicious::Plugin::Directory::Stylish> supports the following options. | ||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||
| 188 | =head2 C<root> | ||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | plugin 'Directory::Stylish' => { root => "/path/to/htdocs" }; | ||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | Document root directory. Defaults to the current directory. | ||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||
| 194 | If root is a file, serve only root file. | ||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||
| 196 | =head2 C<auto_index> | ||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||
| 198 | # Mojolicious::Lite | ||||||||||||||||||||||||||||||
| 199 | plugin 'Directory::Stylish' => { auto_index => 0 }; | ||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||
| 201 | Automatically generate index page for directory, default true. | ||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||
| 203 | =head2 C<dir_index> | ||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||
| 205 | plugin 'Directory::Stylish' => { dir_index => [qw/index.html index.htm/] }; | ||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||
| 207 | Like a Apache's DirectoryIndex directive. | ||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||
| 209 | =head2 C<dir_template> | ||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | plugin 'Directory::Stylish' => { dir_template => 'index' }; | ||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||
| 213 | # with 'render_opts' option | ||||||||||||||||||||||||||||||
| 214 | plugin 'Directory::Stylish' => { | ||||||||||||||||||||||||||||||
| 215 | dir_template => 'index', | ||||||||||||||||||||||||||||||
| 216 | render_opts => { format => 'html', handler => 'ep' }, | ||||||||||||||||||||||||||||||
| 217 | }; | ||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||
| 219 | ... | ||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||
| 221 | __DATA__ | ||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||
| 223 | @@ index.html.ep | ||||||||||||||||||||||||||||||
| 224 | % layout 'default'; | ||||||||||||||||||||||||||||||
| 225 | % title 'DirectoryIndex'; | ||||||||||||||||||||||||||||||
| 226 | <h1>Index of <%= $current %></h1> | ||||||||||||||||||||||||||||||
| 227 | <ul> | ||||||||||||||||||||||||||||||
| 228 | % for my $file (@$files) { | ||||||||||||||||||||||||||||||
| 229 | <li><a href='<%= $file->{url} %>'><%== $file->{name} %></a></li> | ||||||||||||||||||||||||||||||
| 230 | % } | ||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||
| 232 | @@ layouts/default.html.ep | ||||||||||||||||||||||||||||||
| 233 | <!DOCTYPE html> | ||||||||||||||||||||||||||||||
| 234 | <html> | ||||||||||||||||||||||||||||||
| 235 | <head><title><%= title %></title></head> | ||||||||||||||||||||||||||||||
| 236 | <body><%= content %></body> | ||||||||||||||||||||||||||||||
| 237 | %= include $css; | ||||||||||||||||||||||||||||||
| 238 | </html> | ||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||
| 240 | A name for the template to use for the index page. | ||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||
| 242 | "$files", "$current", and "$css" are passed in stash. | ||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||
| 244 | =over 2 | ||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||
| 246 | =item * $files: Array[Hash] | ||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||
| 248 | list of files and directories | ||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||
| 250 | =item * $current: String | ||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||
| 252 | current path | ||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||
| 254 | =item * $css: String | ||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||
| 256 | name of template with css that you want to include | ||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||
| 258 | =back | ||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||
| 260 | =head2 C<handler> | ||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||
| 262 | use Text::Markdown qw{ markdown }; | ||||||||||||||||||||||||||||||
| 263 | use Path::Class; | ||||||||||||||||||||||||||||||
| 264 | use Encode qw{ decode_utf8 }; | ||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||
| 266 | plugin 'Directory::Stylish' => { | ||||||||||||||||||||||||||||||
| 267 | handler => sub { | ||||||||||||||||||||||||||||||
| 268 | my ($c, $path) = @_; | ||||||||||||||||||||||||||||||
| 269 | if ($path =~ /\.(md|mkdn)$/) { | ||||||||||||||||||||||||||||||
| 270 | my $text = file($path)->slurp; | ||||||||||||||||||||||||||||||
| 271 | my $html = markdown( decode_utf8($text) ); | ||||||||||||||||||||||||||||||
| 272 | $c->render( inline => $html ); | ||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||
| 275 | }; | ||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||
| 277 | CODEREF for handle a request file. | ||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||
| 279 | If not rendered in CODEREF, serve as static file. | ||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||
| 281 | =head2 C<enable_json> | ||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||
| 283 | # http://host/directory?format=json | ||||||||||||||||||||||||||||||
| 284 | plugin 'Directory::Stylish' => { enable_json => 1 }; | ||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||
| 286 | enable json response. | ||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||
| 288 | =head2 C<css> | ||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||
| 290 | plugin 'Directory::Stylish' => { css => 'custom_template' }; | ||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||
| 292 | ... | ||||||||||||||||||||||||||||||
| 293 | __DATA__ | ||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||
| 295 | @@ custom_template.html.ep | ||||||||||||||||||||||||||||||
| 296 | <style type="text/css"> | ||||||||||||||||||||||||||||||
| 297 | body { background: black; color: white; } | ||||||||||||||||||||||||||||||
| 298 | </style> | ||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||
| 300 | A name for the template with css that will be included by the default template | ||||||||||||||||||||||||||||||
| 301 | for the index. | ||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||
| 303 | This name will be available as C<$css> in the stash. | ||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||
| 305 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||
| 307 | Many thanks to the contributors for their work. | ||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||
| 309 | =over 2 | ||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||
| 311 | =item * ChinaXing | ||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||
| 313 | =item * Su-Shee | ||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||
| 315 | =back | ||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||
| 317 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||
| 319 | =over 2 | ||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||
| 321 | =item * L<Mojolicious::Plugin::Directory> | ||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||
| 323 | =item * L<Plack::App::Directory> | ||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||
| 325 | =back | ||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||
| 327 | =head1 ORIGINAL AUTHOR | ||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||
| 329 | hayajo E<lt>hayajo@cpan.orgE<gt> - Original author of L<Mojolicious::Plugin::Directory> | ||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||
| 331 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||
| 333 | Andreas Guldstrand <andreas.guldstrand@gmail.com> | ||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||
| 335 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||
| 337 | This software is copyright (c) 2017 by Hayato Imai, Andreas Guldstrand. | ||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||
| 339 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||
| 340 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||
| 342 | =cut | ||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||
| 344 | __DATA__ | ||||||||||||||||||||||||||||||