SiteTear/PageFilter.pm

filter: Statementstmt 30.3% 34 / 112 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 10 / 20 Podpod 88.8% 8 / 9 totaltotal 24.8% 52 / 209 CC 32 SCAR 58.9
CC
32
Cov
19%
start
702.0
comment
20.0
__ANON__
20.0
CRAP 361.5
↑ Summary SiteTear/Item.pm »
1 package HTML::SiteTear::PageFilter;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Data::Dumper;
10  
11 1 ▶use HTML::Parser 3.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTML::HeadParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use base qw(HTML::Parser Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 __PACKAGE__->mk_accessors(qw(has_remote_base
15 page));
16  
17 1 ▶use HTML::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '1.45';
20 1 our @htmlSuffix = qw(.html .htm .xhtml);
21  
22 =head1 NAME
23  
24 HTML::SiteTear::PageFilter - change link pathes in HTML files.
25  
26 =head1 SYMPOSIS
27  
28 use HTML::SiteTear::PageFilter;
29  
30 # $page must be an instance of L<HTML::SiteTear::Page>.
31 $filter = HTML::SiteTear::PageFilter->new($page);
32 $fileter->parse_file();
33  
34 =head1 DESCRIPTION
35  
36 This module is to change link pathes in HTML files. It's a sub class of L<HTML::Parser>. Internal use only.
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 $filter = HTML::SiteTear::PageFilter->new($page);
43  
44 Make an instance of this moduel. $parent must be an instance of HTML::SiteTear::Root or HTML::SiteTear::Page. This method is called from $parent.
45  
46 =cut
47  
48 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
49 0 my ($class, $page) = @_;
50 0 my $parent = $class->SUPER::new();
51 0 my $self = bless $parent, $class;
52 0 $self->page($page);
53 0 $self->{'allow_abs_link'} = $page->source_root->allow_abs_link;
54 0 $self->{'use_abs_link'} = 0;
55 0 $self->attr_encoded(1);
56 0 $self->has_remote_base(0);
57 0 return $self;
58 }
59  
60 =head2 parse_file
61  
62 $filter->parse_file;
63  
64 Parse the HTML file given by $page and change link pathes. The output data are retuned thru the method "write_data".
65  
66 =cut
67  
68 0 ▶sub parse_file {
Subroutine parse_file: not called CC=1 SCAR=6.9
Pod documented
69 0 my ($self) = @_;
70 0 my $p = HTML::Copy->new($self->page->source_path);
71 0 $self->page->set_binmode($p->io_layer);
72 0 $self->SUPER::parse($p->source_html);
73 }
74  
75 =head1 SEE ALOSO
76  
77 L<HTML::SiteTear>, L<HTML::SiteTear::Item>, L<HTML::SiteTear::Root>, L<HTML::SiteTear:Page>
78  
79 =head1 AUTHOR
80  
81 Tetsuro KURITA <tkurita@mac.com>
82  
83 =cut
84  
85 ##== private methods
86 0 ▶sub output {
Subroutine output: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($self, $data) = @_;
88 0 $self->page->write_data($data);
89 }
90  
91 ##== overriding methods of HTML::Parser
92  
93 0 ▶sub declaration { $_[0]->output("<!$_[1]>") }
Subroutine declaration: not called CC=1 SCAR=6.9
Pod documented
94 0 ▶sub process { $_[0]->output($_[2]) }
Subroutine process: not called CC=1 SCAR=6.9
Pod documented
95 0 ▶sub end { $_[0]->output($_[2]) }
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
96 0 ▶sub text { $_[0]->output($_[1]) }
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
97  
98 0 ▶sub comment {
Subroutine comment: not called CC=4 SCAR=30.0
Pod documented
99 0 my ($self, $comment) = @_;
100  
101 0 ▶ if ($self->{'allow_abs_link'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"allow_abs_link"}) 0 0 0
102 0 ▶ if ($comment =~ /^\s*begin abs_link/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($comment =~ /^\s*begin abs_link/) { } 0 0 0
elsif ($comment =~ /^\s*end abs_link/) { } 0 0 0
103 0 $self->{'use_abs_link'} = 1;
104
105 } elsif($comment =~ /^\s*end abs_link/) {
106 0 $self->{'use_abs_link'} = 0;
107 }
108 }
109  
110 0 $self->output("<!--$comment-->");
111 }
112  
113 0 ▶sub start {
Subroutine start: not called CC=26 SCAR=65.5
Pod documented
114 0 my ($self, $tag, $attr_dict, $attr_names, $tag_text) = @_;
115 0 my $page = $self->page;
116 0 ▶ my $empty_tag_end = ($tag =~ /\/>$/) ? ' />' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ m[/>$] ? : 0 0 0
117
118 0 ▶ if ($self->has_remote_base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_remote_base) 0 0 0
119 0 return $self->output($tag_text);
120 }
121
122 0 ▶ my $process_link = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
123 0 my ($target_attr, $folder_name, $kind) = @_;
124 0 ▶ if (my $link = $attr_dict->{$target_attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link = $attr_dict->{$target_attr}) 0 0 0
125 0 ▶ if ($self->{'use_abs_link'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_abs_link'}) { } 0 0 0
126 0 $attr_dict->{$target_attr} = $page->build_abs_url($link);
127 } else {
128 0 ▶ unless ($kind) {$kind = $folder_name};
Branch: 1 branch
BranchTrueFalseTotal
unless ($kind) 0 0 0
129 0 $attr_dict->{$target_attr}
130 = $page->change_path($link, $folder_name, $kind);
131 }
132 0 return HTML::Copy->build_attributes($attr_dict, $attr_names);
133 }
134 0 return ();
135 0 };
136
137 0 ▶ if ($tag eq 'base') {
Branch: 8 branches
BranchTrueFalseTotal
if ($tag eq 'base') { } 0 0 0
elsif ($tag eq 'img') { } 0 0 0
elsif ($tag eq 'body') { } 0 0 0
elsif ($tag eq 'link') { } 0 0 0
elsif ($tag eq 'frame') { } 0 0 0
elsif ($tag eq 'script' or $tag eq 'embed') { } 0 0 0
elsif ($tag eq 'a') { } 0 0 0
elsif ($tag eq 'param') { } 0 0 0
138 0 my $uri = URI->new($attr_dict->{'href'});
139 0 ▶ if (!($uri->scheme) or ($uri->scheme eq 'file')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $uri->scheme or $uri->scheme eq 'file') { } 0 0 0
Condition0%0/3
not $uri->scheme or $uri->scheme eq "file"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $uri->scheme or $uri->scheme eq "file"
not $uri->scheme $uri->scheme eq "file"
Truth table0%0/3
not(A) or B
A= not $uri->schemeB= $uri->scheme eq "file"
ABresult
1X1
011
000
140 0 $page->base_uri($uri->abs($page->base_uri));
141 0 $tag_text = '';
142 } else {
143 0 $self->has_remote_base(1);
144 }
145 #treat image files
146 } elsif ($tag eq 'img') {
147 0 ▶ if (my $tag_attrs = &$process_link('src', $page->resource_folder_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag_attrs = &$process_link("src", $page->resource_folder_name)) 0 0 0
148 0 $tag_text = "<$tag $tag_attrs".$empty_tag_end;
149 }
150  
151 } elsif ($tag eq 'body') { #background images
152 0 ▶ if (my $tag_attrs = &$process_link('background', $page->resource_folder_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag_attrs = &$process_link("background", $page->resource_folder_name)) 0 0 0
153 0 $tag_text = "<$tag $tag_attrs>";
154 }
155 }
156 #linked stylesheet
157 elsif ($tag eq 'link') {
158 0 my $folder_name = $page->resource_folder_name;
159 0 my $kind = $folder_name;
160 0 my $relation;
161 0 ▶ if (defined( $relation = ($attr_dict ->{'rel'}) )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($relation = $attr_dict->{"rel"})) 0 0 0
162 0 $relation = lc $relation;
163 0 ▶ if ($relation eq 'stylesheet') {
Branch: 1 branch
BranchTrueFalseTotal
if ($relation eq "stylesheet") 0 0 0
164 0 $kind = 'css';
165 }
166 }
167
168 0 ▶ if (my $tag_attrs = &$process_link('href', $folder_name, $kind)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag_attrs = &$process_link("href", $folder_name, $kind)) 0 0 0
169 0 $tag_text = "<$tag $tag_attrs".$empty_tag_end;
170 }
171 }
172 #frame
173 elsif ($tag eq 'frame') {
174 0 ▶ if (my $tag_attrs = &$process_link('src', $page->page_folder_name, 'page')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag_attrs = &$process_link("src", $page->page_folder_name, "page")) 0 0 0
175 0 $tag_text = "<$tag $tag_attrs".$empty_tag_end;
176 }
177 }
178 #javascript
179 ▶ elsif (($tag eq 'script') or ($tag eq 'embed')) {
Condition0%0/3
$tag eq "script" or $tag eq "embed"
l!l&&r!l&&!r
000
MC/DC0%0/2
$tag eq "script" or $tag eq "embed"
$tag eq "script" $tag eq "embed"
Truth table0%0/3
A or B
A= $tag eq "script"B= $tag eq "embed"
ABresult
1X1
011
000
180 0 ▶ if (my $tag_attrs = &$process_link('src', $page->resource_folder_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag_attrs = &$process_link("src", $page->resource_folder_name)) 0 0 0
181 0 $tag_text = "<$tag $tag_attrs>";
182 }
183 }
184 #link
185 elsif ($tag eq 'a') {
186 0 ▶ if ( exists($attr_dict->{'href'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr_dict->{"href"}) 0 0 0
187 0 my $href = $attr_dict->{'href'};
188 0 my $kind = 'page';
189 0 my $folder_name = $page->page_folder_name;
190 0 ▶ if ($href !~/(.+)#(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($href =~ /(.+)#(.*)/) 0 0 0
191 0 my @matchedSuffix = grep {$href =~ /\Q$_\E$/} @htmlSuffix;
192 0 ▶ unless (@matchedSuffix) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@matchedSuffix) 0 0 0
193 0 $folder_name = $page->resource_folder_name;
194 0 $kind = $folder_name;
195 }
196 }
197 0 ▶ if (my $tag_attrs = &$process_link('href', $folder_name, $kind)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag_attrs = &$process_link("href", $folder_name, $kind)) 0 0 0
198 0 $tag_text = "<$tag $tag_attrs>";
199 }
200 }
201 }
202 elsif ($tag eq 'param') {
203 0 ▶ if (my $tag_attrs = &$process_link('src', $page->resource_folder_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag_attrs = &$process_link("src", $page->resource_folder_name)) 0 0 0
204 0 $tag_text = "<$tag $tag_attrs".$empty_tag_end;
205 }
206 }
207
208 0 $self->output($tag_text);
209 }
210  
211 1 1;
↑ Summary SiteTear/Item.pm »