line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::LinkChanger; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Version: $Id: LinkChanger.pm 4 2007-10-05 15:51:37Z sergey.chernyshev $ |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
23607
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
136
|
|
6
|
3
|
|
|
3
|
|
17
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
2299
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
require AutoLoader; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
@ISA = qw(Exporter AutoLoader HTML::Parser); |
12
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
13
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
14
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
15
|
|
|
|
|
|
|
@EXPORT = qw( |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
$VERSION = sprintf("2.%d", q$Rev: 4 $ =~ /(\d+)/); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Preloaded methods go here. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Autoload methods go after =cut, and are processed by the autosplit program. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
require HTML::Parser; |
25
|
|
|
|
|
|
|
require HTML::Tagset; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new |
28
|
|
|
|
|
|
|
{ |
29
|
3
|
|
|
3
|
1
|
232
|
my $class = shift; |
30
|
3
|
|
|
|
|
12
|
my %args = @_; |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
11
|
my $url_filters = $args{'url_filters'}; # reference to array HTML::LinkChanger::URLFilter objects |
33
|
|
|
|
|
|
|
# or reference to one such object |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $self = $class->SUPER::new( |
36
|
|
|
|
|
|
|
api_version => 3, |
37
|
3
|
|
|
14
|
|
67
|
default_h => [sub { my $self = shift; $self->{_filtered_html} .= shift }, 'self,text'], |
|
14
|
|
|
|
|
20
|
|
|
14
|
|
|
|
|
97
|
|
38
|
|
|
|
|
|
|
start_h => ['link_tag_start', 'self,tagname,text,attr,attrseq'], |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# initializing transforming functions array |
42
|
3
|
100
|
|
|
|
291
|
if (ref($url_filters) eq 'ARRAY') |
|
|
50
|
|
|
|
|
|
43
|
|
|
|
|
|
|
{ |
44
|
2
|
|
|
|
|
5
|
foreach (@{$url_filters}) |
|
2
|
|
|
|
|
8
|
|
45
|
|
|
|
|
|
|
{ |
46
|
2
|
50
|
|
|
|
19
|
die "Array must contain only HTML::LinkChanger::URLFilter objects" |
47
|
|
|
|
|
|
|
unless UNIVERSAL::isa($_, 'HTML::LinkChanger::URLFilter'); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
18
|
$self->{url_filters} = $url_filters; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
elsif (UNIVERSAL::isa($url_filters, 'HTML::LinkChanger::URLFilter')) |
53
|
|
|
|
|
|
|
{ |
54
|
0
|
|
|
|
|
0
|
$self->{url_filters} = [$url_filters]; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else |
57
|
|
|
|
|
|
|
{ |
58
|
1
|
|
|
|
|
8
|
$self->{url_filters} = []; # empty array - can add more filters later |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
12
|
$self; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub link_tag_start |
65
|
|
|
|
|
|
|
{ |
66
|
5
|
|
|
5
|
0
|
12
|
my($self, $tag, $text, $attr, $attrseq) = @_; |
67
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
14
|
my $link_attrs = $HTML::Tagset::linkElements{$tag}; |
69
|
|
|
|
|
|
|
|
70
|
5
|
50
|
|
|
|
15
|
if ($link_attrs) |
71
|
|
|
|
|
|
|
{ |
72
|
5
|
50
|
|
|
|
16
|
$link_attrs = [$link_attrs] unless ref $link_attrs; |
73
|
|
|
|
|
|
|
|
74
|
5
|
|
|
|
|
16
|
for my $link_attr (@$link_attrs) |
75
|
|
|
|
|
|
|
{ |
76
|
14
|
100
|
|
|
|
44
|
next unless exists $attr->{$link_attr}; |
77
|
5
|
|
|
|
|
37
|
$attr->{$link_attr} = $self->change_url( |
78
|
|
|
|
|
|
|
$attr->{$link_attr}, |
79
|
|
|
|
|
|
|
$tag, |
80
|
|
|
|
|
|
|
$link_attr |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
5
|
|
|
|
|
11
|
my $output='<'.$tag; |
85
|
5
|
|
|
|
|
10
|
foreach my $attribute (@$attrseq) |
86
|
|
|
|
|
|
|
{ |
87
|
5
|
|
|
|
|
30
|
$output.=' '.$attribute.'="'.$attr->{$attribute}.'"'; |
88
|
|
|
|
|
|
|
} |
89
|
5
|
|
|
|
|
26
|
$output.='>'; |
90
|
|
|
|
|
|
|
|
91
|
5
|
|
|
|
|
66
|
$self->{_filtered_html} .= $output; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else |
94
|
|
|
|
|
|
|
{ |
95
|
0
|
|
|
|
|
0
|
$self->{_filtered_html} .= $text; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub filter |
100
|
|
|
|
|
|
|
{ |
101
|
4
|
|
|
4
|
0
|
384
|
my $self = shift; |
102
|
|
|
|
|
|
|
|
103
|
4
|
|
|
|
|
9
|
delete $self->{_filtered_html}; |
104
|
4
|
|
|
|
|
64
|
$self->parse(@_); |
105
|
4
|
|
|
|
|
34
|
$self->eof; |
106
|
|
|
|
|
|
|
|
107
|
4
|
|
|
|
|
17
|
return $self->{_filtered_html}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub filter_file |
111
|
|
|
|
|
|
|
{ |
112
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
0
|
delete $self->{_filtered_html}; |
115
|
0
|
|
|
|
|
0
|
$self->parse_file(@_); |
116
|
0
|
|
|
|
|
0
|
$self->eof; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
0
|
return $self->{_filtered_html}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub filtered_html |
122
|
|
|
|
|
|
|
{ |
123
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
124
|
0
|
|
|
|
|
0
|
return $self->{_filtered_html}; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub change_url |
128
|
|
|
|
|
|
|
{ |
129
|
5
|
|
|
5
|
0
|
11
|
my $self = shift; |
130
|
5
|
|
|
|
|
9
|
my $url = shift; # url of the link |
131
|
5
|
|
|
|
|
12
|
my $tag = shift; # tag containing a link to change |
132
|
5
|
|
|
|
|
7
|
my $attr = shift; # attribute containing a link to change |
133
|
|
|
|
|
|
|
|
134
|
5
|
|
|
|
|
9
|
foreach my $filter (@{$self->{url_filters}}) |
|
5
|
|
|
|
|
12
|
|
135
|
|
|
|
|
|
|
{ |
136
|
3
|
|
|
|
|
13
|
$url = $filter->url_filter( |
137
|
|
|
|
|
|
|
url => $url, |
138
|
|
|
|
|
|
|
tag => $tag, |
139
|
|
|
|
|
|
|
attr => $attr |
140
|
|
|
|
|
|
|
); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
5
|
|
|
|
|
1040
|
return $url; # abstract class just keeps everything as it is |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
1; |
147
|
|
|
|
|
|
|
__END__ |