blib/lib/HTML/Links/Localize.pm

filter: Statementstmt 46.6% 62 / 133 Branchbran 9.0% 4 / 44 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 71.4% 15 / 21 Podpod 100% 4 / 4 totaltotal 39.1% 87 / 222 CC 32 SCAR 43.0
CC
32
Cov
36%
process_content
240.0
process_dir_tree
13.8
process_file
12.0
CRAP 73.9
↑ Summary Build »
1 package HTML::Links::Localize;
2 3 $HTML::Links::Localize::VERSION = '0.2.10';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Two utility functions
13 sub _is_older
14 {
15 0 ▶ my $file1 = shift;
Subroutine _is_older: not called CC=1 SCAR=6.9
16 0 my $file2 = shift;
17 0 my @stat1 = stat($file1);
18 0 my @stat2 = stat($file2);
19 0 return ($stat1[9] <= $stat2[9]);
20 }
21  
22 sub _is_newer
23 {
24 0 ▶ my $file1 = shift;
Subroutine _is_newer: not called CC=1 SCAR=6.9
25 0 my $file2 = shift;
26 0 return (! _is_older($file1, $file2));
27 }
28  
29 sub new
30 {
31 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 1 my $self = {};
33 1 bless $self, $class;
34  
35 1 $self->_init(@_);
36  
37 1 return $self;
38 }
39  
40 sub _set_base_dir
41 {
42 1 ▶ my $self = shift;
Subroutine _set_base_dir: called CC=1 SCAR=0.0
43  
44 1 my $base_dir = shift;
45  
46 1 $base_dir =~ s{/*$}{/};
47  
48 1 $self->{'base_dir'} = $base_dir;
49  
50 1 return 0;
51 }
52  
53 sub _get_base_dir
54 {
55 1 ▶ my $self = shift;
Subroutine _get_base_dir: called CC=1 SCAR=0.0
56  
57 1 return $self->{'base_dir'};
58 }
59  
60 sub _set_dest_dir
61 {
62 1 ▶ my $self = shift;
Subroutine _set_dest_dir: called CC=1 SCAR=0.0
63  
64 1 my $dest_dir = shift;
65  
66 1 $self->{'dest_dir'} = $dest_dir;
67  
68 1 return 0;
69 }
70  
71 sub _get_dest_dir
72 {
73 1 ▶ my $self = shift;
Subroutine _get_dest_dir: called CC=1 SCAR=0.0
74  
75 1 return $self->{'dest_dir'};
76 }
77  
78 sub _init
79 {
80 1 ▶ my $self = shift;
Subroutine _init: called CC=3 SCAR=11.3
81  
82 1 my %args = @_;
83  
84 1 ▶ $self->_set_base_dir($args{'base_dir'} || ".");
Condition50%1/2
$args{'base_dir'} || '.'
l!l
10
MC/DC0%0/1
$args{'base_dir'} || '.'
$args{'base_dir'}
Truth table50%1/2
A
A= $args{'base_dir'}
Aresult
11
00
85  
86 1 ▶ $self->_set_dest_dir($args{'dest_dir'} || "./dest");
Condition50%1/2
$args{'dest_dir'} || './dest'
l!l
10
MC/DC0%0/1
$args{'dest_dir'} || './dest'
$args{'dest_dir'}
Truth table50%1/2
A
A= $args{'dest_dir'}
Aresult
11
00
87  
88 1 return 0;
89 }
90  
91 sub process_content
92 {
93 0 ▶ my $self = shift;
Subroutine process_content: not called CC=15 SCAR=54.8
Pod documented
94  
95 0 my $fh = shift;
96  
97 0 my $out_content = "";
98  
99 my $out = sub {
100 0 ▶ $out_content .= join("", @_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 };
102  
103 0 my $parser = HTML::TokeParser->new($fh);
104 0 ▶ while (my $token = $parser->get_token())
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $parser->get_token) 0 0 0
105 {
106 0 my $type = $token->[0];
107 0 ▶ if ($type eq "E")
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'E') { } 0 0 0
elsif ($type eq 'C') { } 0 0 0
elsif ($type eq 'T') { } 0 0 0
elsif ($type eq 'D') { } 0 0 0
elsif ($type eq 'PI') { } 0 0 0
elsif ($type eq 'S') { } 0 0 0
108 {
109 0 $out->($token->[2]);
110 }
111 elsif ($type eq "C")
112 {
113 0 $out->($token->[1]);
114 }
115 elsif ($type eq "T")
116 {
117 0 $out->($token->[1]);
118 }
119 elsif ($type eq "D")
120 {
121 0 $out->($token->[1]);
122 }
123 elsif ($type eq "PI")
124 {
125 0 $out->($token->[2]);
126 }
127 elsif ($type eq "S")
128 {
129 0 my $tag = $token->[1];
130 0 my %process_tags =
131 (
132 'form' => { 'action' => 1 },
133 'img' => { 'src' => 1},
134 'a' => { 'href' => 1},
135 'link' => { 'href' => 1},
136 );
137 0 ▶ if (exists($process_tags{$tag}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $process_tags{$tag}) { } 0 0 0
138 {
139 0 my $ret = "<$tag";
140 0 my $attrseq = $token->[3];
141 0 my $attr_values = $token->[2];
142 0 my $process_attrs = $process_tags{$tag};
143 0 foreach my $attr (@$attrseq)
144 {
145 0 my $value = $attr_values->{$attr};
146 0 ▶ if (exists($process_attrs->{$attr}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $process_attrs->{$attr}) 0 0 0
147 {
148 # If it's a local link that ends with slash -
149 # then append index.html
150 0 ▶ if (($value !~ /^[a-z]+:/) && ($value !~ /^\//) &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $value =~ /^[a-z]+:/ || $value =~ m[^/] and $value =~ m[/(#[^#/]*)?$]) 0 0 0
Condition0%0/6
not $value =~ /^[a-z]+:/ || $value =~ m[^/] and $value =~ m[/(#[^#/]*)?$]
!ll&&!rl&&r
000
$value =~ /^[a-z]+:/ || $value =~ m[^/]
l!l&&r!l&&!r
000
MC/DC0%0/3
not $value =~ /^[a-z]+:/ || $value =~ m[^/] and $value =~ m[/(#[^#/]*)?$]
$value =~ /^[a-z]+:/ $value =~ m[^/] $value =~ m[/(#[^#/]*)?$]
Truth table0%0/4
not(A || B) and C
A= $value =~ /^[a-z]+:/B= $value =~ m[^/]C= $value =~ m[/(#[^#/]*)?$]
ABCresult
1XX0
01X0
0000
0011
151 ($value =~ /\/(#[^#\/]*)?$/))
152 {
153 0 my $pos = rindex($value, "/");
154 0 substr($value,$pos+1,0) = "index.html";
155 }
156 }
157 0 ▶ if ($attr eq "/")
Branch: 1 branch
BranchTrueFalseTotal
if ($attr eq '/') { } 0 0 0
158 {
159 0 $ret .= " /";
160 }
161 else
162 {
163 0 $ret .= " $attr=\"$value\"";
164 }
165 }
166 0 $out->($ret);
167 0 $out->(">");
168 }
169 else
170 {
171 0 $out->($token->[4]);
172 }
173 }
174 }
175  
176 0 return $out_content;
177 }
178  
179 sub process_file
180 {
181 0 ▶ my $self = shift;
Subroutine process_file: not called CC=3 SCAR=24.8
Pod documented
182 0 my $filename = shift;
183  
184 0 my $dest_dir = $self->_get_dest_dir();
185 0 my $src_dir = $self->_get_base_dir();
186  
187 0 ▶ open my $in, '<', "$src_dir/$filename"
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", "$src_dir/$filename" or die "Cannot open '$src_dir/${filename}' - $!" 0 0 0
188 or die "Cannot open '$src_dir/$filename' - $!";
189 0 ▶ open my $out, '>', "$dest_dir/$filename"
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", "$dest_dir/$filename" or die "Cannot open '$dest_dir/${filename}' for writing- $!" 0 0 0
190 or die "Cannot open '$dest_dir/$filename' for writing- $!";
191 0 print {$out} $self->process_content($in);
192 0 close($in);
193 0 close($out);
194 }
195  
196 sub process_dir_tree
197 {
198 1 ▶ my $self = shift;
Subroutine process_dir_tree: called CC=6 SCAR=26.2
Pod documented
199  
200 1 my %args = @_;
201  
202 my $should_replace_file = sub {
203 0 ▶ my ($src, $dest) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
204 0 ▶ if ($args{'only-newer'})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'only-newer'}) { } 0 0 0
205 {
206 0 ▶ return ((! -e $dest) || (_is_newer($src, $dest)));
Condition0%0/3
!(-e $dest) || _is_newer($src, $dest)
l!l&&r!l&&!r
000
MC/DC0%0/2
!(-e $dest) || _is_newer($src, $dest)
!(-e $dest) _is_newer($src, $dest)
Truth table0%0/3
not(A) || B
A= !(-e $dest)B= _is_newer($src, $dest)
ABresult
1X1
011
000
207 }
208 else
209 {
210 0 return 1;
211 }
212 1 };
213  
214 1 my $src_dir = $self->_get_base_dir();
215 1 my $dest_dir = $self->_get_dest_dir();
216  
217 1 my (@dirs, @other_files, @html_files);
218  
219 my $wanted = sub {
220 1 ▶ my $filename = $File::Find::name;
Subroutine __ANON__: called CC=4 SCAR=22.9
221 1 ▶ if (length($filename) < length($src_dir))
Branch: 1 branch
BranchTrueFalseTotal
if (length $filename < length $src_dir) 1 0 1
222 {
223 1 return;
224 }
225 # Remove the $src_dir from the filename;
226 0 $filename = substr($filename, length($src_dir));
227  
228 0 ▶ if (-d $_)
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_) { } 0 0 0
elsif (/\.html?$/) { } 0 0 0
229 {
230 0 push @dirs, $filename;
231 }
232 elsif (/\.html?$/)
233 {
234 0 push @html_files, $filename;
235 }
236 else
237 {
238 0 push @other_files, $filename;
239 }
240 1 };
241  
242 1 find($wanted, $src_dir);
243  
244 my $soft_mkdir = sub {
245 1 ▶ my $dir = shift;
Subroutine __ANON__: called CC=4 SCAR=16.1
246 1 ▶ if (-d $dir)
Branch: 2 branches
BranchTrueFalseTotal
if (-d $dir) { } 0 1 1
elsif (-e $dir) { } 0 1 1
247 {
248 # Do nothing
249 }
250 elsif (-e $dir)
251 {
252 0 die "$dir exists in destination and is not a directory";
253 }
254 else
255 {
256 1 ▶ mkdir($dir) || die "mkdir failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die "mkdir failed: $!\n" 0 1 1
257 }
258 1 };
259  
260 # Create the directory structure in $dest
261  
262 1 $soft_mkdir->($dest_dir);
263 1 foreach my $dir (@dirs)
264 {
265 0 $soft_mkdir->("$dest_dir/$dir");
266 }
267  
268 1 foreach my $file (@other_files)
269 {
270 0 my $src = "$src_dir/$file";
271 0 my $dest = "$dest_dir/$file";
272 0 ▶ if ($should_replace_file->($src, $dest))
Branch: 1 branch
BranchTrueFalseTotal
if (&$should_replace_file($src, $dest)) 0 0 0
273 {
274 0 copy($src, $dest);
275 }
276 }
277  
278 1 foreach my $file (@html_files)
279 {
280 0 my $src = "$src_dir/$file";
281 0 my $dest = "$dest_dir/$file";
282 0 ▶ if ($should_replace_file->($src,$dest))
Branch: 1 branch
BranchTrueFalseTotal
if (&$should_replace_file($src, $dest)) 0 0 0
283 {
284 0 $self->process_file($file);
285 }
286 }
287  
288 1 return 0;
289 }
290  
291 3 1;
292  
293 __END__
↑ Summary Build »