File Coverage

lib/App/Followme/CreateIndex.pm
Criterion Covered Total %
statement 67 70 95.7
branch 8 14 57.1
condition 1 3 33.3
subroutine 15 15 100.0
pod 2 6 33.3
total 93 108 86.1


line stmt bran cond sub pod time code
1             package App::Followme::CreateIndex;
2 3     3   703 use 5.008005;
  3         29  
3 3     3   18 use strict;
  3         5  
  3         76  
4 3     3   14 use warnings;
  3         6  
  3         103  
5              
6 3     3   19 use lib '../..';
  3         6  
  3         18  
7              
8 3     3   424 use base qw(App::Followme::Module);
  3         6  
  3         1044  
9              
10 3     3   20 use IO::Dir;
  3         18  
  3         147  
11 3     3   18 use File::Spec::Functions qw(abs2rel rel2abs splitdir catfile no_upwards);
  3         6  
  3         200  
12              
13 3     3   20 use App::Followme::FIO;
  3         6  
  3         295  
14 3     3   19 use App::Followme::Web;
  3         6  
  3         2147  
15              
16             our $VERSION = "2.03";
17              
18             #----------------------------------------------------------------------
19             # Read the default parameter values
20              
21             sub parameters {
22 52     52 1 106 my ($pkg) = @_;
23              
24             return (
25 52         164 template_file => 'create_index.htm',
26             data_pkg => 'App::Followme::WebData',
27             );
28             }
29              
30             #----------------------------------------------------------------------
31             # Create an index to all files in a folder with a specified extension
32              
33             sub run {
34 12     12 0 709 my ($self, $folder) = @_;
35              
36 12         33 eval {$self->update_folder($folder)};
  12         44  
37 12         146 $self->check_error($@, $folder);
38              
39 12         149 return;
40             }
41              
42             #----------------------------------------------------------------------
43             # Return a flag indicating that recursion on folders is not needed
44              
45             sub no_recurse {
46 12     12 0 37 my ($self) = @_;
47              
48 12         56 my $template_file = $self->get_template_name($self->{template_file});
49 12         62 my $template = fio_read_page($template_file);
50 12 50       51 die "Couldn't read template: $template_file" unless $template;
51              
52 12         67 return web_has_variables($template, '@all_files', '@top_files');
53             }
54              
55             #----------------------------------------------------------------------
56             # Return true if each section in a file has been filled from the template
57              
58             sub sections_are_filled {
59 1     1 0 457 my ($self, $filename) = @_;
60              
61 1         4 my $page = fio_read_page($filename);
62 1 50       5 return unless $page;
63 1         5 my $page_sections = web_parse_sections($page);
64              
65 1         6 my $template_file = $self->get_template_name($self->{template_file});
66 1         6 my $template = fio_read_page($template_file);
67 1 50       4 die "Couldn't read template: $template_file" unless $template;
68 1         4 my $template_sections = web_parse_sections($template);
69              
70 1         5 foreach my $name (keys %$template_sections) {
71 2 50       8 next unless $template_sections->{$name} =~ /\S/;
72 2 50       8 return unless $page_sections->{$name} =~ /\S/;
73             }
74              
75 1         6 return 1;
76             }
77              
78             #----------------------------------------------------------------------
79             # Set exclude_index to true in the data package
80              
81             sub setup {
82 13     13 1 34 my ($self) = @_;
83              
84 13         32 $self->{data}{exclude_index} = 1;
85 13         31 return;
86             }
87              
88             #----------------------------------------------------------------------
89             # Find files in directory to convert and do that
90              
91             sub update_folder {
92 12     12 0 33 my ($self, $folder) = @_;
93              
94 12         59 my $index_file = $self->to_file($folder);
95 12         66 my $base_index_file = $self->to_file($self->{data}{base_directory});
96              
97 12         65 my $template_file = $self->get_template_name($self->{template_file});
98 12         91 my $newest_file = $self->{data}->build('newest_file', $base_index_file);
99              
100              
101 12 50 33     59 unless (fio_is_newer($index_file, $template_file, @$newest_file) &&
102             $self->sections_are_filled($index_file)) {
103              
104 12         97 my $page = $self->render_file($self->{template_file}, $base_index_file);
105              
106 12         102 my $prototype_file = $self->find_prototype($folder, 0);
107 12         81 $page = $self->reformat_file($prototype_file, $index_file, $page);
108 12         54 fio_write_page($index_file, $page);
109             }
110              
111 12 100       86 unless ($self->no_recurse) {
112 8         44 my $folders = $self->{data}->build('folders', $index_file);
113 8         32 foreach my $subfolder (@$folders) {
114 0         0 eval {$self->update_folder($subfolder)};
  0         0  
115 0         0 $self->check_error($@, $subfolder);
116             }
117             }
118              
119 12         44 return;
120             }
121              
122             1;
123             __END__
124              
125             =encoding utf-8
126              
127             =head1 NAME
128              
129             App::Followme::CreateIndex - Create index file for a directory
130              
131             =head1 SYNOPSIS
132              
133             use App::Followme::CreateIndex;
134             my $indexer = App::Followme::CreateIndex->new($configuration);
135             $indexer->run($directory);
136              
137             =head1 DESCRIPTION
138              
139             This package builds an index for a directory containing links to all the files
140             contained in it with the specified extensions. The variables described below are
141             substituted into a template to produce the index. Loop comments that look like
142              
143             <!-- for @files -->
144             <!-- endfor -->
145              
146             indicate the section of the template that is repeated for each file contained
147             in the index.
148              
149             =head1 CONFIGURATION
150              
151             The following fields in the configuration file are used:
152              
153             =over 4
154              
155             =item template_file
156              
157             The name of the template file. The template file is either in the same
158             directory as the configuration file used to invoke this method, or if not
159             there, in the templates subdirectory. The default value is 'create_index.htm'.
160              
161             =item data_pkg
162              
163             The name of the class used to find and parse files included in the index. The
164             default value is 'App::Followme::WebData', which handles html files.
165              
166             =back
167              
168             =head1 LICENSE
169              
170             Copyright (C) Bernie Simon.
171              
172             This library is free software; you can redistribute it and/or modify
173             it under the same terms as Perl itself.
174              
175             =head1 AUTHOR
176              
177             Bernie Simon E<lt>bernie.simon@gmail.comE<gt>
178              
179             =cut