lib/App/Followme/EditSections.pm

filter: Statementstmt 91.4% 96 / 105 Branchbran 80.9% 34 / 42 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 93.3% 14 / 15 Podpod 25.0% 2 / 8 totaltotal 82.6% 148 / 179 CC 27 SCAR 19.5
CC
27
Cov
86%
find_location
13.9
strip_comments
7.0
update_page
6.1
CRAP 7.1
« lib/App/Followme/Web.pm ↑ Summary blib/lib/auto/share/dist/App-Followme/flatten.pl »
1 package App::Followme::EditSections;
2  
3 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw(App::Followme::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = "2.03";
15  
16 #----------------------------------------------------------------------
17 # Read the default parameter values
18  
19 16 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
20 16 my ($pkg) = @_;
21  
22 return (
23 16 remove_comments => 0,
24 data_pkg => 'App::Followme::WebData',
25 );
26 }
27  
28 #----------------------------------------------------------------------
29 # Modify pages to match the most recently modified page
30  
31 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my ($self, $folder) = @_;
33  
34 0 $self->update_folder($folder);
35 0 return;
36 }
37  
38 #----------------------------------------------------------------------
39 # Find the location of a string in a page
40  
41 42 ▶sub find_location {
Subroutine find_location: called CC=12 SCAR=26.3
Pod undocumented
42 42 my ($self, $page, $prototype, $propos, $after) = @_;
43  
44 42 my $hi;
45 42 my $lo = 0;
46 42 my $mid = 8;
47  
48 42 ▶ while ($mid > $lo && (! defined $hi || $mid < $hi)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid > $lo and !defined($hi) || $mid < $hi) 49 0 49
Condition33%2/6
$mid > $lo and !defined($hi) || $mid < $hi
!ll&&!rl&&r
0049
not defined $hi or $mid < $hi
l!l&&r!l&&!r
4900
MC/DC0%0/3
$mid > $lo and !defined($hi) || $mid < $hi
$mid > $lo not defined $hi $mid < $hi
Truth table25%1/4
A and not(B) or C
A= $mid > $loB= not defined $hiC= $mid < $hi
ABCresult
0XX0
1000
11X1
1011
49 49 my $str;
50 49 ▶ if ($after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after) { } 21 28 49
51 21 $str = substr($prototype, $propos, $mid)
52 } else {
53 28 ▶ $mid = $propos if $mid > $propos;
Branch: 1 branch
BranchTrueFalseTotal
if $mid > $propos 0 28 28
54 28 $str = substr($prototype, $propos-$mid, $mid);
55 }
56  
57 49 my $count = 0;
58 49 my $pagepos = -1;
59 49 ▶ while ($count < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 2) 98 7 105
60 98 my $newpos = index($page, $str, $pagepos+1);
61 98 ▶ last unless $newpos > $pagepos;
Branch: 1 branch
BranchTrueFalseTotal
unless $newpos > $pagepos 42 56 98
62  
63 56 $count ++;
64 56 $pagepos = $newpos;
65 }
66  
67 49 ▶ if ($count > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count > 1) { } 7 42 49
elsif ($count == 1) { } 42 0 42
68 7 $lo = $mid;
69 7 ▶ if (defined $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hi) { } 0 7 7
70 0 $mid = int(0.5 * ($mid + $hi));
71 } else {
72 7 $mid = 2 * $mid;
73 }
74  
75 } elsif ($count == 1) {
76 42 ▶ $pagepos += $mid unless $after;
Branch: 1 branch
BranchTrueFalseTotal
unless $after 21 21 42
77 42 return $pagepos;
78  
79 } else {
80 0 $hi = $mid;
81 0 $mid = int(0.5 * ($mid + $lo));
82 }
83 }
84  
85 0 return;
86 }
87  
88 #----------------------------------------------------------------------
89 # Initialize the extension
90  
91 4 ▶sub setup {
Subroutine setup: called CC=1 SCAR=0.0
Pod documented
92 4 my ($self) = @_;
93  
94 4 $self->{extension} = $self->{web_extension};
95 4 return;
96 }
97  
98 #----------------------------------------------------------------------
99 # Read page and strip comments
100  
101 13 ▶sub strip_comments {
Subroutine strip_comments: called CC=7 SCAR=19.5
Pod undocumented
102 13 my ($self, $file, $keep_sections) = @_;
103  
104 13 my $page = fio_read_page($file);
105 13 ▶ die "Could not read page" unless length($page);
Branch: 1 branch
BranchTrueFalseTotal
unless length $page 0 13 13
106  
107 13 my @output;
108 13 my @tokens = split(/(<!--.*?-->)/, $page);
109  
110 13 foreach my $token (@tokens) {
111 169 ▶ if ($token !~ /^(<!--.*?-->)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $token =~ /^(<!--.*?-->)$/) { } 91 78 169
elsif ($token =~ /(<!--\s*end)?section\s+.*?-->/) { } 24 54 78
112 91 push(@output, $token);
113 } elsif ($token =~ /(<!--\s*end)?section\s+.*?-->/) {
114 24 ▶ push(@output, $token) if $keep_sections;
Branch: 1 branch
BranchTrueFalseTotal
if $keep_sections 18 6 24
115 } else {
116 54 ▶ push(@output, $token) unless $self->{remove_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"remove_comments"} 48 6 54
117 }
118 }
119  
120 13 return join('', @output);
121 }
122  
123 #----------------------------------------------------------------------
124 # Strip file of comments and combine with prototype
125  
126 7 ▶sub update_file {
Subroutine update_file: called CC=1 SCAR=0.0
Pod undocumented
127 7 my ($self, $file, $prototype) = @_;
128  
129 7 my $page = $self->strip_comments($file, 0);
130 7 $page = $self->update_page($page, $prototype);
131 7 fio_write_page($file, $page);
132  
133 7 return;
134 }
135  
136 #----------------------------------------------------------------------
137 # Parse prototype and page and combine them
138  
139 7 ▶sub update_page {
Subroutine update_page: called CC=6 SCAR=18.1
Pod undocumented
140 7 my ($self, $page, $prototype) = @_;
141  
142 7 my @output;
143 my $notfound;
144 7 ▶ while ($prototype =~ /(<!--\s*(?:end)?section\s+.*?-->)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prototype =~ /(<!--\s*(?:end)?section\s+.*?-->)/g) 42 7 49
145 42 my $comment = $1;
146 42 my $pos = pos($prototype);
147 42 my $after = $comment =~ /<!--\s*end/;
148  
149 # locate the position of the comment from the prototype in the page
150 42 ▶ $pos = $pos - length($comment) unless $after;
Branch: 1 branch
BranchTrueFalseTotal
unless $after 21 21 42
151 42 my $loc = $self->find_location($page, $prototype, $pos, $after);
152  
153 42 ▶ unless (defined $loc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loc) 0 42 42
154 0 $notfound ++;
155 0 $loc= 0;
156 }
157  
158 # substitute comment into page
159 42 ▶ $comment = $after ? "\n$comment" : "$comment\n";
Branch: 1 branch
BranchTrueFalseTotal
$after ? : 21 21 42
160 42 push(@output, substr($page, 0, $loc), $comment);
161 42 $page = substr($page, $loc);
162 }
163  
164 7 push(@output, $page);
165  
166 7 ▶ die "Could not locate tags\n" if $notfound;
Branch: 1 branch
BranchTrueFalseTotal
if $notfound 0 7 7
167  
168 7 return join('', @output);
169 }
170  
171 #----------------------------------------------------------------------
172 # Edit all files in the directory
173  
174 2 ▶sub update_folder {
Subroutine update_folder: called CC=5 SCAR=16.1
Pod undocumented
175 2 my ($self, $folder, $prototype_file, $prototype) = @_;
176  
177 2 my $index_file = $self->to_file($folder);
178 2 my $files = $self->{data}->build('files_by_mdate_reversed', $index_file);
179  
180 2 ▶ unless (defined $prototype_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prototype_file) 1 1 2
181 1 ▶ if (@$files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$files) 1 0 1
182 1 $prototype_file = shift(@$files);
183 1 $prototype = $self->strip_comments($prototype_file, 1);
184 }
185 }
186  
187 2 foreach my $file (@$files) {
188 7 eval {$self->update_file($file, $prototype)};
189 7 $self->check_error($@, $folder);
190 }
191  
192 2 my $folders = $self->{data}->build('folders', $index_file);
193 2 for my $subfolder (@$folders) {
194 1 $self->update_folder($subfolder, $prototype_file, $prototype);
195 }
196  
197 2 return;
198 }
199  
200 1 1;
201 __END__
« lib/App/Followme/Web.pm ↑ Summary blib/lib/auto/share/dist/App-Followme/flatten.pl »