lib/MojoMojo/Formatter/Dir.pm

filter: Statementstmt 100% 90 / 90 Branchbran 83.3% 30 / 36 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 95.8% 140 / 146 CC 23 SCAR 21.1
CC
23
Cov
95%
to_xhtml
14.1
checkdir
6.0
format_content
5.0
CRAP 8.3
« lib/Text/Wikispaces2Markdown.pm ↑ Summary lib/MojoMojo/Schema/Result/Person.pm »
1 package MojoMojo::Formatter::Dir;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use base qw/MojoMojo::Formatter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use Path::Class ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use MojoMojo::Formatter::File::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 26 my $debug=0;
10  
11 =head1 NAME
12  
13 MojoMojo::Formatter::Dir - format local directory as XHTML
14  
15 =head1 DESCRIPTION
16  
17 This formatter will format the directory argument as XHTML.
18 Usage:
19  
20 {{dir directory exclude=exclude_regex}}
21  
22  
23 For security reasons the directory must be include in 'whitelisting'. You can use path_to(DIR) to describe directory in mojomojo.conf:
24  
25 <Formatter::Dir>
26 prefix_url /myfiles
27 whitelisting __path_to(uploads)__
28 </Formatter::Dir>
29  
30  
31 =head1 METHODS
32  
33 =over 4
34  
35 =item format_content_order
36  
37 Format order can be 1-99. The File formatter runs on 92.
38  
39 =cut
40  
41 620 ▶sub format_content_order { 92 }
Subroutine format_content_order: called CC=1 SCAR=0.0
Pod documented
42  
43 =item format_content
44  
45 Calls the formatter. Takes a ref to the content as well as the
46 context object.
47  
48 =cut
49  
50  
51 128 ▶sub format_content {
Subroutine format_content: called CC=5 SCAR=16.1
Pod documented
52 128 my ( $self, $content, $c ) = @_;
53  
54  
55 # TODO : Add cache if directory is not modified
56  
57 128 my @lines = split /\n/, $$content;
58  
59 128 $$content = "";
60 128 foreach my $line (@lines) {
61  
62 734 ▶ if ( $line =~ m|<p>\{\{dir\s*(\S*)\s*(\S*)}}</p>| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[<p>\{\{dir\s*(\S*)\s*(\S*)}}</p>]) { } 4 730 734
63 4 my $dir = $1;
64 4 my $exclude = $2;
65  
66 4 $exclude =~ s/exclude=//;
67  
68 4 my $path_to = $c->path_to();
69 # use path_to(dir) ?
70 4 $dir =~ s/path_to\((\S*)\)/${path_to}\/$1/;
71 4 $dir =~ s/\/$//;
72  
73 4 my $error;
74 4 ▶ if ( $error = $self->checkdir($dir, $c)){
Branch: 1 branch
BranchTrueFalseTotal
if ($error = $self->checkdir($dir, $c)) 3 1 4
75 3 $$content .= $error;
76 }
77  
78 4 ▶ if ( ! $error ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 1 3 4
79 # format with plugin
80 1 $$content .= $self->format($dir, $exclude, $c);
81 }
82 }
83 else{
84 730 $$content .= $line . "\n";
85 }
86 }
87 128 return $content;
88 }
89  
90  
91  
92 =item format
93  
94 Return the content formatted
95  
96 =cut
97  
98 1 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod documented
99 1 my $self = shift;
100 1 my $dir = shift;
101 1 my $exclude = shift;
102 1 my $c = shift;
103  
104 1 my $baseuri = $c->base_uri;
105 1 my $path = $c->stash->{path};
106  
107 1 return $self->to_xhtml($dir, $exclude, $baseuri, $path);
108 }
109  
110  
111 =item to_xhtml
112  
113 Return Directory and files lists in xhtml
114  
115 =cut
116  
117 4 ▶sub to_xhtml{
Subroutine to_xhtml: called CC=14 SCAR=26.4
Pod documented
118 4 my ($self, $dir, $exclude, $baseuri, $path) = @_;
119  
120 4 my $pcdir = Path::Class::dir->new("$dir");
121  
122 4 my @subdirs;
123 my @files;
124 4 ▶ while (my $file = $pcdir->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $pcdir->next) 16 4 20
125 16 ▶ next if ($file =~ m/^$dir\/?\.*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^$dir/?\.*$] 8 8 16
126 8 ▶ next if ( "$exclude" && grep(/$exclude/, $file ));
Branch: 1 branch
BranchTrueFalseTotal
if "$exclude" and grep /$exclude/, $file 2 6 8
Condition100%3/3
"$exclude" and grep /$exclude/, $file
!ll&&!rl&&r
422
MC/DC100%2/2
"$exclude" and grep /$exclude/, $file
"$exclude" grep /$exclude/, $file
Truth table100%3/3
A and B
A= "$exclude"B= grep /$exclude/, $file
ABresult
0X0
100
111
127  
128 6 ▶ if ( -d $file ){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) { } 2 4 6
129 2 push @subdirs , $file;
130 }
131 else{
132 4 push @files, $file;
133 }
134 }
135  
136 #-mxh Sort the array for predictable ordering in formatter_dir.t
137 4 @subdirs = sort @subdirs;
138 4 @files = sort @files;
139  
140 4 $path =~ s/^\///;
141 4 $path =~ s/\/$//;
142 4 my $url = "${baseuri}/${path}";
143  
144 4 my $ret;
145 4 ▶ if ( $subdirs[0] ){
Branch: 1 branch
BranchTrueFalseTotal
if ($subdirs[0]) 2 2 4
146 2 $ret = '<div id="dirs"><ul>';
147 2 ▶ $ret .= "<li><a href=\"$url\">..</a></li>" if ( $url =! "/$path");
Branch: 1 branch
BranchTrueFalseTotal
if $url = !("/$path") 0 2 2
148 2 foreach my $d (@subdirs){
149 2 ▶ next if ( ! -r $d);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $d 0 2 2
150 2 $d =~ s/$dir\///;
151  
152 2 $ret .= "<li><a href=\"$url/$path/$d\">[$d]</a></li>";
153 }
154 2 $ret .= "</ul></div>\n";
155 }
156  
157 4 ▶ if ( $files[0] ){
Branch: 1 branch
BranchTrueFalseTotal
if ($files[0]) 2 2 4
158 2 $ret .= '<div id="files"><ul>';
159 2 foreach my $f (@files){
160 4 ▶ next if ( ! -r $f);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $f 0 4 4
161 4 $f =~ s/$dir\///;
162 4 $f =~ s/^\///;
163  
164 # Use Image controller if it is a image
165 4 $f =~ /.*\.(.*)$/;
166  
167 # replace dot with '_' if it's not a image
168 4 ▶ $f =~ s/\./_/
Branch: 1 branch
BranchTrueFalseTotal
unless "MojoMojo::Formatter::File::Image"->can_format($1) 4 0 4
169 if ( ! MojoMojo::Formatter::File::Image->can_format($1) );
170  
171 4 $ret .= "<li><a href=\"$url/$f\">$f</a></li>";
172 }
173 2 $ret .= "</ul></div>\n";
174 }
175 4 return $ret;
176 }
177  
178  
179 =item checkdir
180  
181 Directory must be include in whitelisting
182  
183 =cut
184 6 ▶sub checkdir{
Subroutine checkdir: called CC=6 SCAR=18.0
Pod documented
185 6 my ($self,$dir,$c) = @_;
186  
187 6 ▶ return "Append a directory after 'dir'"
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 6 6
188 if ( ! $dir );
189  
190 6 ▶ return "You can't use '..' in the name of directory"
Branch: 1 branch
BranchTrueFalseTotal
if $dir =~ /\.\./ 1 5 6
191 if ( $dir =~ /\.\./ );
192  
193 5 my $confwl = $c->config->{'Formatter::Dir'}{whitelisting};
194 5 ▶ my @whitelist = ref $confwl eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $confwl eq 'ARRAY' ? : 5 0 5
195 @$confwl : ( $confwl );
196 # Add '/' if not exist at the end of whitelist directories
197 5 my @wl = map { $_ . '/' } # Add '/'
198 5 ( map{ /(\S*[^\/])/ } # Delete '/' if exist
199 @whitelist );
200  
201 # Add '/' if not exist at the end of dierctory
202 5 $dir =~ s|^(\S*[^/])$|$1\/|;
203  
204 # if $dir is not include in whitelisting
205 5 ▶ if ( ! map ( $dir =~ m|^$_| , @wl) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (map(($dir =~ /^$_/), @wl)) 2 3 5
206 2 return "Directory '$dir' must be include in whitelisting ! see Formatter::Dir:whitelisting in mojomojo.conf"
207 }
208  
209  
210 3 ▶ return "'$dir' is not a directory !\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 2 3
211 if ( ! -d $dir );
212  
213 2 return 0;
214 }
215  
216 =back
217  
218 =head1 SEE ALSO
219  
220 L<MojoMojo>
221  
222 =head1 AUTHORS
223  
224 Daniel Brosseau <dab@catapulse.org>
225  
226 =head1 LICENSE
227  
228 This module is licensed under the same terms as Perl itself.
229  
230 =cut
231  
232 26 1;
« lib/Text/Wikispaces2Markdown.pm ↑ Summary lib/MojoMojo/Schema/Result/Person.pm »