blib/lib/URI/Title/HTML.pm untested

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