Yadis/HTMLParse.pm untested

filter: Statementstmt 0.0% 0 / 28 Branchbran n/a Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 50 CC 1 SCAR 6.9
CC
1
Cov
0%
parseMetaTags
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Net::Yadis::HTMLParse;
2  
3 use strict;
4 use Exporter;
5 our @ISA = qw(Exporter);
6 our @EXPORT_OK = qw(parseMetaTags);
7  
8 my $htmlre = qr{
9 # Starts with the tag name at a word boundary, where the tag name is
10 # not a namespace
11 <html\b(?!:)
12  
13 # All of the stuff up to a ">", hopefully attributes.
14 ([^>]*?)
15  
16 (?: # Match a short tag
17 />
18  
19 | # Match a full tag
20 >
21  
22 # contents
23 (.*?)
24  
25 # Closed by
26 (?: # One of the specified close tags
27 </?html\s*>
28  
29 # End of the string
30 | \Z
31  
32 )
33  
34 )
35 }isox;
36  
37 my $headre = qr{
38 # Starts with the tag name at a word boundary, where the tag name is
39 # not a namespace
40 <head\b(?!:)
41  
42 # All of the stuff up to a ">", hopefully attributes.
43 ([^>]*?)
44  
45 (?: # Match a short tag
46 />
47  
48 | # Match a full tag
49 >
50  
51 # match the contents of the full tag
52 (.*?)
53  
54 # Closed by
55 (?: # One of the specified close tags
56 </?(?:head|body)\s*>
57  
58 # End of the string
59 | \Z
60  
61 )
62  
63 )
64 }soxi;
65  
66 # http-equiv = $2 || $3
67 # content = $5 || $6
68 my $tagre = qr{
69 <meta\s+http-equiv=
70 (?:
71 # between matching quote marks
72 (["'])(.*?)\1
73 |
74 # or up to whitespace
75 ([^"'\s]+)
76 )
77 \s*
78 content=
79 (?:
80 # between matching quote marks
81 (["'])(.*?)\4
82 |
83 # or up to whitespace
84 ([^"'\s]+)
85 )
86 \s*
87 /?>?
88 }sixo;
89  
90  
91 my $removere = qr{
92 # Comments
93 <!--.*?-->
94  
95 # CDATA blocks
96 | <!\[CDATA\[.*?\]\]>
97  
98 # script blocks
99 | <script\b
100  
101 # make sure script is not an XML namespace
102 (?!:)
103  
104 [^>]*>.*?</script>
105 }soix;
106  
107 my %replacements = (
108 'amp' => '&',
109 'lt' => '<',
110 'gt' => '>',
111 'quot' => '"',
112 );
113
114 sub parseMetaTags {
115 my ($html) = @_;
116  
117 $html =~ s/$removere//;
118 $html =~ $htmlre or return ();
119 my $htmlcontents = $2;
120 $htmlcontents =~ $headre or return ();
121 my $head = $2;
122 defined $head or return ();
123
124 my %headerhash;
125 foreach my $tag ($head =~ /$tagre/g) {
126 my ($httpequiv,$content) = ($2 || $3, $5 || $6);
127 for my $pat (keys %replacements) {
128 $httpequiv =~ s/&$pat;/$replacements{$pat}/g;
129 $content =~ s/&$pat;/$replacements{$pat}/g;
130 }
131 $headerhash{lc($httpequiv)}=$content;
132 }
133
134 return \%headerhash;
135 }
136