line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::LinkTarget; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
############################################################################### |
4
|
|
|
|
|
|
|
# Required inclusions. |
5
|
|
|
|
|
|
|
############################################################################### |
6
|
1
|
|
|
1
|
|
58985
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
7
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
644
|
use HTML::Parser; |
|
1
|
|
|
|
|
5692
|
|
|
1
|
|
|
|
|
37
|
|
9
|
1
|
|
|
1
|
|
6
|
use HTML::Entities qw(encode_entities); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
66
|
|
10
|
1
|
|
|
1
|
|
10
|
use base qw(Template::Plugin::Filter); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
639
|
|
11
|
1
|
|
|
1
|
|
2640
|
use namespace::clean; |
|
1
|
|
|
|
|
16286
|
|
|
1
|
|
|
|
|
7
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
############################################################################### |
14
|
|
|
|
|
|
|
# Version number. |
15
|
|
|
|
|
|
|
############################################################################### |
16
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
############################################################################### |
19
|
|
|
|
|
|
|
# Subroutine: init() |
20
|
|
|
|
|
|
|
############################################################################### |
21
|
|
|
|
|
|
|
# Initializes the template plugin. |
22
|
|
|
|
|
|
|
############################################################################### |
23
|
|
|
|
|
|
|
sub init { |
24
|
10
|
|
|
10
|
1
|
36125
|
my $self = shift; |
25
|
10
|
|
|
|
|
26
|
$self->{'_DYNAMIC'} = 1; |
26
|
10
|
|
50
|
|
|
73
|
$self->install_filter( $self->{'_ARGS'}->[0] || 'linktarget' ); |
27
|
10
|
|
|
|
|
419
|
return $self; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
############################################################################### |
31
|
|
|
|
|
|
|
# Subroutine: filter($text, $args, $conf) |
32
|
|
|
|
|
|
|
############################################################################### |
33
|
|
|
|
|
|
|
# Filters the given text, and adds the "target" attribute to links. |
34
|
|
|
|
|
|
|
############################################################################### |
35
|
|
|
|
|
|
|
sub filter { |
36
|
10
|
|
|
10
|
1
|
878
|
my ($self, $text, $args, $conf) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Merge the FILTER config with the USE config |
39
|
10
|
|
|
|
|
30
|
$conf = $self->merge_config( $conf ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Get list of "excluded" things (e.g. things we DON'T add targets to) |
42
|
10
|
|
|
|
|
96
|
my @exclude; |
43
|
10
|
100
|
|
|
|
28
|
if ($conf->{'exclude'}) { |
44
|
|
|
|
|
|
|
@exclude = ref($conf->{'exclude'}) eq 'ARRAY' |
45
|
2
|
|
|
|
|
5
|
? @{$conf->{'exclude'}} |
46
|
3
|
100
|
|
|
|
12
|
: $conf->{'exclude'}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Get the "target" for links. |
50
|
10
|
|
100
|
|
|
36
|
my $target = $conf->{'target'} || '_blank'; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Create a new HTML parser. |
53
|
10
|
|
|
|
|
16
|
my $filtered = ''; |
54
|
|
|
|
|
|
|
my $p = HTML::Parser->new( |
55
|
61
|
|
|
61
|
|
231
|
'default_h' => [sub { $filtered .= shift; }, 'text'], |
56
|
|
|
|
|
|
|
'start_h' => [sub { |
57
|
16
|
|
|
16
|
|
43
|
my ($tag, $text, $attr, $attrseq) = @_; |
58
|
16
|
50
|
|
|
|
37
|
if ($tag eq 'a') { |
59
|
16
|
|
|
|
|
23
|
my $should_add = 1; |
60
|
16
|
100
|
|
|
|
35
|
if (grep { $attr->{'href'} =~ /$_/ } @exclude) { |
|
12
|
|
|
|
|
140
|
|
61
|
5
|
|
|
|
|
11
|
$should_add = 0; |
62
|
|
|
|
|
|
|
} |
63
|
16
|
100
|
|
|
|
42
|
if ($should_add) { |
64
|
|
|
|
|
|
|
# add in our "target" attr, replacing any existing one |
65
|
11
|
100
|
|
|
|
29
|
unless (exists $attr->{'target'}) { |
66
|
10
|
|
|
|
|
12
|
push( @{$attrseq}, 'target' ) |
|
10
|
|
|
|
|
21
|
|
67
|
|
|
|
|
|
|
} |
68
|
11
|
|
|
|
|
23
|
$attr->{'target'} = $target; |
69
|
|
|
|
|
|
|
# rebuild the tag |
70
|
23
|
|
|
|
|
255
|
my @attrs = map { qq{$_="} . encode_entities($attr->{$_}) . qq{"} } |
71
|
11
|
|
|
|
|
18
|
@{$attrseq}; |
|
11
|
|
|
|
|
19
|
|
72
|
11
|
|
|
|
|
181
|
$text = ''; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
16
|
|
|
|
|
79
|
$filtered .= $text; |
76
|
10
|
|
|
|
|
89
|
}, 'tag, text, attr, attrseq'], |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Filter the text. |
80
|
10
|
|
|
|
|
520
|
$p->parse( $text ); |
81
|
10
|
|
|
|
|
35
|
$p->eof(); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Return the filtered text back to the caller. |
84
|
10
|
|
|
|
|
141
|
return $filtered; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 NAME |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Template::Plugin::LinkTarget - Template Toolkit filter to add "target" attribute to all HTML links |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 SYNOPSIS |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
[% USE LinkTarget(target="_blank" exclude=['www.example.com']) %] |
96
|
|
|
|
|
|
|
... |
97
|
|
|
|
|
|
|
[% FILTER linktarget %] |
98
|
|
|
|
|
|
|
Google |
99
|
|
|
|
|
|
|
[% END %] |
100
|
|
|
|
|
|
|
... |
101
|
|
|
|
|
|
|
[% text | linktarget %] |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 DESCRIPTION |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
C is a filter plugin for C, |
106
|
|
|
|
|
|
|
which adds a C attribute to all HTML links found in the filtered text. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Through the use of the C option, you can specify URLs that are I |
109
|
|
|
|
|
|
|
given a new C attribute. This can be used to set up a filter that |
110
|
|
|
|
|
|
|
leaves internal links alone but that sets up external links to open in a new |
111
|
|
|
|
|
|
|
browser window. C accepts a list of regular expressions, so you can |
112
|
|
|
|
|
|
|
be as elaborate as you'd like. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
The C option specifies what target you'd like to give to links, |
115
|
|
|
|
|
|
|
defaulting to "_blank". |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 METHODS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=over |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item init() |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Initializes the template plugin. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item filter($text, $args, $conf) |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Filters the given text, and adds the "target" attribute to links. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=back |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 AUTHOR |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Graham TerMarsch (cpan@howlingfrog.com) |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 COPYRIGHT |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Copyright (C) 2008, Graham TerMarsch. All Rights Reserved. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the same |
140
|
|
|
|
|
|
|
terms as Perl itself. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SEE ALSO |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
L. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |