| 1 | package HTML::LinkChanger; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # Version: $Id: LinkChanger.pm 4 2007-10-05 15:51:37Z sergey.chernyshev $ | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 3 | require Exporter; | |||||||||||||
| 9 | 3 | require AutoLoader; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 3 | @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 | 3 | @EXPORT = qw( | |||||||||||||
| 16 | |||||||||||||||
| 17 | ); | ||||||||||||||
| 18 | 3 | $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 | 3 | require HTML::Parser; | |||||||||||||
| 25 | 3 | require HTML::Tagset; | |||||||||||||
| 26 | |||||||||||||||
| 27 | sub new | ||||||||||||||
| 28 | 3 | ▶ | { | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||
| 29 | 3 | my $class = shift; | |||||||||||||
| 30 | 3 | my %args = @_; | |||||||||||||
| 31 | |||||||||||||||
| 32 | 3 | 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 | ▶ | default_h => [sub { my $self = shift; $self->{_filtered_html} .= shift }, 'self,text'], | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | start_h => ['link_tag_start', 'self,tagname,text,attr,attrseq'], | ||||||||||||||
| 39 | ); | ||||||||||||||
| 40 | |||||||||||||||
| 41 | # initializing transforming functions array | ||||||||||||||
| 42 | 3 | ▶ | if (ref($url_filters) eq 'ARRAY') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 43 | { | ||||||||||||||
| 44 | 2 | foreach (@{$url_filters}) | |||||||||||||
| 45 | { | ||||||||||||||
| 46 | 2 | ▶ | die "Array must contain only HTML::LinkChanger::URLFilter objects" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | unless UNIVERSAL::isa($_, 'HTML::LinkChanger::URLFilter'); | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 2 | $self->{url_filters} = $url_filters; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | elsif (UNIVERSAL::isa($url_filters, 'HTML::LinkChanger::URLFilter')) | ||||||||||||||
| 53 | { | ||||||||||||||
| 54 | 0 | $self->{url_filters} = [$url_filters]; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | else | ||||||||||||||
| 57 | { | ||||||||||||||
| 58 | 1 | $self->{url_filters} = []; # empty array - can add more filters later | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 3 | $self; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | sub link_tag_start | ||||||||||||||
| 65 | 5 | ▶ | { | ||||||||||||
|
Subroutine
link_tag_start: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||
| 66 | 5 | my($self, $tag, $text, $attr, $attrseq) = @_; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 5 | my $link_attrs = $HTML::Tagset::linkElements{$tag}; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 5 | ▶ | if ($link_attrs) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | { | ||||||||||||||
| 72 | 5 | ▶ | $link_attrs = [$link_attrs] unless ref $link_attrs; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | 5 | for my $link_attr (@$link_attrs) | |||||||||||||
| 75 | { | ||||||||||||||
| 76 | 14 | ▶ | next unless exists $attr->{$link_attr}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | $attr->{$link_attr} = $self->change_url( | ||||||||||||||
| 78 | 5 | $attr->{$link_attr}, | |||||||||||||
| 79 | $tag, | ||||||||||||||
| 80 | $link_attr | ||||||||||||||
| 81 | ); | ||||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 5 | my $output='<'.$tag; | |||||||||||||
| 85 | 5 | foreach my $attribute (@$attrseq) | |||||||||||||
| 86 | { | ||||||||||||||
| 87 | 5 | $output.=' '.$attribute.'="'.$attr->{$attribute}.'"'; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | 5 | $output.='>'; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 5 | $self->{_filtered_html} .= $output; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | else | ||||||||||||||
| 94 | { | ||||||||||||||
| 95 | 0 | $self->{_filtered_html} .= $text; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | sub filter | ||||||||||||||
| 100 | 4 | ▶ | { | ||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 101 | 4 | my $self = shift; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 4 | delete $self->{_filtered_html}; | |||||||||||||
| 104 | 4 | $self->parse(@_); | |||||||||||||
| 105 | 4 | $self->eof; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 4 | return $self->{_filtered_html}; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | sub filter_file | ||||||||||||||
| 111 | 0 | ▶ | { | ||||||||||||
|
Subroutine
filter_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | delete $self->{_filtered_html}; | |||||||||||||
| 115 | 0 | $self->parse_file(@_); | |||||||||||||
| 116 | 0 | $self->eof; | |||||||||||||
| 117 | |||||||||||||||
| 118 | 0 | return $self->{_filtered_html}; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | sub filtered_html | ||||||||||||||
| 122 | 0 | ▶ | { | ||||||||||||
|
Subroutine
filtered_html: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||
| 124 | 0 | return $self->{_filtered_html}; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | sub change_url | ||||||||||||||
| 128 | 5 | ▶ | { | ||||||||||||
|
Subroutine
change_url: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 129 | 5 | my $self = shift; | |||||||||||||
| 130 | 5 | my $url = shift; # url of the link | |||||||||||||
| 131 | 5 | my $tag = shift; # tag containing a link to change | |||||||||||||
| 132 | 5 | my $attr = shift; # attribute containing a link to change | |||||||||||||
| 133 | |||||||||||||||
| 134 | 5 | foreach my $filter (@{$self->{url_filters}}) | |||||||||||||
| 135 | { | ||||||||||||||
| 136 | 3 | $url = $filter->url_filter( | |||||||||||||
| 137 | url => $url, | ||||||||||||||
| 138 | tag => $tag, | ||||||||||||||
| 139 | attr => $attr | ||||||||||||||
| 140 | ); | ||||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 5 | return $url; # abstract class just keeps everything as it is | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 3 | 1; | |||||||||||||
| 147 | __END__ | ||||||||||||||