lib/URI/Title/HTML.pm

filter: Statementstmt 72.5% 45 / 62 Branchbran 50.0% 14 / 28 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 56.6% 72 / 127 SCAR 38.9
CC
21
Cov
60%
title
72.5
BEGIN
1.0
BEGIN
1.0
CRAP 48.8
« blib/lib/URI/Title/MP3.pm ↑ Summary blib/lib/URI/Title/PDF.pm »
1 package URI::Title::HTML;
2 3 $URI::Title::HTML::VERSION = '1.902';
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $CAN_USE_ENCODE;
9 BEGIN {
10 3 ▶ eval { require Encode; Encode->import('decode') };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 $CAN_USE_ENCODE = !$@;
12 }
13  
14 sub types {(
15 2 ▶ 'text/html',
Subroutine types: called CC=1 SCAR=0.0
Pod undocumented
16 'default',
17 )}
18  
19 sub title {
20 1 ▶ my ($class, $url, $data, $type, $cset) = @_;
Subroutine title: called CC=21 SCAR=42.8
Pod undocumented
21  
22 1 my $title;
23 my $special_case;
24  
25 1 my $default_match = '<title.*?>(.+?)</title';
26  
27 # special case for the iTMS.
28 1 ▶ if ( $INC{'URI/Title/iTMS.pm'} and $url =~ m!phobos.apple.com! and $data =~ m!(itms://[^']*)! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"URI/Title/iTMS.pm"} and $url =~ /phobos.apple.com/ and $data =~ m[(itms://[^']*)]) 0 1 1
Condition33%2/6
$INC{"URI/Title/iTMS.pm"} and $url =~ /phobos.apple.com/ and $data =~ m[(itms://[^']*)]
!ll&&!rl&&r
100
$INC{"URI/Title/iTMS.pm"} and $url =~ /phobos.apple.com/
!ll&&!rl&&r
100
MC/DC0%0/3
$INC{"URI/Title/iTMS.pm"} and $url =~ /phobos.apple.com/ and $data =~ m[(itms://[^']*)]
$INC{"URI/Title/iTMS.pm"} $url =~ /phobos.apple.com/ $data =~ m[(itms://[^']*)]
Truth table25%1/4
A and B and C
A= $INC{"URI/Title/iTMS.pm"}B= $url =~ /phobos.apple.com/C= $data =~ m[(itms://[^']*)]
ABCresult
0XX0
10X0
1100
1111
29 0 return URI::Title::iTMS->title($1);
30 }
31  
32 # TODO - work this out from the headers of the HTML
33 1 ▶ if ($data =~ /charset=\"?([\w-]+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /charset=\"?([\w-]+)/i) 0 1 1
34 0 $cset = lc($1);
35 }
36  
37 1 ▶ if ( $CAN_USE_ENCODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($URI::Title::HTML::CAN_USE_ENCODE) 1 0 1
38 1 ▶ $data = eval { decode('utf-8', $data, 1) } || eval { decode($cset, $data, 1) } || $data;
Condition33%2/6
eval { do { &decode('utf-8', $data, 1) } } || eval { do { &decode($cset, $data, 1) } } || $data
l!l&&r!l&&!r
100
eval { do { &decode("utf-8", $data, 1) } } or eval { do { &decode($cset, $data, 1) } }
l!l&&r!l&&!r
100
MC/DC0%0/3
eval { do { &decode('utf-8', $data, 1) } } || eval { do { &decode($cset, $data, 1) } } || $data
eval { do { &decode("utf-8", $data, 1) } } eval { do { &decode($cset, $data, 1) } } $data
Truth table25%1/4
A or B || C
A= eval { do { &decode("utf-8", $data, 1) } }B= eval { do { &decode($cset, $data, 1) } }C= $data
ABCresult
1XX1
01X1
0011
0000
39 }
40  
41 1 my $found_title;
42  
43 1 ▶ if ($url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 1 0 1
44 1 ▶ if ($url =~ /use\.perl\.org\/~([^\/]+).*journal\/\d/i) {
Branch: 7 branches
BranchTrueFalseTotal
if ($url =~ m[use\.perl\.org/~([^/]+).*journal/\d]i) { } 0 1 1
elsif ($url =~ /(pants\.heddley\.com|dailychump\.org).*#(.*)$/i) { } 0 1 1
elsif ($url =~ /paste\.husk\.org/i) { } 0 1 1
elsif ($url =~ m[twitter.com/(.*?)/status(es)?/\d+]i) { } 0 1 1
elsif ($url =~ /independent\.co\.uk/i) { } 0 1 1
elsif ($url =~ m[www\.hs\.fi/english/article]i) { } 0 1 1
elsif ($url =~ /google.com/i and $data =~ /calc_img/) { } 0 2 2
45 0 $special_case = '<FONT FACE="geneva,verdana,sans-serif" SIZE="1"><B>(.+?)<';
46 0 $title = "use.perl journal of $1 - ";
47  
48 } elsif ($url =~ /(pants\.heddley\.com|dailychump\.org).*#(.*)$/i) {
49 0 my $id = $2;
50 0 $special_case = 'id="a'.$id.'.*?></a>(.+?)<';
51 0 $title = "pants daily chump - ";
52  
53 } elsif ($url =~ /paste\.husk\.org/i) {
54 0 $special_case = 'Summary: (.+?)<';
55 0 $title = "paste - ";
56  
57 } elsif ($url =~ /twitter.com\/(.*?)\/status(es)?\/\d+/i) {
58 0 $special_case = '<p class="js-tweet-text tweet-text">([^\<]+)';
59 0 $title = "twitter - ";
60  
61 } elsif ($url =~ /independent\.co\.uk/i) {
62 0 $special_case = '<h1 class=head1>(.+?)<';
63  
64 } elsif ($url =~ /www\.hs\.fi\/english\/article/i) {
65 0 $special_case = '<h1>(.+?)</h1>';
66  
67 ▶ } elsif ($url =~ /google.com/i and $data =~ /calc_img/) {
Condition33%1/3
$url =~ /google.com/i and $data =~ /calc_img/
!ll&&!rl&&r
100
MC/DC0%0/2
$url =~ /google.com/i and $data =~ /calc_img/
$url =~ /google.com/i $data =~ /calc_img/
Truth table33%1/3
A and B
A= $url =~ /google.com/iB= $data =~ /calc_img/
ABresult
0X0
100
111
68 # google can be used as a calculator. Try to find the result.
69 0 $special_case = 'calc_img.*<td nowrap>(.+?)</td';
70  
71  
72 }
73 }
74  
75 1 ▶ if (!$found_title and $special_case) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found_title and $special_case) 0 1 1
Condition33%1/3
not $found_title and $special_case
!ll&&!rl&&r
010
MC/DC0%0/2
not $found_title and $special_case
not $found_title $special_case
Truth table33%1/3
not(A) and B
A= not $found_titleB= $special_case
ABresult
0X0
100
111
76 0 ($found_title) = $data =~ /$special_case/ims;
77 }
78 1 ▶ if (!$found_title) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_title) 1 0 1
79 1 ($found_title) = $data =~ /$default_match/ims;
80 }
81 1 ▶ return unless $found_title;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_title 0 1 1
82  
83 1 $found_title =~ s/<sup>(.+?)<\/sup>/^$1/g; # for the google math output
84 1 $found_title =~ s/<.*?>//g;
85 1 $title .= $found_title;
86  
87  
88 1 $title =~ s/\s+$//;
89 1 $title =~ s/^\s+//;
90 1 $title =~ s/\n+//g;
91 1 $title =~ s/\s+/ /g;
92  
93 #use Devel::Peek;
94 #Dump( $title );
95  
96 1 $title = decode_entities($title);
97  
98 #Dump( $title );
99  
100 # decode nasty number-encoded entities. Mostly works
101 1 $title =~ s/(&\#(\d+);?)/chr($2)/eg;
102  
103 1 return $title;
104 }
105  
106 3 1;
107  
108 __END__
« blib/lib/URI/Title/MP3.pm ↑ Summary blib/lib/URI/Title/PDF.pm »