| 1 | package DocLife; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use parent qw( Plack::Component ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Path::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Plack::Util::Accessor qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | base_url | ||||||||||||||||||||||||||||
| 17 | root | ||||||||||||||||||||||||||||
| 18 | suffix | ||||||||||||||||||||||||||||
| 19 | 4 | ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 15 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 15 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 23 | 15 | ▶ | $self->base_url('/') unless defined $self->base_url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 15 | ▶ | $self->root(dir(abs_path($self->root || './'))); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->root || './'
MC/DC0%0/1
$self->root || './'
$self->root
Truth table50%1/2
A
A= $self->root
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 15 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 15 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 15 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||
| 31 | 15 | my $res = $req->new_response(200); | |||||||||||||||||||||||||||
| 32 | 15 | $res->content_type('text/html; charset=UTF-8'); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 15 | ▶ | if ($req->path eq '/') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 35 | 0 | $self->toppage($req, $res); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | ▶ | elsif ($req->path=~m|\Q/../| or $req->path=~m|\Q//|) { | |||||||||||||||||||||||||||
|
Condition66%2/3
$req->path =~ m[\/\.\.\/] or $req->path =~ m[\/\/]
MC/DC50%1/2
$req->path =~ m[\/\.\.\/] or $req->path =~ m[\/\/]
$req->path =~ m[\/\.\.\/]
$req->path =~ m[\/\/]
Truth table66%2/3
A or B
A= $req->path =~ m[\/\.\.\/]B= $req->path =~ m[\/\/]
| |||||||||||||||||||||||||||||
| 38 | 3 | $self->forbidden($req, $res); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | else { | ||||||||||||||||||||||||||||
| 41 | 12 | $self->page($req, $res); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 15 | $res->finalize; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 3 | ▶ | sub format { | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 3 | my ($self, $req, $res, $file) = @_; | |||||||||||||||||||||||||||
| 48 | 3 | $res->content_type('text/plain; charset=UTF-8'); | |||||||||||||||||||||||||||
| 49 | 3 | $res->body($file->slurp); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 12 | ▶ | sub page { | ||||||||||||||||||||||||||
|
Subroutine
page: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 12 | my ($self, $req, $res) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 12 | my $file = file($self->root, $req->path); | |||||||||||||||||||||||||||
| 56 | 12 | ▶ | $file = file($self->root, $req->path . $self->suffix) if !-f $file and defined $self->suffix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not -f $file and defined $self->suffix
MC/DC100%2/2
not -f $file and defined $self->suffix
not -f $file
defined $self->suffix
Truth table100%3/3
not(A) and B
A= not -f $fileB= defined $self->suffix
| |||||||||||||||||||||||||||||
| 57 | 12 | ▶ | if (-f $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 9 | $self->format($req, $res, $file); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 3 | $self->not_found($req, $res); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub toppage { | ||||||||||||||||||||||||||
|
Subroutine
toppage: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ($self, $req, $res) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $body = $self->html_header; | |||||||||||||||||||||||||||
| 69 | 0 | my $suffix = $self->suffix; | |||||||||||||||||||||||||||
| 70 | 0 | my $root = $self->root; | |||||||||||||||||||||||||||
| 71 | 0 | my @files; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | find( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return unless -f $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return if length $suffix and $_!~m|\Q$suffix\E$|; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $suffix and not $_ =~ /\Q$suffix\E$/
MC/DC0%0/2
length $suffix and not $_ =~ /\Q$suffix\E$/
length $suffix
not $_ =~ /\Q$suffix\E$/
Truth table0%0/3
A and not(B)
A= length $suffixB= not $_ =~ /\Q$suffix\E$/
| |||||||||||||||||||||||||||||
| 75 | 0 | push @files, $File::Find::name; | |||||||||||||||||||||||||||
| 76 | 0 | }, $root ); | |||||||||||||||||||||||||||
| 77 | 0 | $body.= "<ul>\n"; | |||||||||||||||||||||||||||
| 78 | 0 | for my $file ( sort @files ) { | |||||||||||||||||||||||||||
| 79 | 0 | $file=~s|^\Q$root\E/?||; | |||||||||||||||||||||||||||
| 80 | 0 | my $url = $self->base_url . uri_escape($file); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (length $suffix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $file=~s|\Q$suffix\E$||; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | $body.= qq{<li><a href="$url">$file</a></li>\n}; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 0 | $body.= "</ul>\n"; | |||||||||||||||||||||||||||
| 87 | 0 | $body.= $self->html_footer; | |||||||||||||||||||||||||||
| 88 | 0 | $res->body($body); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | sub forbidden { | ||||||||||||||||||||||||||
|
Subroutine
forbidden: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 3 | my ($self, $req, $res) = @_; | |||||||||||||||||||||||||||
| 93 | 3 | $res->status(403); | |||||||||||||||||||||||||||
| 94 | 3 | $res->body('Forbidden.'); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | sub not_found { | ||||||||||||||||||||||||||
|
Subroutine
not_found: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 3 | my ($self, $req, $res) = @_; | |||||||||||||||||||||||||||
| 99 | 3 | $res->status(404); | |||||||||||||||||||||||||||
| 100 | 3 | $res->body('Not Found.'); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub html_header { | ||||||||||||||||||||||||||
|
Subroutine
html_header: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | <<"EOF" | ||||||||||||||||||||||||||||
| 105 | <!DOCTYPE html> | ||||||||||||||||||||||||||||
| 106 | <html> | ||||||||||||||||||||||||||||
| 107 | <head> | ||||||||||||||||||||||||||||
| 108 | <title>Index</title> | ||||||||||||||||||||||||||||
| 109 | </head> | ||||||||||||||||||||||||||||
| 110 | <body> | ||||||||||||||||||||||||||||
| 111 | EOF | ||||||||||||||||||||||||||||
| 112 | 0 | } | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub html_footer { | ||||||||||||||||||||||||||
|
Subroutine
html_footer: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | <<"EOF" | ||||||||||||||||||||||||||||
| 116 | </body> | ||||||||||||||||||||||||||||
| 117 | </html> | ||||||||||||||||||||||||||||
| 118 | EOF | ||||||||||||||||||||||||||||
| 119 | 0 | } | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 NAME | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | DocLife - Document Viewer written in Perl, to run under Plack. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # app.psgi | ||||||||||||||||||||||||||||
| 128 | use DocLife::Pod; | ||||||||||||||||||||||||||||
| 129 | DocLife::Pod->new( root => "./lib" ); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # one-liner | ||||||||||||||||||||||||||||
| 132 | plackup -MDocLife::Pod -e 'DocLife::Pod->new( root => "./lib" )->to_app' | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 How To Mount | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | need base_url option. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # app.psgi | ||||||||||||||||||||||||||||
| 139 | use Plack::Builder; | ||||||||||||||||||||||||||||
| 140 | use DocLife::Pod; | ||||||||||||||||||||||||||||
| 141 | use DocLife::Markdown; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | my $pod_app = DocLife::Pod->new( | ||||||||||||||||||||||||||||
| 144 | root => '../lib', | ||||||||||||||||||||||||||||
| 145 | base_url => '/pod/' | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | my $doc_app = DocLife::Markdown->new( | ||||||||||||||||||||||||||||
| 149 | root => './doc', | ||||||||||||||||||||||||||||
| 150 | suffix => '.md', | ||||||||||||||||||||||||||||
| 151 | base_url => '/doc/' | ||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | builder { | ||||||||||||||||||||||||||||
| 155 | mount '/pod' => $pod_app; | ||||||||||||||||||||||||||||
| 156 | mount '/doc' => $doc_app; | ||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =over 4 | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item root | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Document root directory. Defaults to the current directory. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =back | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =over 4 | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item base_url | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Specifies a base URL for all URLs on a index page. Defaults to the `/`. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =back | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =over 4 | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =item suffix | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Show only files that match the suffix. No url suffix. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =back | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | L<Plack> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Copyright 2013 Shinichiro Aska | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 4 | 1; | |||||||||||||||||||||||||||