lib/App/Followme/FormatPage.pm

filter: Statementstmt 100% 161 / 161 Branchbran 90.4% 38 / 42 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 26 / 26 Podpod 16.6% 2 / 12 totaltotal 91.5% 239 / 261 CC 29 SCAR 14.7
CC
29
Cov
96%
update_folder
9.0
parse_blocks
9.0
parse_blockname
4.2
CRAP 4.4
« lib/App/Followme/Template.pm ↑ Summary lib/App/Followme/Initialize.pm »
1 package App::Followme::FormatPage;
2 2 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw(App::Followme::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec::Functions qw(abs2rel rel2abs splitdir catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = "2.03";
15  
16 #----------------------------------------------------------------------
17 # Read the default parameter values
18  
19 24 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
20 24 my ($pkg) = @_;
21  
22 return (
23 24 data_pkg => 'App::Followme::WebData',
24 );
25 }
26  
27 #----------------------------------------------------------------------
28 # Modify pages to match the most recently modified page
29  
30 7 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
31 7 my ($self, $folder) = @_;
32  
33 7 $self->update_folder($folder);
34 7 return;
35 }
36  
37 #----------------------------------------------------------------------
38 # Compute checksum for constant sections of page
39  
40 26 ▶sub checksum_prototype {
Subroutine checksum_prototype: called CC=1 SCAR=0.0
Pod undocumented
41 26 my ($self, $prototype, $prototype_path) = @_;
42  
43 26 my $md5 = Digest::MD5->new;
44  
45 78 ▶ my $block_handler = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
46 78 my ($blockname, $locality, $blocktext) = @_;
47 78 ▶ $md5->add($blocktext) if exists $prototype_path->{$locality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $prototype_path->{$locality} 6 72 78
48 26 };
49  
50 104 ▶ my $prototype_handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 104 my ($blocktext) = @_;
52 104 $md5->add($blocktext);
53 104 return;
54 26 };
55  
56 26 $self->parse_blocks($prototype, $block_handler, $prototype_handler);
57 26 return $md5->hexdigest;
58 }
59  
60 #----------------------------------------------------------------------
61 # Get the prototype path for the current directory
62  
63 15 ▶sub get_prototype_path {
Subroutine get_prototype_path: called CC=1 SCAR=0.0
Pod undocumented
64 15 my ($self, $filename) = @_;
65  
66 15 $filename = rel2abs($filename);
67 15 $filename = abs2rel($filename, $self->{top_directory});
68 15 my @path = splitdir($filename);
69 15 pop(@path);
70  
71 15 my %prototype_path = map {$_ => 1} @path;
72 15 return \%prototype_path;
73 }
74  
75 #----------------------------------------------------------------------
76 # Parse fields out of section tag
77  
78 239 ▶sub parse_blockname {
Subroutine parse_blockname: called CC=4 SCAR=14.3
Pod undocumented
79 239 my ($self, $str) = @_;
80  
81 239 my ($blockname, $in, $locality) = split(/\s+/, $str);
82  
83 239 ▶ if ($in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in) { } 22 217 239
84 22 ▶ die "Syntax error in block ($str)"
Branch: 1 branch
BranchTrueFalseTotal
unless $in eq "in" and defined $locality 0 22 22
Condition33%1/3
$in eq "in" and defined $locality
!ll&&!rl&&r
0022
MC/DC0%0/2
$in eq "in" and defined $locality
$in eq "in" defined $locality
Truth table33%1/3
A and B
A= $in eq "in"B= defined $locality
ABresult
0X0
100
111
85 unless $in eq 'in' && defined $locality;
86 } else {
87 217 $locality = '';
88 }
89  
90 239 return ($blockname, $locality);
91 }
92  
93 #----------------------------------------------------------------------
94 # This code considers the surrounding tags to be part of the block
95  
96 45 ▶sub parse_blocks {
Subroutine parse_blocks: called CC=9 SCAR=22.0
Pod undocumented
97 45 my ($self, $page, $block_handler, $prototype_handler) = @_;
98  
99 45 my $locality;
100 45 my $block = '';
101 45 my $blockname = '';
102 45 my @tokens = split(/(<!--\s*(?:section|endsection)\s+.*?-->)/, $page);
103  
104 45 foreach my $token (@tokens) {
105 521 ▶ if ($token =~ /^<!--\s*section\s+(.*?)-->/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ /^<!--\s*section\s+(.*?)-->/) { } 121 400 521
elsif ($token =~ /^<!--\s*endsection\s+(.*?)-->/) { } 119 281 400
106 121 ▶ die "Improperly nested block ($token)\n" if $blockname;
Branch: 1 branch
BranchTrueFalseTotal
if $blockname 1 120 121
107  
108 120 ($blockname, $locality) = $self->parse_blockname($1);
109 120 $block .= $token
110  
111 } elsif ($token =~ /^<!--\s*endsection\s+(.*?)-->/) {
112 119 my ($endname) = $self->parse_blockname($1);
113 119 ▶ die "Unmatched ($token)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $blockname eq "" or $blockname ne $endname 2 117 119
Condition100%3/3
$blockname eq "" or $blockname ne $endname
l!l&&r!l&&!r
11117
MC/DC100%2/2
$blockname eq "" or $blockname ne $endname
$blockname eq "" $blockname ne $endname
Truth table100%3/3
A or B
A= $blockname eq ""B= $blockname ne $endname
ABresult
1X1
011
000
114 if $blockname eq '' || $blockname ne $endname;
115  
116 117 $block .= $token;
117 117 $block_handler->($blockname, $locality, $block);
118  
119 116 $block = '';
120 116 $blockname = '';
121  
122 } else {
123 281 ▶ if ($blockname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blockname) { } 120 161 281
124 120 $block .= $token;
125 } else {
126 161 $prototype_handler->($token);
127 }
128 }
129 }
130  
131 41 ▶ die "Unmatched block (<!-- section $blockname -->)\n" if $blockname;
Branch: 1 branch
BranchTrueFalseTotal
if $blockname 1 40 41
132 40 return;
133 }
134  
135 #----------------------------------------------------------------------
136 # Extract named blocks from a page
137  
138 8 ▶sub parse_page {
Subroutine parse_page: called CC=1 SCAR=0.0
Pod undocumented
139 8 my ($self, $page) = @_;
140  
141 8 my $blocks = {};
142 20 ▶ my $block_handler = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
143 20 my ($blockname, $locality, $blocktext) = @_;
144 20 ▶ if (exists $blocks->{$blockname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $blocks->{$blockname}) 1 19 20
145 1 die "Duplicate block name ($blockname)\n";
146 }
147 19 $blocks->{$blockname} = $blocktext;
148 19 return;
149 8 };
150  
151 27 ▶ my $prototype_handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 27 return;
153 8 };
154  
155 8 $self->parse_blocks($page, $block_handler, $prototype_handler);
156 7 return $blocks;
157 }
158  
159 #----------------------------------------------------------------------
160 # Initialize the extension
161  
162 6 ▶sub setup {
Subroutine setup: called CC=1 SCAR=0.0
Pod documented
163 6 my ($self) = @_;
164  
165 6 $self->{extension} = $self->{web_extension};
166 6 return;
167 }
168  
169 #----------------------------------------------------------------------
170 # Determine if page matches prototype or needs to be updated
171  
172 13 ▶sub unchanged_prototype {
Subroutine unchanged_prototype: called CC=2 SCAR=6.9
Pod undocumented
173 13 my ($self, $prototype, $page, $prototype_path) = @_;
174  
175 13 my $prototype_checksum =
176 $self->checksum_prototype($prototype, $prototype_path);
177  
178 13 my $page_checksum =
179 $self->checksum_prototype($page, $prototype_path);
180  
181 13 my $unchanged;
182 13 ▶ if ($prototype_checksum eq $page_checksum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prototype_checksum eq $page_checksum) { } 9 4 13
183 9 $unchanged = 1;
184 } else {
185 4 $unchanged = 0;
186 }
187  
188 13 return $unchanged;
189 }
190  
191 #----------------------------------------------------------------------
192 # Update file using prototype
193  
194 13 ▶sub update_file {
Subroutine update_file: called CC=3 SCAR=11.0
Pod undocumented
195 13 my ($self, $file, $prototype, $prototype_path) = @_;
196  
197 13 my $page = fio_read_page($file);
198 13 ▶ return unless defined $page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page 0 13 13
199  
200 # Check for changes before updating page
201 13 ▶ return 0 if $self->unchanged_prototype($prototype, $page, $prototype_path);
Branch: 1 branch
BranchTrueFalseTotal
if $self->unchanged_prototype($prototype, $page, $prototype_path) 9 4 13
202  
203 4 $page = $self->update_page($page, $prototype, $prototype_path);
204  
205 4 my $modtime = fio_get_date($file);
206 4 fio_write_page($file, $page);
207 4 fio_set_date($file, $modtime);
208  
209 4 return 1;
210 }
211  
212 #----------------------------------------------------------------------
213 # Perform all updates on the directory
214  
215 10 ▶sub update_folder {
Subroutine update_folder: called CC=9 SCAR=22.0
Pod undocumented
216 10 my ($self, $folder, $prototype_file) = @_;
217  
218 10 my $index_file = $self->to_file($folder);
219 10 my ($prototype_path, $prototype);
220 10 my $modtime = fio_get_date($folder);
221  
222 10 my $files = $self->{data}->build('files_by_mdate_reversed', $index_file);
223 10 my $file = shift(@$files);
224  
225 10 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 7 3 10
226 # The first update uses a file from the directory above
227 # as a prototype, if one is found
228  
229 7 ▶ $prototype_file ||= $self->find_prototype($folder, 1);
Condition33%1/3
$prototype_file ||= $self->find_prototype($folder, 1)
l!l&&r!l&&!r
070
MC/DC0%0/2
$prototype_file ||= $self->find_prototype($folder, 1)
$prototype_file $self->find_prototype($folder, 1)
Truth table33%1/3
A ||= B
A= $prototype_fileB= $self->find_prototype($folder, 1)
ABresult
1X1
011
000
230  
231 7 ▶ if ($prototype_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prototype_file) 7 0 7
232 7 $prototype_path = $self->get_prototype_path($prototype_file);
233 7 my $prototype = fio_read_page($prototype_file);
234  
235 7 eval {$self->update_file($file, $prototype, $prototype_path)};
236 7 $self->check_error($@, $file);
237 }
238  
239 # Subsequent updates use the most recently modified file
240 # in the directory as the prototype
241  
242 7 $prototype_file = $file;
243 7 $prototype_path = $self->get_prototype_path($prototype_file);
244 7 $prototype = fio_read_page($prototype_file);
245 }
246  
247 10 my $changes = 0;
248 10 foreach my $file (@$files) {
249 6 my $change;
250 6 eval {$change = $self->update_file($file, $prototype, $prototype_path)};
251 6 $self->check_error($@, $file);
252  
253 6 ▶ last unless $change;
Branch: 1 branch
BranchTrueFalseTotal
unless $change 3 3 6
254 3 $changes += 1;
255 }
256  
257 10 fio_set_date($folder, $modtime);
258  
259 # Update files in subdirectory
260  
261 10 ▶ if ($changes || @$files == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes or @$files == 0) 7 3 10
Condition100%3/3
$changes or @$files == 0
l!l&&r!l&&!r
163
MC/DC100%2/2
$changes or @$files == 0
$changes @$files == 0
Truth table100%3/3
A or B
A= $changesB= @$files == 0
ABresult
1X1
011
000
262 7 my $folders = $self->{data}->build('folders', $index_file);
263  
264 7 foreach my $subfolder (@$folders) {
265 3 $self->update_folder($subfolder, $prototype_file);
266 }
267 }
268  
269 10 return;
270 }
271  
272 #----------------------------------------------------------------------
273 # Parse prototype and page and combine them
274  
275 6 ▶sub update_page {
Subroutine update_page: called CC=3 SCAR=11.0
Pod undocumented
276 6 my ($self, $page, $prototype, $prototype_path) = @_;
277 6 ▶ $prototype_path = {} unless defined $prototype_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prototype_path 0 6 6
278  
279 6 my $output = [];
280 6 my $blocks = $self->parse_page($page);
281  
282 16 ▶ my $block_handler = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
283 16 my ($blockname, $locality, $blocktext) = @_;
284 16 ▶ if (exists $blocks->{$blockname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $blocks->{$blockname}) { } 15 1 16
285 15 ▶ if (exists $prototype_path->{$locality}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prototype_path->{$locality}) { } 5 10 15
286 5 push(@$output, $blocktext);
287 } else {
288 10 push(@$output, $blocks->{$blockname});
289 }
290 15 delete $blocks->{$blockname};
291 } else {
292 1 push(@$output, $blocktext);
293 }
294 16 return;
295 6 };
296  
297 22 ▶ my $prototype_handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
298 22 my ($blocktext) = @_;
299 22 push(@$output, $blocktext);
300 22 return;
301 6 };
302  
303 6 $self->parse_blocks($prototype, $block_handler, $prototype_handler);
304  
305 6 ▶ if (%$blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$blocks) 1 5 6
306 1 my $names = join(' ', sort keys %$blocks);
307 1 die "Unused blocks ($names)\n";
308 }
309  
310 5 return join('', @$output);
311 }
312  
313 2 1;
314 __END__
« lib/App/Followme/Template.pm ↑ Summary lib/App/Followme/Initialize.pm »