File Coverage

lib/App/Followme/CreateIndex.pm
Criterion Covered Total %
statement 70 70 100.0
branch 7 14 50.0
condition 1 3 33.3
subroutine 15 15 100.0
pod 2 6 33.3
total 95 108 87.9


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