blib/lib/HTML/FormatText/WithLinks.pm

filter: Statementstmt 98.1% 155 / 158 Branchbran 90.0% 54 / 60 Conditioncond 86.8% 33 / 38 MC/DCmcdc 72.7% 16 / 22 Subroutinesub 100% 24 / 24 Podpod 25.0% 4 / 16 totaltotal 89.9% 286 / 318 CC 46 SCAR 15.5
CC
46
Cov
95%
a_start
12.0
text
5.0
html_end
5.0
CRAP 4.7
↑ Summary
1 package HTML::FormatText::WithLinks;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use URI::WithBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use base qw(HTML::FormatText);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 $VERSION = '0.15';
10  
11 33 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
12  
13 33 my $proto = shift;
14 33 ▶ my $class = ref( $proto ) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1320
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
15 33 my $self = $class->SUPER::new( @_ );
16 33 ▶ $self->configure() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 5 28 33
17  
18 33 bless ( $self, $class );
19 33 return $self;
20  
21 }
22  
23 33 ▶sub configure {
Subroutine configure: called CC=4 SCAR=13.9
Pod undocumented
24  
25 33 my ($self, $hash) = @_;
26  
27 # a base uri so we can resolve relative uris
28 33 $self->{base} = $hash->{base};
29 33 delete $hash->{base};
30 33 ▶ $self->{base} =~ s#(.*?)/[^/]*$#$1/# if $self->{base};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"base"} 5 28 33
31  
32 33 $self->{doc_overrides_base} = $hash->{doc_overrides_base};
33 33 delete $hash->{doc_overrides_base};
34  
35 33 $self->{before_link} = '[%n]';
36 33 $self->{after_link} = '';
37 33 $self->{footnote} = '%n. %l';
38 33 ▶ $self->{link_num_generator} = sub { return shift() + 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 33 $self->{unique_links} = 0;
41  
42 33 $self->{anchor_links} = 1;
43  
44 33 $self->{skip_linked_urls} = 0;
45  
46 33 $self->{_link_track} = {};
47  
48 33 $self->{bold_marker} = '_';
49 33 $self->{italic_marker} = '/';
50  
51 33 foreach ( qw( before_link after_link footnote link_num_generator
52 with_emphasis bold_marker italic_marker
53 unique_links anchor_links skip_linked_urls ) ) {
54 330 ▶ $self->{ $_ } = $hash->{ $_ } if exists $hash->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 21 309 330
55 330 delete $hash->{ $_ };
56 }
57  
58 33 $self->SUPER::configure($hash);
59  
60 }
61  
62 # we need to do this as if you pass an HTML fragment without any
63 # containing block level markup (e.g. a p tag) then no indentation
64 # takes place so if we've not got a cur_pos we indent.
65 120 ▶sub textflow {
Subroutine textflow: called CC=2 SCAR=7.1
Pod undocumented
66 120 my $self = shift;
67 120 ▶ $self->goto_lm unless defined $self->{cur_pos};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"cur_pos"} 120 0 120
68 120 $self->SUPER::textflow(@_);
69 }
70  
71 29 ▶sub head_start {
Subroutine head_start: called CC=3 SCAR=11.0
Pod undocumented
72 29 my ($self) = @_;
73 29 $self->SUPER::head_start();
74  
75 # we don't care about what the documents says it's base is
76 29 ▶ if ( $self->{base} and not $self->{doc_overrides_base} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"base"} and not $self->{"doc_overrides_base"}) 4 25 29
Condition100%3/3
$self->{"base"} and not $self->{"doc_overrides_base"}
!ll&&!rl&&r
2414
MC/DC100%2/2
$self->{"base"} and not $self->{"doc_overrides_base"}
$self->{"base"} not $self->{"doc_overrides_base"}
Truth table100%3/3
A and not(B)
A= $self->{"base"}B= not $self->{"doc_overrides_base"}
ABresult
0X0
100
111
77 4 return 0;
78 }
79  
80 # descend into <head> for possible <base> there, even if superclass not
81 # interested (as of HTML::FormatText 2.04 it's not)
82 25 return 1;
83 }
84  
85 # <base> is supposed to be inside <head>, but no need to demand that.
86 # "lynx -source" sticks a <base> at the very start of the document, before
87 # even <html>, so accepting <base> anywhere lets that work.
88 2 ▶sub base_start {
Subroutine base_start: called CC=3 SCAR=11.8
Pod undocumented
89 2 my ($self, $node) = @_;
90 2 ▶ if (my $href = $node->attr('href')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $href = $node->attr("href")) 2 0 2
91 2 $self->{base} = $href;
92 }
93  
94 # allow for no superclass base_start() in HTML::FormatText 2.04
95 2 ▶ if (! HTML::FormatText->can('base_start')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("HTML::FormatText"->can("base_start")) 2 0 2
96 2 return 0;
97 }
98  
99 # chain up if it exists in the future
100 0 return $self->SUPER::base_start();
101 }
102  
103 34 ▶sub a_start {
Subroutine a_start: called CC=12 SCAR=24.9
Pod undocumented
104  
105 34 my $self = shift;
106 34 my $node = shift;
107 # local urls are no use so we have to make them absolute
108 34 ▶ my $href = $node->attr('href') || '';
Condition100%2/2
$node->attr('href') || ''
l!l
304
MC/DC100%1/1
$node->attr('href') || ''
$node->attr('href')
Truth table100%2/2
A
A= $node->attr('href')
Aresult
11
00
109 34 ▶ if ($href && $self->{anchor_links} == 0 && $href =~ m/^#/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($href and $self->{'anchor_links'} == 0 and $href =~ /^#/o) { } 1 33 34
elsif ($href and $self->{'skip_linked_urls'} and $href eq $node->as_text) { } 3 58 61
Condition83%5/6
$href and $self->{"anchor_links"} == 0 and $href =~ /^#/o
!ll&&!rl&&r
3301
$href and $self->{"anchor_links"} == 0
!ll&&!rl&&r
4291
MC/DC66%2/3
$href and $self->{"anchor_links"} == 0 and $href =~ /^#/o
$href $self->{"anchor_links"} == 0 $href =~ /^#/o
Truth table75%3/4
A and B and C
A= $hrefB= $self->{"anchor_links"} == 0C= $href =~ /^#/o
ABCresult
0XX0
10X0
1100
1111
110 1 $href = '';
111 }
112 ▶ elsif ($href and $self->{skip_linked_urls} and $href eq $node->as_text) {
Condition100%6/6
$href and $self->{"skip_linked_urls"} and $href eq $node->as_text
!ll&&!rl&&r
2823
$href and $self->{"skip_linked_urls"}
!ll&&!rl&&r
4245
MC/DC100%3/3
$href and $self->{"skip_linked_urls"} and $href eq $node->as_text
$href $self->{"skip_linked_urls"} $href eq $node->as_text
Truth table100%4/4
A and B and C
A= $hrefB= $self->{"skip_linked_urls"}C= $href eq $node->as_text
ABCresult
0XX0
10X0
1100
1111
113 3 $href = '';
114 }
115 34 ▶ if ( $href ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href) 26 8 34
116 26 ▶ if ($href !~ m#^https?:|^mailto:#o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($href =~ /^https?:|^mailto:/o) 5 21 26
117 5 $href = URI::WithBase->new($href, $self->{base})->abs();
118 }
119 26 ▶ if ($self->{unique_links})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'unique_links'}) { } 8 18 26
120 {
121 8 ▶ if (defined $self->{_link_track}->{$href})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_link_track'}{$href}) { } 3 5 8
122 {
123 3 $self->out( $self->text('before_link', $self->{_link_track}->{$href}, $href ) );
124 } else {
125 5 push @{$self->{_links}}, $href;
126 5 $self->{_link_track}->{$href} = $#{$self->{_links}};
127 5 $self->out( $self->text('before_link', $#{$self->{_links}}, $href ) );
128 }
129 } else {
130 18 push @{$self->{_links}}, $href;
131 18 $self->out( $self->text('before_link') );
132 }
133 }
134 34 $self->SUPER::a_start();
135  
136 }
137  
138 34 ▶sub a_end {
Subroutine a_end: called CC=5 SCAR=16.1
Pod undocumented
139  
140 34 my $self = shift;
141 34 my $node = shift;
142 34 my $text;
143 34 ▶ unless ($self->{skip_linked_urls} and $node->attr('href') eq $node->as_text) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"skip_linked_urls"} and $node->attr("href") eq $node->as_text) 31 3 34
Condition100%3/3
$self->{"skip_linked_urls"} and $node->attr("href") eq $node->as_text
!ll&&!rl&&r
2923
MC/DC100%2/2
$self->{"skip_linked_urls"} and $node->attr("href") eq $node->as_text
$self->{"skip_linked_urls"} $node->attr("href") eq $node->as_text
Truth table100%3/3
A and B
A= $self->{"skip_linked_urls"}B= $node->attr("href") eq $node->as_text
ABresult
0X0
100
111
144 31 ▶ if ($self->{unique_links})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'unique_links'}) { } 8 23 31
145 {
146 8 my $href = $node->attr('href');
147 8 $text = $self->text('after_link', $self->{_link_track}->{$href}, $href);
148 } else {
149 23 $text = $self->text('after_link');
150 }
151 # If we're just dealing with a fragment of HTML, with a link at the
152 # end, we get a space before the first footnote link if we do
153 # $self->out( '' )
154 31 ▶ if ($text ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "") 6 25 31
155 6 $self->out( $text );
156 }
157 }
158 34 $self->SUPER::a_end();
159  
160 }
161  
162 6 ▶sub b_start {
Subroutine b_start: called CC=2 SCAR=6.9
Pod undocumented
163 6 my $self = shift;
164 6 ▶ $self->out( $self->{'bold_marker'} ) if $self->{ with_emphasis };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"with_emphasis"} 4 2 6
165 6 $self->SUPER::b_start();
166 }
167  
168 6 ▶sub b_end {
Subroutine b_end: called CC=2 SCAR=6.9
Pod undocumented
169 6 my $self = shift;
170 6 ▶ $self->out( $self->{'bold_marker'} ) if $self->{ with_emphasis };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"with_emphasis"} 4 2 6
171 6 $self->SUPER::b_end();
172 }
173  
174 6 ▶sub i_start {
Subroutine i_start: called CC=2 SCAR=6.9
Pod undocumented
175 6 my $self = shift;
176 6 ▶ $self->out( $self->{'italic_marker'} ) if $self->{ with_emphasis };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"with_emphasis"} 4 2 6
177 6 $self->SUPER::i_start();
178 }
179  
180 6 ▶sub i_end {
Subroutine i_end: called CC=2 SCAR=6.9
Pod undocumented
181 6 my $self = shift;
182 6 ▶ $self->out( $self->{'italic_marker'} ) if $self->{ with_emphasis };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"with_emphasis"} 4 2 6
183 6 $self->SUPER::i_end();
184 }
185  
186 # print out our links
187 29 ▶sub html_end {
Subroutine html_end: called CC=5 SCAR=16.1
Pod undocumented
188  
189 29 my $self = shift;
190 29 ▶ if ( $self->{_links} and @{$self->{_links}} and $self->{footnote} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_links"} and @{$self->{"_links"};} and $self->{"footnote"}) 16 13 29
Condition83%5/6
$self->{"_links"} and @{$self->{"_links"};} and $self->{"footnote"}
!ll&&!rl&&r
11216
$self->{"_links"} and @{$self->{"_links"};}
!ll&&!rl&&r
01118
MC/DC66%2/3
$self->{"_links"} and @{$self->{"_links"};} and $self->{"footnote"}
$self->{"_links"} @{$self->{"_links"};} $self->{"footnote"}
Truth table75%3/4
A and B and C
A= $self->{"_links"}B= @{$self->{"_links"};}C= $self->{"footnote"}
ABCresult
0XX0
10X0
1100
1111
191 16 $self->nl; $self->nl; # be tidy
192 16 $self->goto_lm;
193 16 for (0 .. $#{$self->{_links}}) {
194 20 $self->goto_lm;
195 $self->out(
196 20 $self->text( 'footnote', $_, $self->{_links}->[$_] )
197 );
198 20 $self->nl;
199 }
200 }
201 29 $self->SUPER::end();
202  
203 }
204  
205 77 ▶sub _link_num {
Subroutine _link_num: called CC=2 SCAR=6.9
206  
207 77 my ($self, $num) = @_;
208 77 ▶ $num = $#{$self->{_links}} unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 41 36 77
209 77 return &{$self->{link_num_generator}}($num);
210  
211 }
212  
213 77 ▶sub text {
Subroutine text: called CC=5 SCAR=16.1
Pod undocumented
214  
215 77 my ($self, $type, $num, $href) = @_;
216  
217 77 ▶ if ($self->{_links} and @{$self->{_links}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_links"} and @{$self->{"_links"};}) 72 5 77
Condition66%2/3
$self->{"_links"} and @{$self->{"_links"};}
!ll&&!rl&&r
0572
MC/DC50%1/2
$self->{"_links"} and @{$self->{"_links"};}
$self->{"_links"} @{$self->{"_links"};}
Truth table66%2/3
A and B
A= $self->{"_links"}B= @{$self->{"_links"};}
ABresult
0X0
100
111
218 72 ▶ $href = $self->{_links}->[$#{$self->{_links}}]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num and defined $href 36 36 72
Condition66%2/3
defined $num and defined $href
!ll&&!rl&&r
36036
MC/DC50%1/2
defined $num and defined $href
defined $num defined $href
Truth table66%2/3
A and B
A= defined $numB= defined $href
ABresult
0X0
100
111
219 unless (defined $num and defined $href);
220 }
221 77 $num = $self->_link_num($num);
222 77 my $text = $self->{$type};
223 77 $text =~ s/%n/$num/g;
224 77 $text =~ s/%l/$href/g;
225 77 return $text;
226 }
227  
228 30 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
229  
230 30 my $self = shift;
231 30 my $text = shift;
232  
233 30 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 29 30
234 29 ▶ return '' if $text eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "" 1 28 29
235  
236 28 my $tree = HTML::TreeBuilder->new->parse( $text );
237  
238 28 return $self->_parse( $tree );
239 }
240  
241 3 ▶sub parse_file {
Subroutine parse_file: called CC=3 SCAR=11.0
Pod documented
242  
243 3 my $self = shift;
244 3 my $file = shift;
245  
246 3 ▶ unless (-e $file and -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file and -f $file) 2 1 3
Condition100%3/3
-e $file and -f $file
!ll&&!rl&&r
111
MC/DC100%2/2
-e $file and -f $file
-e $file -f $file
Truth table100%3/3
A and B
A= -e $fileB= -f $file
ABresult
0X0
100
111
247 2 $self->error("$file not found or not a regular file");
248 2 return undef;
249 }
250  
251 1 my $tree = HTML::TreeBuilder->new->parse_file( $file );
252
253 1 return $self->_parse( $tree );
254 }
255  
256 29 ▶sub _parse {
Subroutine _parse: called CC=3 SCAR=12.0
257  
258 29 my $self = shift;
259 29 my $tree = shift;
260  
261 29 $self->{_link_track} = {};
262 29 $self->{_links} = [];
263  
264 29 ▶ unless ( $tree ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree) 0 29 29
265 0 ▶ $self->error( "HTML::TreeBuilder problem" . ( $! ? ": $!" : '' ) );
Branch: 1 branch
BranchTrueFalseTotal
$! ? : 0 0 0
266 0 return undef;
267 }
268 29 $tree->eof();
269  
270 29 my $return_text = $self->format( $tree );
271  
272 29 $tree->delete;
273  
274 29 return $return_text;
275 }
276
277  
278 4 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod documented
279 4 my $self = shift;
280 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
281 2 $self->{error} = shift;
282 }
283 4 return $self->{error};
284 }
285  
286 18 1;
287  
288 __END__
↑ Summary