| 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.43'; | |||||||||||||||||||||||||||||||||
| 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 | sub new { | ||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my ($class, $page) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 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->has_remote_base(0); | |||||||||||||||||||||||||||||||||
| 56 | 0 | return $self; | |||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | =head2 parse_file | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | $filter->parse_file; | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | Parse the HTML file given by $page and change link pathes. The output data are retuned thru the method "write_data". | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | sub parse_file { | ||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
parse_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 69 | 0 | my $p = HTML::Copy->new($self->page->source_path); | |||||||||||||||||||||||||||||||||
| 70 | 0 | $self->page->set_binmode($p->io_layer); | |||||||||||||||||||||||||||||||||
| 71 | 0 | $self->SUPER::parse($p->source_html); | |||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | =head1 SEE ALOSO | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | L<HTML::SiteTear>, L<HTML::SiteTear::Item>, L<HTML::SiteTear::Root>, L<HTML::SiteTear:Page> | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | Tetsuro KURITA <tkurita@mac.com> | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | ##== private methods | ||||||||||||||||||||||||||||||||||
| 85 | sub output { | ||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
output: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->page->write_data($data); | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | ##== overriding methods of HTML::Parser | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub declaration { $_[0]->output("<!$_[1]>") } | ||||||||||||||||||||||||||||||||
|
Subroutine
declaration: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub process { $_[0]->output($_[2]) } | ||||||||||||||||||||||||||||||||
|
Subroutine
process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub end { $_[0]->output($_[2]) } | ||||||||||||||||||||||||||||||||
|
Subroutine
end: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub text { $_[0]->output($_[1]) } | ||||||||||||||||||||||||||||||||
|
Subroutine
text: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | sub comment { | ||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my ($self, $comment) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
comment: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($self->{'allow_abs_link'}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($comment =~ /^\s*begin abs_link/) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 102 | 0 | $self->{'use_abs_link'} = 1; | |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | } elsif($comment =~ /^\s*end abs_link/) { | ||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->{'use_abs_link'} = 0; | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | 0 | $self->output("<!--$comment-->"); | |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | sub start { | ||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($self, $tag, $attr_dict, $attr_names, $tag_text) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=23 SCAR=63.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 114 | 0 | my $page = $self->page; | |||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $empty_tag_end = ($tag =~ /\/>$/) ? ' />' : '>'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($self->has_remote_base) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 0 | return $self->output($tag_text); | |||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | my $process_link = sub { | ||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my ($target_attr, $folder_name, $kind) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (my $link = $attr_dict->{$target_attr}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ($self->{'use_abs_link'}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 125 | 0 | $attr_dict->{$target_attr} = $page->build_abs_url($link); | |||||||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | unless ($kind) {$kind = $folder_name}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 128 | 0 | $attr_dict->{$target_attr} | |||||||||||||||||||||||||||||||||
| 129 | = $page->change_path($link, $folder_name, $kind); | ||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | 0 | return HTML::Copy->build_attributes($attr_dict, $attr_names); | |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | 0 | return (); | |||||||||||||||||||||||||||||||||
| 134 | 0 | }; | |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | #treat image files | ||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($tag eq 'base') { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 138 | 0 | my $uri = URI->new($attr_dict->{'href'}); | |||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if (!($uri->scheme) or ($uri->scheme eq 'file')) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $uri->scheme or $uri->scheme eq "file"
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"
| |||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||
| 146 | } elsif ($tag eq 'img') { | ||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if (my $tag_attrs = &$process_link('src', $page->resource_folder_name)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 162 | 0 | $relation = lc $relation; | |||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ($relation eq 'stylesheet') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 164 | 0 | $kind = 'css'; | |||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if (my $tag_attrs = &$process_link('href', $folder_name, $kind)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 175 | 0 | $tag_text = "<$tag $tag_attrs".$empty_tag_end; | |||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | #javascript | ||||||||||||||||||||||||||||||||||
| 179 | elsif ($tag eq 'script') { | ||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if (my $tag_attrs = &$process_link('src', $page->resource_folder_name)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 191 | 0 | my @matchedSuffix = grep {$href =~ /\Q$_\E$/} @htmlSuffix; | |||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | unless (@matchedSuffix) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 198 | 0 | $tag_text = "<$tag $tag_attrs>"; | |||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||
| 203 | 0 | $self->output($tag_text); | |||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | 1 | 1; | |||||||||||||||||||||||||||||||||