lib/MojoMojo/Formatter/File.pm

filter: Statementstmt 98.8% 87 / 88 Branchbran 80.0% 24 / 30 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 93.9% 139 / 148 CC 20 SCAR 16.0
CC
20
Cov
94%
format_content
9.0
checkfile
7.1
plugin
4.0
CRAP 5.0
« lib/MojoMojo/Schema/ResultSet/Person.pm ↑ Summary lib/MojoMojo/Formatter/Redirect.pm »
1 package MojoMojo::Formatter::File;
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 File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use MojoMojo::Formatter::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Module::Pluggable (
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 search_path => ['MojoMojo::Formatter::File'],
12 require => 1,
13 26 );
14 26 my $debug=0;
15  
16 =head1 NAME
17  
18 MojoMojo::Formatter::File - format file as XHTML
19  
20 =head1 DESCRIPTION
21  
22 This formatter will format the file argument as XHTML.
23  
24 Usage: {{file TYPE filename}}
25  
26  
27 {{file Text uploads/Files/test.txt}}
28  
29  
30 TYPE is a plugin present in Formatter/File/ directory.
31  
32 Currently there are only three: Pod, DocBook and Text
33  
34 The plugin TYPE format only the file which the extension match with 'can_format' method. Respectively pod, xml and txt for existing plugins.
35  
36 For security reasons the path of file must be include in 'whitelisting' directory. You can use path_to(DIR) to describe directory in mojomojo.conf:
37  
38  
39 Just an example to view the test pod file t/var/files/test.pod :
40  
41 Add this to mojomojo.conf :
42  
43 <Formatter::Dir>
44 prefix_url /myfiles
45 whitelisting __path_to(t/var/files)__
46 </Formatter::Dir>
47  
48 To see the pod content formatted in xhtml, write in the text area:
49  
50 {{file Pod path_to(t/var/files)test.pod}}
51  
52  
53 To show recursively all files of directory see script/util/dir2mojomojo.pl script. To test it:
54  
55 # start mojomojo
56  
57 ./script/mojomojo_server.pl
58  
59 # run dir2mojomojo script
60  
61 ./script/util/dir2mojomojo.pl --dir=~/dev/mojomojo/t/var/files/ --url=/myfiles
62  
63  
64 Connect to http://server:3000/myfiles/
65  
66  
67 =head1 METHODS
68  
69 =over 4
70  
71 =item format_content_order
72  
73 Format order can be 1-99. The File formatter runs on 92.
74  
75 =cut
76  
77 744 ▶sub format_content_order { 92 }
Subroutine format_content_order: called CC=1 SCAR=0.0
Pod documented
78  
79 =item format_content
80  
81 Calls the formatter. Takes a ref to the content as well as the
82 context object.
83  
84 =cut
85  
86  
87 129 ▶sub format_content {
Subroutine format_content: called CC=9 SCAR=22.0
Pod documented
88 129 my ( $self, $content, $c ) = @_;
89  
90  
91 # TODO : Add cache if file is not modified
92  
93  
94 129 my @lines = split /\n/, $$content;
95  
96 129 $$content = "";
97 129 my $is_image = 0;
98 129 foreach my $line (@lines) {
99  
100 735 ▶ if ( $line =~ m|\{\{\s*file\s*(\w+)\s*(.*)}}.*| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\{\{\s*file\s*(\w+)\s*(.*)}}.*/) { } 4 731 735
101 4 my $plugin=$1; # DocBook, Pod, ...
102 4 my $file=$2; # File, Attachment
103  
104 4 ▶ $is_image = 1 if ( $plugin eq 'Image' );
Branch: 1 branch
BranchTrueFalseTotal
if $plugin eq "Image" 0 4 4
105  
106 # use path_to(dir)/filename ?
107 4 my $path_to = $c->path_to();
108 4 $file =~ s/path_to\([\s|\/]*(\S*)[\s|\/]*\)\s*(\S*)\s*/${path_to}\/$1\/$2/;
109  
110 4 my $error;
111 4 ▶ if ( $error = checkplugin($plugin, $file)){
Branch: 1 branch
BranchTrueFalseTotal
if ($error = checkplugin($plugin, $file)) 1 3 4
112 1 $$content .= $error;
113 }
114 4 ▶ if ( ! $error && ( $error = $self->checkfile($file, $c))){
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $error = $self->checkfile($file, $c)) 2 2 4
Condition100%3/3
not $error and $error = $self->checkfile($file, $c)
!ll&&!rl&&r
112
MC/DC100%2/2
not $error and $error = $self->checkfile($file, $c)
not $error $error = $self->checkfile($file, $c)
Truth table100%3/3
not(A) and B
A= not $errorB= $error = $self->checkfile($file, $c)
ABresult
0X0
100
111
115 2 $$content .= $error;
116 }
117  
118 4 ▶ if ( ! $error ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 1 3 4
119 # format with plugin
120 1 $$content .= $self->format($plugin,$file);
121 }
122 }
123 else{
124 # Image have not more content
125 731 ▶ if ( ! $is_image ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_image) 731 0 731
126 731 $$content .= $line . "\n";
127 }
128 }
129 }
130 129 return $content;
131 }
132  
133  
134 =item plugin
135  
136 Return the plugin to use with file attachment
137  
138 =cut
139  
140 2 ▶sub plugin {
Subroutine plugin: called CC=4 SCAR=13.9
Pod documented
141 2 my $self = shift;
142 2 my $filename = shift;
143  
144 2 my ($name,$extension) = $filename =~ /(.*)\.(.*)/;
145  
146 2 foreach my $plugin ( plugins() ) {
147 7 ▶ if ( $plugin->can('can_format') && $plugin->can_format($extension)){
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("can_format") and $plugin->can_format($extension)) 2 5 7
Condition66%2/3
$plugin->can("can_format") and $plugin->can_format($extension)
!ll&&!rl&&r
052
MC/DC50%1/2
$plugin->can("can_format") and $plugin->can_format($extension)
$plugin->can("can_format") $plugin->can_format($extension)
Truth table66%2/3
A and B
A= $plugin->can("can_format")B= $plugin->can_format($extension)
ABresult
0X0
100
111
148 2 my $pluginname = $plugin;
149 2 $pluginname =~ s/.*:://;
150  
151 2 return $pluginname;
152 }
153 }
154 }
155  
156  
157 =item format
158  
159 Return the content formatted
160  
161 =cut
162  
163 3 ▶sub format {
Subroutine format: called CC=2 SCAR=7.0
Pod documented
164 3 my $self = shift;
165 3 my $pluginname = shift;
166 3 my $file = shift;
167  
168 3 my $error;
169 3 ▶ if ( $error = checkplugin($pluginname)){
Branch: 1 branch
BranchTrueFalseTotal
if ($error = checkplugin($pluginname)) 0 3 3
170 0 return $error;
171 }
172  
173 3 my $text = read_file( $file );
174 3 utf8::decode($text);
175 3 $text = encode('utf-8', $text);
176 3 $text = Encode::decode('utf-8', $text);
177  
178 3 my $plugin = __PACKAGE__ . "::$pluginname";
179 3 return $plugin->to_xhtml($text,$file) . "\n";
180 }
181  
182  
183 =item checkplugin
184  
185 Return 0 if plugin exist
186  
187 =cut
188 7 ▶sub checkplugin{
Subroutine checkplugin: called CC=2 SCAR=6.9
Pod documented
189 7 my $pluginname = shift;
190 7 my $file = shift;
191  
192 7 my $plugin = __PACKAGE__ . "::$pluginname";
193  
194 7 ▶ return 0 if $plugin->can('can_format');
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->can("can_format") 6 1 7
195  
196 1 return "Can't find plugin for $file !";
197 }
198  
199 =item checkfile
200  
201 Directory must be include in whitelisting
202  
203 =cut
204 6 ▶sub checkfile{
Subroutine checkfile: called CC=7 SCAR=19.6
Pod documented
205 6 my ($self, $file, $c) = @_;
206  
207 6 ▶ return "Append a file after 'file'"
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 6 6
208 if ( ! $file );
209  
210 6 ▶ return "You can't use '..' in the name of file"
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.\./ 1 5 6
211 if ( $file =~ /\.\./ );
212  
213 5 my $dir = dirname($file);
214  
215 5 my $confwl = $c->config->{'Formatter::Dir'}{whitelisting};
216 5 ▶ my @whitelist = ref $confwl eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $confwl eq 'ARRAY' ? : 5 0 5
217 @$confwl : ( $confwl );
218 # Add '/' if not exist at the end of whitelist directories
219 5 my @wl = map { $_ . '/' } # Add '/'
220 5 ( map{ /(\S*[^\/])/ } # Delete '/' if exist
221 @whitelist );
222  
223  
224 # Add '/' if not exist at the end of dierctory
225 5 $dir =~ s|^(\S*[^/])$|$1\/|;
226  
227 # if $dir is not include in whitelisting
228 5 ▶ if ( ! map ( $dir =~ m|^$_| , @wl) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (map(($dir =~ /^$_/), @wl)) 2 3 5
229 2 return "Directory '$dir' must be include in whitelisting ! see Formatter::Dir:whitelisting in mojomojo.conf"
230 }
231  
232  
233 3 ▶ return "'$dir' is not a directory !\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 3 3
234 if ( ! -d $dir );
235  
236 3 ▶ return "Can not read '$file' !\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 1 2 3
237 if ( ! -r $file );
238  
239 2 return 0;
240 }
241  
242 =back
243  
244 =head1 SEE ALSO
245  
246 L<MojoMojo>,L<Module::Pluggable::Ordered>
247  
248 =head1 AUTHORS
249  
250 Daniel Brosseau <dab@catapulse.org>
251  
252 =head1 LICENSE
253  
254 This module is licensed under the same terms as Perl itself.
255  
256 =cut
257  
258 26 1;
« lib/MojoMojo/Schema/ResultSet/Person.pm ↑ Summary lib/MojoMojo/Formatter/Redirect.pm »