| 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
Condition33%2/6
$mid > $lo and !defined($hi) || $mid < $hi
not defined $hi or $mid < $hi
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
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 49 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 49 | ▶ | if ($after) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 21 | $str = substr($prototype, $propos, $mid) | |||||||||||||||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 28 | ▶ | $mid = $propos if $mid > $propos; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 98 | my $newpos = index($page, $str, $pagepos+1); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 98 | ▶ | last unless $newpos > $pagepos; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 56 | $count ++; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 56 | $pagepos = $newpos; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 49 | ▶ | if ($count > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | $lo = $mid; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 7 | ▶ | if (defined $hi) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 91 | push(@output, $token); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } elsif ($token =~ /(<!--\s*end)?section\s+.*?-->/) { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 24 | ▶ | push(@output, $token) if $keep_sections; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 54 | ▶ | push(@output, $token) unless $self->{remove_comments}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 42 | my $loc = $self->find_location($page, $prototype, $pos, $after); | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 42 | ▶ | unless (defined $loc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | if (@$files) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||