lib/App/Followme/FolderData.pm

filter: Statementstmt 99.6% 284 / 285 Branchbran 87.1% 68 / 78 Conditioncond 71.4% 20 / 28 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 100% 51 / 51 Podpod 4.6% 2 / 43 totaltotal 85.6% 431 / 503 CC 63 SCAR 12.3
CC
63
Cov
95%
find_newest_file
8.1
match_file
7.0
format_size
6.0
CRAP 3.4
« lib/App/Followme/Module.pm ↑ Summary lib/App/Followme/ConvertPage.pm »
1 package App::Followme::FolderData;
2  
3 19 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use File::Spec::Functions qw(abs2rel catfile rel2abs splitdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use base qw(App::Followme::BaseData);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 19 our $VERSION = "2.03";
15  
16 #----------------------------------------------------------------------
17 # Read the default parameter values
18  
19 368 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
20 368 my ($self) = @_;
21  
22 return (
23 368 extension => '',
24 site_url => '',
25 remote_url => '',
26 author => '',
27 size_format => 'kb',
28 date_format => 'Mon d, yyyy h:mm',
29 exclude_index => 0,
30 exclude => '',
31 exclude_dirs => '.*,_*',
32 web_extension => 'html',
33 );
34 }
35  
36 #----------------------------------------------------------------------
37 # Return the author's name stored in the configuration file
38  
39 14 ▶sub calculate_author {
Subroutine calculate_author: called CC=1 SCAR=0.0
Pod undocumented
40 14 my ($self, $filename) = @_;
41 14 return $self->{author};
42 }
43  
44 #----------------------------------------------------------------------
45 # Calculate the creation date from the modification date
46  
47 5 ▶sub calculate_date {
Subroutine calculate_date: called CC=1 SCAR=0.0
Pod undocumented
48 5 my ($self, $filename) = @_;
49 5 return $self->get_mdate($filename);
50 }
51  
52 #----------------------------------------------------------------------
53 # Get the list of keywords from the file path
54  
55 10 ▶sub calculate_keywords {
Subroutine calculate_keywords: called CC=2 SCAR=6.9
Pod undocumented
56 10 my ($self, $filename) = @_;
57 10 $filename = $self->dir_to_filename($filename);
58  
59 10 $filename = abs2rel($filename);
60 10 my @path = splitdir($filename);
61 10 pop(@path);
62  
63 10 ▶ my $keywords = pop(@path) || '';
Condition100%2/2
pop @path || ''
l!l
82
MC/DC100%1/1
pop @path || ''
pop @path
Truth table100%2/2
A
A= pop @path
Aresult
11
00
64 10 return $keywords;
65 }
66  
67 #----------------------------------------------------------------------
68 # Calculate the title from the filename root
69  
70 35 ▶sub calculate_title {
Subroutine calculate_title: called CC=4 SCAR=13.9
Pod undocumented
71 35 my ($self, $filename) = @_;
72 35 $filename = $self->dir_to_filename($filename);
73  
74 35 my ($dir, $file) = fio_split_filename($filename);
75 35 my ($root, $ext) = split(/\./, $file);
76  
77 35 ▶ if ($root eq 'index') {
Branch: 1 branch
BranchTrueFalseTotal
if ($root eq "index") 12 23 35
78 12 my @dirs = splitdir($dir);
79 12 ▶ $root = pop(@dirs) || '';
Condition50%1/2
pop @dirs || ''
l!l
120
MC/DC0%0/1
pop @dirs || ''
pop @dirs
Truth table50%1/2
A
A= pop @dirs
Aresult
11
00
80 }
81  
82 35 ▶ $root =~ s/^\d+// unless $root =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $root =~ /^\d+$/ 35 0 35
83 35 my @words = map {ucfirst $_} split(/[\-\_]/, $root);
84  
85 35 return join(' ', @words);
86 }
87  
88 #----------------------------------------------------------------------
89 # Check that filename is defined, die if not
90  
91 597 ▶sub check_filename {
Subroutine check_filename: called CC=2 SCAR=7.1
Pod undocumented
92 597 my ($self, $name, $filename) = @_;
93  
94 597 ▶ die "Cannot use \$$name outside of loop" unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 597 597
95 597 return;
96 }
97  
98 #-----------------------------------------------------------------------
99 # Get the name of the web file a file will be converted to
100  
101 12 ▶sub convert_filename {
Subroutine convert_filename: called CC=1 SCAR=0.0
Pod undocumented
102 12 my ($self, $filename) = @_;
103  
104 12 $filename =~ s/\.[^\.]*$/.$self->{web_extension}/;
105 12 return $filename;
106 }
107  
108 #-----------------------------------------------------------------------
109 # Get the name of the source directory for ConvertPage
110  
111 9 ▶sub convert_source_directory {
Subroutine convert_source_directory: called CC=1 SCAR=0.0
Pod undocumented
112 9 my ($self, $directory) = @_;
113  
114 # Defaults to same as output directory
115 9 return $directory;
116 }
117  
118 #----------------------------------------------------------------------
119 # Get a filename if the directory happens to be a directory
120  
121 220 ▶sub dir_to_filename {
Subroutine dir_to_filename: called CC=2 SCAR=6.9
Pod undocumented
122 220 my ($self, $directory) = @_;
123  
124 220 my $filename;
125 220 ▶ if (-d $directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $directory) { } 13 207 220
126 13 my $index = 'index.' . $self->{web_extension};
127 13 $filename = catfile($directory, $index);
128 } else {
129 207 $filename = $directory;
130 }
131  
132 220 return $filename;
133 }
134  
135 #----------------------------------------------------------------------
136 # Fetch data from all its possible sources
137  
138 39 ▶sub fetch_data {
Subroutine fetch_data: called CC=2 SCAR=6.9
Pod undocumented
139 39 my ($self, $name, $filename, $loop) = @_;
140  
141 # Look under both sets of functions
142  
143 39 $self->check_filename($name, $filename);
144 39 my %data = $self->gather_data('get', $name, $filename, $loop);
145  
146 %data = (%data, $self->gather_data('calculate', $name, $filename, $loop))
147 39 ▶ unless exists $data{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data{$name} 12 27 39
148  
149 39 return %data;
150 }
151  
152 #----------------------------------------------------------------------
153 # Convert filename to url
154  
155 103 ▶sub filename_to_url {
Subroutine filename_to_url: called CC=3 SCAR=11.0
Pod undocumented
156 103 my ($self, $directory, $filename, $ext) = @_;
157  
158 103 $filename = $self->dir_to_filename($filename);
159  
160 103 $filename = rel2abs($filename);
161 103 $filename = abs2rel($filename, $directory);
162  
163 103 ▶ my @path = $filename eq '.' ? () : splitdir($filename);
Branch: 1 branch
BranchTrueFalseTotal
$filename eq '.' ? : 0 103 103
164  
165 103 my $url = join('/', @path);
166 103 ▶ $url =~ s/\.[^\.]*$/.$ext/ if defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext 83 20 103
167  
168 103 return $url;
169 }
170  
171 #----------------------------------------------------------------------
172 # Find the filename at an offset to the current filename
173  
174 10 ▶sub find_filename {
Subroutine find_filename: called CC=6 SCAR=17.9
Pod undocumented
175 10 my ($self, $offset, $filename, $loop) = @_;
176 10 ▶ die "Can't use \$url_* outside of for\n" unless $loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $loop 0 10 10
177  
178 10 my $match = -999;
179 10 foreach my $i (0 .. @$loop) {
180 30 ▶ if ($loop->[$i] eq $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop->[$i] eq $filename) 10 20 30
181 10 $match = $i;
182 10 last;
183 }
184 }
185  
186 10 my $index = $match + $offset;
187 10 ▶ if ($index < 0 || $index > @$loop-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 0 or $index > @$loop - 1) { } 2 8 10
Condition100%3/3
$index < 0 or $index > @$loop - 1
l!l&&r!l&&!r
118
MC/DC100%2/2
$index < 0 or $index > @$loop - 1
$index < 0 $index > @$loop - 1
Truth table100%3/3
A or B
A= $index < 0B= $index > @$loop - 1
ABresult
1X1
011
000
188 2 $filename = '';
189 } else {
190 8 $filename = $loop->[$index];
191 }
192  
193 10 return $filename;
194 }
195  
196 #----------------------------------------------------------------------
197 # Build a recursive list of directories with a breadth first search
198  
199 37 ▶sub find_matching_directories {
Subroutine find_matching_directories: called CC=4 SCAR=13.9
Pod undocumented
200 37 my ($self, $directory) = @_;
201  
202 37 my ($filenames, $subdirectories) = fio_visit($directory);
203  
204 37 my @directories;
205 37 foreach my $subdirectory (@$subdirectories) {
206 14 ▶ next unless $self->match_directory($subdirectory);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match_directory($subdirectory) 0 14 14
207 14 push(@directories, $subdirectory);
208 }
209  
210 37 foreach my $subdirectory (@directories) {
211 14 push(@directories, $self->find_matching_directories($subdirectory));
212 }
213  
214 37 return @directories;
215 }
216  
217 #----------------------------------------------------------------------
218 # Build a list of matching files in a folder
219  
220 84 ▶sub find_matching_files {
Subroutine find_matching_files: called CC=3 SCAR=11.0
Pod undocumented
221 84 my ($self, $folder) = @_;
222  
223 84 my ($filenames, $folders) = fio_visit($folder);
224  
225 84 my @files;
226 84 foreach my $filename (@$filenames) {
227 360 ▶ push(@files, $filename) if $self->match_file($filename);
Branch: 1 branch
BranchTrueFalseTotal
if $self->match_file($filename) 174 186 360
228 }
229  
230 84 return @files;
231 }
232  
233 #----------------------------------------------------------------------
234 # Find the newest file in a set of files
235  
236 18 ▶sub find_newest_file {
Subroutine find_newest_file: called CC=8 SCAR=20.9
Pod undocumented
237 18 my ($self, @files) = @_;
238  
239 18 my ($newest_file, $newest_date);
240 18 ▶ while (@files && ! defined $newest_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files and not defined $newest_file) 15 18 33
Condition100%3/3
@files and not defined $newest_file
!ll&&!rl&&r
9915
MC/DC100%2/2
@files and not defined $newest_file
@files not defined $newest_file
Truth table100%3/3
A and not(B)
A= @filesB= not defined $newest_file
ABresult
0X0
100
111
241 15 $newest_file = shift(@files);
242 15 ▶ $newest_date =fio_get_date($newest_file) if defined $newest_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $newest_file 11 4 15
243 }
244  
245 18 foreach my $file (@files) {
246 22 ▶ next unless defined $file && -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and -e $file 0 22 22
Condition33%1/3
defined $file and -e $file
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $file and -e $file
defined $file -e $file
Truth table33%1/3
A and B
A= defined $fileB= -e $file
ABresult
0X0
100
111
247  
248 22 my $date = fio_get_date($file);
249 22 ▶ if ($date > $newest_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date > $newest_date) 15 7 22
250 15 $newest_date = $date;
251 15 $newest_file = $file;
252 }
253 }
254  
255 18 return $newest_file;
256 }
257  
258 #----------------------------------------------------------------------
259 # Get the more recently changed files
260  
261 17 ▶sub find_top_files {
Subroutine find_top_files: called CC=1 SCAR=0.0
Pod undocumented
262 17 my ($self, $folder, $augmented_files) = @_;
263  
264 17 my @files = $self->find_matching_files($folder);
265 17 my @sorted_files = $self->sort_with_field(\@files, 'mdate', 1);
266  
267 17 return $self->merge_augmented($augmented_files, \@sorted_files);
268 }
269  
270 #----------------------------------------------------------------------
271 # Format the file creation date
272  
273 47 ▶sub format_date {
Subroutine format_date: called CC=2 SCAR=6.9
Pod undocumented
274 47 my ($self, $sorted_order, $date) = @_;
275  
276 47 ▶ if ($sorted_order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted_order) { } 1 46 47
277 1 $date = fio_format_date($date);
278 } else {
279 46 $date = fio_format_date($date, $self->{date_format});
280 }
281  
282 47 return $date;
283 }
284  
285 #----------------------------------------------------------------------
286 # Format the file modification date
287  
288 94 ▶sub format_mdate {
Subroutine format_mdate: called CC=1 SCAR=0.0
Pod undocumented
289 94 my ($self, $sorted_order, $date) = @_;
290 94 return fio_format_date($date);
291 }
292  
293 #----------------------------------------------------------------------
294 # Make filenames sortable in a cross-os manner
295  
296 134 ▶sub format_name {
Subroutine format_name: called CC=2 SCAR=7.1
Pod undocumented
297 134 my ($self, $sorted_order, $filename) = @_;
298  
299 134 ▶ $filename = join(' ', fio_split_filename($filename)) if $sorted_order;
Branch: 1 branch
BranchTrueFalseTotal
if $sorted_order 134 0 134
300 134 return $filename;
301 }
302  
303 #----------------------------------------------------------------------
304 # Format the file size
305  
306 2 ▶sub format_size {
Subroutine format_size: called CC=6 SCAR=18.0
Pod undocumented
307 2 my ($self, $sorted_order, $size) = @_;
308  
309 2 ▶ if ($sorted_order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted_order) { } 1 1 2
310 1 $size = sprintf("%012d", $size);
311  
312 } else {
313 1 ▶ if ($self->{size_format}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"size_format"}) 1 0 1
314 1 my $byte_size = $size;
315 1 my $fmt = lc($self->{size_format});
316  
317 1 foreach my $format (qw(b kb mb gb)) {
318 2 ▶ if ($fmt eq $format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt eq $format) 1 1 2
319 1 undef $fmt;
320 1 last;
321 }
322 1 $byte_size /= 1024;
323 }
324  
325 $size = join('', int($byte_size), $self->{size_format})
326 1 ▶ unless $fmt;
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt 1 0 1
327 }
328 }
329  
330 2 return $size;
331 }
332  
333 #----------------------------------------------------------------------
334 # Get the absolute url from a filename and the site url
335  
336 1 ▶sub get_absolute_url {
Subroutine get_absolute_url: called CC=1 SCAR=0.0
Pod undocumented
337 1 my ($self, $filename) = @_;
338 1 $filename = $self->dir_to_filename($filename);
339  
340 1 my $site_url = $self->get_site_url($filename);
341 1 my $relative_url = $self->get_url($filename);
342 1 my $absolute_url = "$site_url/$relative_url";
343  
344 1 return $absolute_url;
345 }
346  
347 #----------------------------------------------------------------------
348 # Get a list of matching files in a folder and its subfolders
349  
350 2 ▶sub get_all_files {
Subroutine get_all_files: called CC=2 SCAR=6.9
Pod undocumented
351 2 my ($self, $filename) = @_;
352  
353 2 my ($folder) = fio_split_filename($filename);
354 2 my @files = $self->find_matching_files($folder);
355  
356 2 my @folders = $self->find_matching_directories($folder);
357 2 foreach my $subfolder (@folders) {
358 1 push(@files, $self->find_matching_files($subfolder));
359 }
360  
361 2 return \@files;
362 }
363  
364 #----------------------------------------------------------------------
365 # Get a list of breadcrumb filenames
366  
367 1 ▶sub get_breadcrumbs {
Subroutine get_breadcrumbs: called CC=3 SCAR=11.0
Pod undocumented
368 1 my ($self, $filename) = @_;
369  
370 1 my @path = splitdir(abs2rel($filename, $self->{top_directory}));
371 1 pop(@path);
372  
373 1 my @breadcrumbs;
374 1 for (;;) {
375 my $dir = @path ? catfile($self->{top_directory}, @path)
376 2 ▶ : $self->{top_directory};
Branch: 1 branch
BranchTrueFalseTotal
@path ? : 1 1 2
377  
378 2 my $breadcrumb = $self->dir_to_filename($dir);
379 2 push (@breadcrumbs, $breadcrumb);
380  
381 2 ▶ last unless @path;
Branch: 1 branch
BranchTrueFalseTotal
unless @path 1 1 2
382 1 pop(@path);
383 }
384  
385 1 @breadcrumbs = reverse(@breadcrumbs);
386 1 return \@breadcrumbs;
387 }
388  
389 #----------------------------------------------------------------------
390 # Get the extension from a filename
391  
392 2 ▶sub get_extension {
Subroutine get_extension: called CC=1 SCAR=0.0
Pod undocumented
393 2 my ($self, $filename) = @_;
394  
395 2 my ($folder, $file) = fio_split_filename($filename);
396 2 my ($extension) = ($file =~ /\.([^\.]*)$/);
397  
398 2 return $extension;
399 }
400  
401  
402 #----------------------------------------------------------------------
403 # Get a list of matching files in a folder
404  
405 47 ▶sub get_files {
Subroutine get_files: called CC=1 SCAR=0.0
Pod undocumented
406 47 my ($self, $filename) = @_;
407  
408 47 my ($folder) = fio_split_filename($filename);
409 47 my @files = $self->find_matching_files($folder);
410 47 return \@files;
411 }
412  
413 #-----------------------------------------------------------------------
414 # Get a list of matching directories
415  
416 45 ▶sub get_folders {
Subroutine get_folders: called CC=3 SCAR=11.0
Pod undocumented
417 45 my ($self, $filename) = @_;
418  
419 45 my ($folder) = fio_split_filename($filename);
420 45 my ($filenames, $subfolders) = fio_visit($folder);
421  
422 45 my @folders;
423 45 foreach my $subfolder (@$subfolders) {
424 25 ▶ next unless $self->match_directory($subfolder);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match_directory($subfolder) 4 21 25
425 21 push(@folders, $subfolder);
426 }
427  
428 45 return \@folders;
429 }
430  
431 #-----------------------------------------------------------------------
432 # Get the url of the index page in the same folder as a file
433  
434 7 ▶sub get_index_url {
Subroutine get_index_url: called CC=1 SCAR=0.0
Pod undocumented
435 7 my ($self, $filename) = @_;
436  
437 7 my ($dir, $file) = fio_split_filename($filename);
438 7 my $index_page = "index.$self->{web_extension}";
439 7 $index_page = catfile($dir, $index_page);
440  
441 7 return $self->get_url($index_page);
442 }
443  
444 #----------------------------------------------------------------------
445 # Set a flag indicating if the the filename is the index file
446  
447 2 ▶sub get_is_index {
Subroutine get_is_index: called CC=3 SCAR=11.0
Pod undocumented
448 2 my ($self, $filename) = @_;
449  
450 2 my ($folder, $file) = fio_split_filename($filename);
451 2 my ($root, $ext) = split(/\./, $file);
452  
453 2 ▶ my $is_index = $root eq 'index' && $ext eq $self->{web_extension};
Condition66%2/3
$root eq 'index' && $ext eq $self->{'web_extension'}
!ll&&!rl&&r
101
MC/DC50%1/2
$root eq 'index' && $ext eq $self->{'web_extension'}
$root eq 'index' $ext eq $self->{'web_extension'}
Truth table66%2/3
A && B
A= $root eq 'index'B= $ext eq $self->{'web_extension'}
ABresult
0X0
100
111
454 2 ▶ return $is_index ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_index ? : 1 1 2
455 }
456  
457 #----------------------------------------------------------------------
458 # Get the modification time in epoch seconds
459  
460 99 ▶sub get_mdate {
Subroutine get_mdate: called CC=1 SCAR=0.0
Pod undocumented
461 99 my ($self, $filename) = @_;
462  
463 99 return fio_get_date($filename);
464 }
465  
466 #----------------------------------------------------------------------
467 # Get the most recently modified file in a folder and its subfolders
468  
469 13 ▶sub get_newest_file {
Subroutine get_newest_file: called CC=3 SCAR=11.0
Pod undocumented
470 13 my ($self, $filename) = @_;
471  
472 13 my ($folder) = fio_split_filename($filename);
473 13 my @files = $self->find_matching_files($folder);
474  
475 13 my $newest_file = $self->find_newest_file(@files);
476 13 my @folders = $self->find_matching_directories($folder);
477  
478 13 foreach my $subfolder (@folders) {
479 5 @files = $self->find_matching_files($subfolder);
480 5 $newest_file = $self->find_newest_file($newest_file, @files);
481 }
482  
483 13 ▶ return defined $newest_file ? [$newest_file] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $newest_file ? : 10 3 13
484 }
485  
486 #----------------------------------------------------------------------
487 # Get a list of files with the same rootname as another filename
488  
489 1 ▶sub get_related_files {
Subroutine get_related_files: called CC=3 SCAR=11.0
Pod undocumented
490 1 my ($self, $filename) = @_;
491 1 my ($folder, $file) = fio_split_filename($filename);
492  
493 1 my $root;
494 1 ($root = $file) =~ s/\.[^\.]*$//;
495  
496 1 my @related_files;
497 1 my ($filenames, $folders) = fio_visit($folder);
498 1 foreach my $filename (@$filenames) {
499 10 my ($folder, $file) = fio_split_filename($filename);
500  
501 10 my $rootname;
502 10 ($rootname = $file) =~ s/\.[^\.]*$//;
503 10 ▶ push(@related_files, $filename) if $root eq $rootname;
Branch: 1 branch
BranchTrueFalseTotal
if $root eq $rootname 2 8 10
504 }
505  
506 1 return \@related_files;
507 }
508  
509 #----------------------------------------------------------------------
510 # Get the remote url from a filename and the remote url
511  
512 29 ▶sub get_remote_url {
Subroutine get_remote_url: called CC=2 SCAR=7.1
Pod undocumented
513 29 my ($self, $filename) = @_;
514  
515 29 my $remote_url;
516 29 ▶ if ($self->{remote_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'remote_url'}) { } 29 0 29
517 29 my $relative_url = $self->get_url($filename);
518 29 $remote_url = "$self->{remote_url}/$relative_url";
519  
520 } else {
521 0 $remote_url = $self->get_absolute_url($filename);
522 }
523  
524 29 return $remote_url;
525 }
526  
527 #----------------------------------------------------------------------
528 # get the site url
529  
530 35 ▶sub get_site_url {
Subroutine get_site_url: called CC=2 SCAR=6.9
Pod undocumented
531 35 my ($self, $filename) = @_;
532  
533 35 my $site_url ;
534 35 ▶ if ($self->{site_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'site_url'}) { } 18 17 35
535 18 $site_url = $self->{site_url};
536  
537 } else {
538 17 $site_url = 'file://' . $self->{top_directory};
539 }
540  
541 35 $site_url =~ s/\/$//;
542 35 return $site_url;
543 }
544  
545 #----------------------------------------------------------------------
546 # Get the file size
547  
548 1 ▶sub get_size {
Subroutine get_size: called CC=1 SCAR=0.0
Pod undocumented
549 1 my ($self, $filename) = @_;
550  
551 1 return fio_get_size($filename);
552 }
553  
554 #----------------------------------------------------------------------
555 # Get a list of matching files in a directory and its subdirectories
556  
557 9 ▶sub get_top_files {
Subroutine get_top_files: called CC=2 SCAR=6.9
Pod undocumented
558 9 my ($self, $filename) = @_;
559  
560 9 my $augmented_files = [];
561 9 my ($folder) = fio_split_filename($filename);
562 9 $augmented_files = $self->find_top_files($folder, $augmented_files);
563  
564 9 my @directories = $self->find_matching_directories($folder);
565  
566 9 foreach my $subfolder (@directories) {
567 8 $augmented_files = $self->find_top_files($subfolder,
568 $augmented_files);
569 }
570  
571 9 my @top_files = $self->strip_augmented(@$augmented_files);
572 9 return \@top_files;
573 }
574  
575 #----------------------------------------------------------------------
576 # Get a url from a filename
577  
578 66 ▶sub get_url {
Subroutine get_url: called CC=1 SCAR=0.0
Pod undocumented
579 66 my ($self, $filename) = @_;
580 66 $filename = $self->dir_to_filename($filename);
581  
582 return $self->filename_to_url($self->{top_directory},
583 $filename,
584 66 $self->{web_extension});
585 }
586  
587 #----------------------------------------------------------------------
588 # Get a url with no extension from a filename
589  
590 1 ▶sub get_url_base {
Subroutine get_url_base: called CC=1 SCAR=0.0
Pod undocumented
591 1 my ($self, $filename) = @_;
592 1 $filename = $self->dir_to_filename($filename);
593  
594 my $url_base = $self->filename_to_url($self->{top_directory},
595 1 $filename, '');
596 1 chop($url_base); # remove trailing dot
597 1 return $url_base;
598 }
599  
600 #----------------------------------------------------------------------
601 # Get a url from the next filename in the loop
602  
603 5 ▶sub get_url_next {
Subroutine get_url_next: called CC=2 SCAR=6.9
Pod undocumented
604 5 my ($self, $filename, $loop) = @_;
605  
606 5 $filename = $self->find_filename(1, $filename, $loop);
607 5 ▶ return $filename ? $self->get_url($filename) : '';
Branch: 1 branch
BranchTrueFalseTotal
$filename ? : 4 1 5
608 }
609  
610 #----------------------------------------------------------------------
611 # Get a url from the previous filename in the loop
612  
613 5 ▶sub get_url_previous {
Subroutine get_url_previous: called CC=2 SCAR=6.9
Pod undocumented
614 5 my ($self, $filename, $loop) = @_;
615  
616 5 $filename = $self->find_filename(-1, $filename, $loop);
617 5 ▶ return $filename ? $self->get_url($filename) : '';
Branch: 1 branch
BranchTrueFalseTotal
$filename ? : 4 1 5
618 }
619  
620 #----------------------------------------------------------------------
621 # Return true if this is an included directory
622  
623 39 ▶sub match_directory {
Subroutine match_directory: called CC=3 SCAR=11.0
Pod undocumented
624 39 my ($self, $directory) = @_;
625  
626 39 ▶ $self->{exclude_dir_patterns} ||= fio_glob_patterns($self->{exclude_dirs});
Condition66%2/3
$self->{"exclude_dir_patterns"} ||= fio_glob_patterns($self->{"exclude_dirs"})
l!l&&r!l&&!r
27120
MC/DC0%0/2
$self->{"exclude_dir_patterns"} ||= fio_glob_patterns($self->{"exclude_dirs"})
$self->{"exclude_dir_patterns"} fio_glob_patterns($self->{"exclude_dirs"})
Truth table66%2/3
A ||= B
A= $self->{"exclude_dir_patterns"}B= fio_glob_patterns($self->{"exclude_dirs"})
ABresult
1X1
011
000
627  
628 39 ▶ return if $self->match_patterns($directory, $self->{exclude_dir_patterns});
Branch: 1 branch
BranchTrueFalseTotal
if $self->match_patterns($directory, $self->{"exclude_dir_patterns"}) 4 35 39
629 35 return 1;
630 }
631  
632 #----------------------------------------------------------------------
633 # Return true if this is an included file
634  
635 407 ▶sub match_file {
Subroutine match_file: called CC=7 SCAR=19.5
Pod undocumented
636 407 my ($self, $filename) = @_;
637  
638 407 my ($dir, $file) = fio_split_filename($filename);
639  
640 407 ▶ if ($self->{exclude_index}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclude_index"}) 122 285 407
641 122 my $index_file = join('.', 'index', $self->{web_extension});
642 122 ▶ return if $file eq $index_file;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $index_file 37 85 122
643 }
644  
645 370 my @patterns = map {"*.$_"} split(/\s*,\s*/, $self->{extension});
646 370 my $patterns = join(',', @patterns);
647  
648 370 ▶ $self->{include_file_patterns} ||= fio_glob_patterns($patterns);
Condition66%2/3
$self->{"include_file_patterns"} ||= fio_glob_patterns($patterns)
l!l&&r!l&&!r
337330
MC/DC0%0/2
$self->{"include_file_patterns"} ||= fio_glob_patterns($patterns)
$self->{"include_file_patterns"} fio_glob_patterns($patterns)
Truth table66%2/3
A ||= B
A= $self->{"include_file_patterns"}B= fio_glob_patterns($patterns)
ABresult
1X1
011
000
649 370 ▶ $self->{exclude_file_patterns} ||= fio_glob_patterns($self->{exclude});
Condition66%2/3
$self->{"exclude_file_patterns"} ||= fio_glob_patterns($self->{"exclude"})
l!l&&r!l&&!r
337330
MC/DC0%0/2
$self->{"exclude_file_patterns"} ||= fio_glob_patterns($self->{"exclude"})
$self->{"exclude_file_patterns"} fio_glob_patterns($self->{"exclude"})
Truth table66%2/3
A ||= B
A= $self->{"exclude_file_patterns"}B= fio_glob_patterns($self->{"exclude"})
ABresult
1X1
011
000
650  
651 370 ▶ return if $self->match_patterns($file, $self->{exclude_file_patterns});
Branch: 1 branch
BranchTrueFalseTotal
if $self->match_patterns($file, $self->{"exclude_file_patterns"}) 9 361 370
652 361 ▶ return unless $self->match_patterns($file, $self->{include_file_patterns});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match_patterns($file, $self->{"include_file_patterns"}) 140 221 361
653 221 return 1;
654 }
655  
656 #----------------------------------------------------------------------
657 # Return true if filename matches pattern
658  
659 779 ▶sub match_patterns {
Subroutine match_patterns: called CC=3 SCAR=11.0
Pod undocumented
660 779 my ($self, $file, $patterns) = @_;
661  
662 779 my @path = splitdir($file);
663 779 $file = pop(@path);
664  
665 779 foreach my $pattern (@$patterns) {
666 479 ▶ return 1 if $file =~ /$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /$pattern/ 234 245 479
667 }
668  
669 545 return;
670 }
671  
672 #----------------------------------------------------------------------
673 # Initialize the configuration parameters
674  
675 92 ▶sub setup {
Subroutine setup: called CC=4 SCAR=13.9
Pod documented
676 92 my ($self) = @_;
677  
678 # Set filename extension if unset
679 92 ▶ $self->{extension} ||= $self->{web_extension};
Condition66%2/3
$self->{"extension"} ||= $self->{"web_extension"}
l!l&&r!l&&!r
3890
MC/DC0%0/2
$self->{"extension"} ||= $self->{"web_extension"}
$self->{"extension"} $self->{"web_extension"}
Truth table66%2/3
A ||= B
A= $self->{"extension"}B= $self->{"web_extension"}
ABresult
1X1
011
000
680  
681 # Remove any trailing slash from urls
682 92 foreach my $url (qw(remote_url site_url)) {
683 184 ▶ next unless $self->{$url};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$url} 161 23 184
684 23 $self->{$url} =~ s/\/$//;
685 }
686  
687 92 return;
688 }
689  
690 19 1;
691  
692 =pod
693  
694 =encoding utf-8
695  
696 =head1 NAME
697  
698 App::Followme::FolderData - Build metadata from folder information
699  
700 =head1 SYNOPSIS
701  
702 use Cwd;
703 use App::Followme::FolderData;
704 my $directory = getcwd();
705 my $data = App::Followme::FolderData->new();
706 my $filenames = $data->get_files($directory);
707 foreach my $filename (@$filenames) {
708 print "$filename\n";
709 }
710  
711 =head1 DESCRIPTION
712  
713 This module contains the methods that build metadata values that can be computed
714 from properties of the file system, that is, without opening a file or
715 without regard to the type of file. It serves as the base of all the metadata
716 classes that build metadata for specific file types.
717  
718 =head1 METHODS
719  
720 All data are accessed through the build method.
721  
722 =over 4
723  
724 =item my %data = $obj->build($name, $filename);
725  
726 Build a variable's value. The first argument is the name of the variable. The
727 second argument is the name of the file the metadata is being computed for. If
728 it is undefined, the filename in the object
729 is used.
730  
731 =back
732  
733 =head1 VARIABLES
734  
735 The folder metadata class can evaluate the following variables. When passing
736 a name to the build method, the sigil should not be used.
737  
738 =over 4
739  
740 =item @all_files
741  
742 A list of matching files in a directory and its subdirectories.
743  
744 =item @top_files
745  
746 A list of the most recently created files in a directory and its
747 subdirectory. The length of the list is determined by the
748 configuration parameter list_length.
749  
750 =item @breadcrumbs
751  
752 A list of breadcrumb filenames, which are the names of the index files
753 above the filename passed as the argument.
754  
755 =item @related_files
756  
757 A list of files with the same file root name as a specified file. This
758 list is not filtered by the configuration variables extension and exclude.
759  
760 =item @files
761  
762 A list of matching files in a directory.
763  
764 =item @folders
765  
766 A list of folders under the default folder that contain an index file.
767  
768 =item $author
769  
770 The name of the author of a file
771  
772 =item $absolute_url
773  
774 The absolute url of a web page from a filename.
775  
776 =item $date
777  
778 A date string built from the creation date of the file. The date is
779 built using the template in date_format which contains the fields:
780 C<weekday, month,day, year, hour, minute,> and C<second.>
781  
782 =item $mdate
783  
784 A date string built from the modification date of the file. The date is
785 built using the template in date_format which contains the fields:
786 C<weekday, month,day, year, hour, minute,> and C<second.>
787  
788 =item $is_current
789  
790 One if the filename matches the default filename, zero if it does not.
791  
792 =item $is_index
793  
794 One of the filename is an index file and zero if it is not.
795  
796 =item $keywords
797  
798 A list of keywords describing the file from the filename path.
799  
800 =item $remote_url
801  
802 The remote url of a web page from a filename.
803  
804 =item $site_url
805  
806 The url of the website. Does not have a trailing slash
807  
808 =item $index_url
809  
810 The url of the index page in the same folder as a file.
811  
812 =item $title
813  
814 The title of a file is derived from the file name by removing the filename
815 extension, removing any leading digits,replacing dashes with spaces, and
816 capitalizing the first character of each word.
817  
818 =item $url
819  
820 Build the relative url of a web page from a filename.
821  
822 =item $url_base
823  
824 Build the relative url of a filename minus any extension and trailing dot
825  
826 =item $extension
827  
828 The extension of a filename.
829  
830 =item $url_next
831  
832 Build the relative url of a web page from the next filename in the loop
833 sequence. Empty string if there is no next filename.
834  
835 =item $url_previous
836  
837 Build the relative url of a web page from the previous filename in the loop
838 sequence. Empty string if there is no previous filename.
839  
840 =back
841  
842 =head1 CONFIGURATION
843  
844 The following fields in the configuration file are used in this class and every
845 class based on it:
846  
847 =over 4
848  
849 =item filename
850  
851 The filename used to retrieve metatdata from if no filename is passed to the
852 build method.
853  
854 =item directory
855  
856 The directory used to retrieve metadata. This is used by list valued variables.
857  
858 =item extension
859  
860 A comma separated list of extensions of files to include in a list of files.
861 If it is left empty, it is set to the web extension.
862  
863 =item date_format
864  
865 The format used to produce date strings. The format is described in the
866 POD for Time::Format.
867  
868 =item remote_url
869  
870 The url of the remote website, e.g. http://www.cloudhost.com.
871  
872 =item site_url
873  
874 The url of the local website, e.g. http://www.example.com.
875  
876 =item sort_numeric
877  
878 If one, use numeric comparisons when sorting. If zero, use string comparisons
879 when sorting.
880  
881 =item exclude_index
882  
883 If the value of this variable is true (1) exclude the index page in the list
884 of files. If it is false (0) include the index page. The default value is 0.
885  
886 =item exclude
887  
888 The files to exclude when traversing the list of files. Leave as a zero length
889 string if there are no files to exclude.
890  
891 =item exclude_dirs
892  
893 The directories excluded while traversing the list of directories. The default
894 value of this parameter is '.*,_*',
895  
896 =item web_extension
897  
898 The extension used by web pages. This controls which files are returned. The
899 default value is 'html'.
900  
901 =back
902  
903 =head1 LICENSE
904  
905 Copyright (C) Bernie Simon.
906  
907 This library is free software; you can redistribute it and/or modify
908 it under the same terms as Perl itself.
909  
910 =head1 AUTHOR
911  
912 Bernie Simon E<lt>bernie.simon@gmail.comE<gt>
913  
914 =cut
« lib/App/Followme/Module.pm ↑ Summary lib/App/Followme/ConvertPage.pm »