File Coverage

lib/App/Followme/CreateIndex.pm
Criterion Covered Total %
statement 70 70 100.0
branch 8 14 57.1
condition 2 3 66.6
subroutine 15 15 100.0
pod 2 6 33.3
total 97 108 89.8


line stmt bran cond sub pod time code
1             package App::Followme::CreateIndex;
2 3     3   828 use 5.008005;
  3         27  
3 3     3   17 use strict;
  3         7  
  3         71  
4 3     3   14 use warnings;
  3         6  
  3         114  
5              
6 3     3   18 use lib '../..';
  3         6  
  3         20  
7              
8 3     3   430 use base qw(App::Followme::Module);
  3         7  
  3         1120  
9              
10 3     3   23 use IO::Dir;
  3         24  
  3         163  
11 3     3   20 use File::Spec::Functions qw(abs2rel rel2abs splitdir catfile no_upwards);
  3         6  
  3         196  
12              
13 3     3   17 use App::Followme::FIO;
  3         5  
  3         380  
14 3     3   22 use App::Followme::Web;
  3         6  
  3         2124  
15              
16             our $VERSION = "2.01";
17              
18             #----------------------------------------------------------------------
19             # Read the default parameter values
20              
21             sub parameters {
22 52     52 1 98 my ($pkg) = @_;
23              
24             return (
25 52         168 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 716 my ($self, $folder) = @_;
35              
36 12         26 eval {$self->update_folder($folder)};
  12         48  
37 12         76 $self->check_error($@, $folder);
38              
39 12         167 return;
40             }
41              
42             #----------------------------------------------------------------------
43             # Return a flag indicating that recursion on folders is not needed
44              
45             sub no_recurse {
46 13     13 0 40 my ($self) = @_;
47              
48 13         68 my $template_file = $self->get_template_name($self->{template_file});
49 13         64 my $template = fio_read_page($template_file);
50 13 50       55 die "Couldn't read template: $template_file" unless $template;
51              
52 13         73 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 2     2 0 566 my ($self, $filename) = @_;
60              
61 2         8 my $page = fio_read_page($filename);
62 2 50       11 return unless $page;
63 2         18 my $page_sections = web_parse_sections($page);
64              
65 2         13 my $template_file = $self->get_template_name($self->{template_file});
66 2         11 my $template = fio_read_page($template_file);
67 2 50       33 die "Couldn't read template: $template_file" unless $template;
68 2         9 my $template_sections = web_parse_sections($template);
69              
70 2         10 foreach my $name (keys %$template_sections) {
71 4 50       21 next unless $template_sections->{$name} =~ /\S/;
72 4 50       16 return unless $page_sections->{$name} =~ /\S/;
73             }
74              
75 2         15 return 1;
76             }
77              
78             #----------------------------------------------------------------------
79             # Set exclude_index to true in the data package
80              
81             sub setup {
82 13     13 1 30 my ($self) = @_;
83              
84 13         29 $self->{data}{exclude_index} = 1;
85 13         27 return;
86             }
87              
88             #----------------------------------------------------------------------
89             # Find files in directory to convert and do that
90              
91             sub update_folder {
92 13     13 0 32 my ($self, $folder) = @_;
93              
94 13         62 my $index_file = $self->to_file($folder);
95 13         72 my $base_index_file = $self->to_file($self->{data}{base_directory});
96              
97 13         67 my $template_file = $self->get_template_name($self->{template_file});
98 13         90 my $newest_file = $self->{data}->build('newest_file', $base_index_file);
99              
100              
101 13 100 66     60 unless (fio_is_newer($index_file, $template_file, @$newest_file) &&
102             $self->sections_are_filled($index_file)) {
103              
104 12         88 my $page = $self->render_file($self->{template_file}, $base_index_file);
105              
106 12         108 my $prototype_file = $self->find_prototype($folder, 0);
107 12         84 $page = $self->reformat_file($prototype_file, $index_file, $page);
108 12         56 fio_write_page($index_file, $page);
109             }
110              
111 13 50       80 unless ($self->no_recurse) {
112 13         105 my $folders = $self->{data}->build('folders', $index_file);
113 13         126 foreach my $subfolder (@$folders) {
114 1         2 eval {$self->update_folder($subfolder)};
  1         6  
115 1         12 $self->check_error($@, $subfolder);
116             }
117             }
118              
119 13         53 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