blib/lib/HTML/ExtractMeta.pm

filter: Statementstmt 98.9% 92 / 93 Branchbran 80.0% 8 / 10 Conditioncond 72.2% 26 / 36 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 18 / 18 Podpod 90.9% 10 / 11 totaltotal 87.1% 162 / 186 CC 29 SCAR 13.3
CC
29
Cov
91%
__ANON__
8.0
_get
5.0
keywords
5.0
CRAP 3.8
↑ Summary
1 package HTML::ExtractMeta;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 HTML::ExtractMeta - Helper class for extracting useful meta data from HTML pages.
10  
11 =head1 VERSION
12  
13 Version 0.21
14  
15 =cut
16  
17 2 our $VERSION = '0.21';
18  
19 =head1 SYNOPSIS
20  
21 use HTML::ExtractMeta;
22  
23 my $em = HTML::ExtractMeta->new( $html );
24  
25 print "Title = " . $em->title . "\n";
26 print "Description = " . $em->description . "\n";
27 print "Author = " . $em->author . "\n";
28 print "URL = " . $em->url . "\n";
29 print "Site name = " . $em->site_name . "\n";
30 print "Type = " . $em->type . "\n";
31 print "Locale = " . $em->locale . "\n";
32 print "Image URL = " . $em->image_url . "\n";
33 print "Authors = " . join( ', ', @{$em->authors} ) . "\n";
34 print "Keywords = " . join( ', ', @{$em->keywords} ) . "\n";
35  
36 =head1 DESCRIPTION
37  
38 HTML::ExtractMeta is a helper class for extracting useful metadata from HTML
39 pages, like their title, description, authors etc.
40  
41 =head1 METHODS
42  
43 =head2 new( %opts )
44  
45 Returns a new HTML::ExtractMeta instance. Requires HTML as input argument;
46  
47 my $em = HTML::ExtractMeta->new( $html );
48  
49 =cut
50  
51 2 has 'html' => (
52 isa => 'Str',
53 is => 'rw',
54 required => 1,
55 default => '',
56 );
57  
58 6 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
59 6 my $orig = shift;
60 6 my $class = shift;
61  
62 6 ▶ if ( @_ == 1 && !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 0 6 6
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
600
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
63 0 return $class->$orig( html => $_[0] );
64 }
65 else {
66 6 return $class->$orig( @_ );
67 }
68 2 };
69  
70 has '_dom' => (
71 isa => 'Mojo::DOM',
72 is => 'ro',
73 lazy => 1,
74 6 ▶ default => sub { Mojo::DOM->new(shift->html) },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 2 );
76  
77 has '_meta' => (
78 isa => 'ArrayRef[HashRef]',
79 is => 'ro',
80 lazy => 1,
81 6 ▶ default => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
82 6 my $self = shift;
83  
84 # my %meta = ();
85 6 my @meta = ();
86  
87 6 foreach ( $self->_dom->find('meta')->each ) {
88 153 ▶ my $name = $_->attr( 'name' ) // $_->attr( 'property' ) // $_->attr( 'itemprop' ) // '';
Condition100%8/8
(($_->attr("name") // $_->attr("property")) // $_->attr("itemprop")) // ""
l!l
1449
($_->attr("name") // $_->attr("property")) // $_->attr("itemprop")
l!l&&r!l&&!r
14139
$_->attr("name") // $_->attr("property")
l!l&&r!l&&!r
875412
MC/DC100%3/3
(($_->attr("name") // $_->attr("property")) // $_->attr("itemprop")) // ""
$_->attr("name") $_->attr("property") $_->attr("itemprop")
Truth table100%4/4
A // B // C
A= $_->attr("name")B= $_->attr("property")C= $_->attr("itemprop")
ABCresult
1XX1
01X1
0011
0000
89 153 ▶ my $content = $_->attr( 'content' ) // '';
Condition100%2/2
$_->attr("content") // ""
l!l
1503
MC/DC100%1/1
$_->attr("content") // ""
$_->attr("content")
Truth table100%2/2
A
A= $_->attr("content")
Aresult
11
00
90  
91 153 ▶ if ( length $name && length $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name and length $content) 138 15 153
Condition100%3/3
length $name and length $content
!ll&&!rl&&r
96138
MC/DC100%2/2
length $name and length $content
length $name length $content
Truth table100%3/3
A and B
A= length $nameB= length $content
ABresult
0X0
100
111
92 # $meta{ $name } = squish( $content );
93 138 push( @meta, { $name => squish($content) } );
94 }
95 }
96  
97 # return \%meta;
98 6 return \@meta;
99 },
100 2 );
101  
102 80 ▶sub _get {
Subroutine _get: called CC=5 SCAR=16.1
103 80 my $self = shift;
104 80 ▶ my $what = shift || [];
Condition50%1/2
shift() || []
l!l
800
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
105  
106 80 my @data = ();
107  
108 80 foreach my $w ( @{$what} ) {
109 141 foreach my $m ( @{$self->_meta} ) {
110 3268 ▶ if ( my $d = $m->{$w} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $m->{$w}) 67 3201 3268
111 67 push( @data, $d );
112 }
113 }
114 }
115  
116 80 return \@data;
117 }
118  
119 =head2 title
120  
121 Returns the HTML page's title.
122  
123 =cut
124  
125 6 ▶sub title {
Subroutine title: called CC=2 SCAR=6.9
Pod documented
126 6 my $self = shift;
127  
128 6 my @ids = (
129 'og:title',
130 'twitter:title',
131 );
132  
133 6 ▶ my $title = $self->_get( \@ids )->[0] // '';
Condition50%1/2
$self->_get(\@ids)->[0] // ""
l!l
60
MC/DC0%0/1
$self->_get(\@ids)->[0] // ""
$self->_get(\@ids)->[0]
Truth table50%1/2
A
A= $self->_get(\@ids)->[0]
Aresult
11
00
134  
135 6 $title =~ s/\s*\|.+//;
136  
137 6 $title =~ s/^\w+\.\w+\s+\-\s+//;
138 6 $title =~ s/^\w+\.\w+\s+\–\s+//;
139  
140 6 $title =~ s/\s+\-\s+[[:upper:]].+//;
141 6 $title =~ s/\s+\–\s+[[:upper:]].+//;
142  
143 6 return squish( $title );
144 }
145  
146 =head2 description
147  
148 Returns the HTML page's description.
149  
150 =cut
151  
152 6 ▶sub description {
Subroutine description: called CC=2 SCAR=7.1
Pod documented
153 6 my $self = shift;
154  
155 6 my @ids = (
156 'og:description',
157 'twitter:description',
158 'description',
159 'Description',
160 );
161  
162 6 ▶ return $self->_get( \@ids )->[0] // '';
Condition50%1/2
$self->_get(\@ids)->[0] // ""
l!l
60
MC/DC0%0/1
$self->_get(\@ids)->[0] // ""
$self->_get(\@ids)->[0]
Truth table50%1/2
A
A= $self->_get(\@ids)->[0]
Aresult
11
00
163 }
164  
165 =head2 url
166  
167 Returns the HTML page's URL.
168  
169 =cut
170  
171 6 ▶sub url {
Subroutine url: called CC=2 SCAR=6.9
Pod documented
172 6 my $self = shift;
173  
174 6 my @ids = (
175 'og:url',
176 'twitter:url',
177 );
178  
179 6 ▶ return $self->_get( \@ids )->[0] // '';
Condition100%2/2
$self->_get(\@ids)->[0] // ""
l!l
51
MC/DC100%1/1
$self->_get(\@ids)->[0] // ""
$self->_get(\@ids)->[0]
Truth table100%2/2
A
A= $self->_get(\@ids)->[0]
Aresult
11
00
180 }
181  
182 =head2 image_url
183  
184 Returns the HTML page's descriptive image URL.
185  
186 =cut
187  
188 5 ▶sub image_url {
Subroutine image_url: called CC=2 SCAR=7.1
Pod documented
189 5 my $self = shift;
190  
191 5 my @ids = (
192 'og:image',
193 'og:image:url',
194 'og:image:secure_url',
195 'twitter:image',
196 );
197  
198 5 ▶ return $self->_get( \@ids )->[0] // '';
Condition50%1/2
$self->_get(\@ids)->[0] // ""
l!l
50
MC/DC0%0/1
$self->_get(\@ids)->[0] // ""
$self->_get(\@ids)->[0]
Truth table50%1/2
A
A= $self->_get(\@ids)->[0]
Aresult
11
00
199 }
200  
201 =head2 site_name
202  
203 Returns the HTML page's site name.
204  
205 =cut
206  
207 5 ▶sub site_name {
Subroutine site_name: called CC=2 SCAR=7.1
Pod documented
208 5 my $self = shift;
209  
210 5 my @ids = (
211 'og:site_name',
212 'application-name',
213 'twitter:site',
214 );
215  
216 5 ▶ return $self->_get( \@ids )->[0] // '';
Condition50%1/2
$self->_get(\@ids)->[0] // ""
l!l
50
MC/DC0%0/1
$self->_get(\@ids)->[0] // ""
$self->_get(\@ids)->[0]
Truth table50%1/2
A
A= $self->_get(\@ids)->[0]
Aresult
11
00
217 }
218  
219 =head2 type
220  
221 Returns the HTML page's type.
222  
223 =cut
224  
225 6 ▶sub type {
Subroutine type: called CC=2 SCAR=7.1
Pod documented
226 6 my $self = shift;
227  
228 6 my @ids = (
229 'og:type',
230 );
231  
232 6 ▶ return $self->_get( \@ids )->[0] // '';
Condition50%1/2
$self->_get(\@ids)->[0] // ""
l!l
60
MC/DC0%0/1
$self->_get(\@ids)->[0] // ""
$self->_get(\@ids)->[0]
Truth table50%1/2
A
A= $self->_get(\@ids)->[0]
Aresult
11
00
233 }
234  
235 =head2 locale
236  
237 Returns the HTML page's locale.
238  
239 =cut
240  
241 3 ▶sub locale {
Subroutine locale: called CC=2 SCAR=7.1
Pod documented
242 3 my $self = shift;
243  
244 3 my @ids = (
245 'og:locale',
246 'inLanguage',
247 'Content-Language',
248 );
249  
250 3 ▶ return $self->_get( \@ids )->[0] // '';
Condition50%1/2
$self->_get(\@ids)->[0] // ""
l!l
30
MC/DC0%0/1
$self->_get(\@ids)->[0] // ""
$self->_get(\@ids)->[0]
Truth table50%1/2
A
A= $self->_get(\@ids)->[0]
Aresult
11
00
251 }
252  
253 =head2 authors
254  
255 Returns the HTML page's author names as an array reference.
256  
257 =cut
258  
259 8 ▶sub authors {
Subroutine authors: called CC=3 SCAR=11.0
Pod documented
260 8 my $self = shift;
261  
262 8 my @ids = (
263 'article:author',
264 'author',
265 'Author',
266 'twitter:creator',
267 'DC.creator',
268 );
269  
270 8 my @authors = ();
271  
272 8 foreach my $id ( @ids ) {
273 40 foreach ( @{$self->_get([$id])} ) {
274 13 push( @authors, $_ );
275 }
276 }
277  
278 8 return \@authors;
279 }
280  
281 =head2 author
282  
283 Helper method; returns the HTML page's first mentioned author. Basically the
284 same as:
285  
286 my $author = $em->authors->[0];
287  
288 =cut
289  
290 5 ▶sub author {
Subroutine author: called CC=2 SCAR=6.9
Pod documented
291 5 my $self = shift;
292  
293 5 ▶ return $self->authors->[0] // '';
Condition100%2/2
$self->authors->[0] // ""
l!l
41
MC/DC100%1/1
$self->authors->[0] // ""
$self->authors->[0]
Truth table100%2/2
A
A= $self->authors->[0]
Aresult
11
00
294 }
295  
296 =head2 keywords
297  
298 Returns the HTML page's keywords.
299  
300 =cut
301  
302 3 ▶sub keywords {
Subroutine keywords: called CC=5 SCAR=16.1
Pod documented
303 3 my $self = shift;
304  
305 3 my @ids = (
306 'keywords',
307 );
308  
309 3 my @keywords = ();
310 3 my %seen = ();
311  
312 3 foreach my $id ( @ids ) {
313 3 ▶ if ( my $keywords = $self->_get([$id])->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $keywords = $self->_get([$id])->[0]) 1 2 3
314 1 foreach my $keyword ( split(/\s*,\s*/, $keywords) ) {
315 15 ▶ unless ( $seen{$keyword} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$keyword}) 15 0 15
316 15 push( @keywords, $keyword );
317 15 $seen{ $keyword }++;
318 }
319 }
320 }
321 }
322  
323 3 return \@keywords;
324 }
325  
326 144 ▶sub squish {
Subroutine squish: called CC=2 SCAR=7.0
Pod undocumented
327 144 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
1440
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
328  
329 144 $str =~ s/\s+/ /sg;
330 144 $str =~ s/^\s+//;
331 144 $str =~ s/\s+$//;
332  
333 144 return $str;
334 }
335  
336 2 __PACKAGE__->meta->make_immutable;
337  
338 2 1;
339  
340 =head1 AUTHOR
341  
342 Tore Aursand, C<< <toreau at gmail.com> >>
343  
344 =head1 BUGS
345  
346 Please report any bugs or feature requests to the web interface at L<https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-ExtractMeta>.
347  
348 =head1 SUPPORT
349  
350 You can find documentation for this module with the perldoc command.
351  
352 perldoc HTML::ExtractMeta
353  
354 You can also look for information at:
355  
356 =over 4
357  
358 =item * AnnoCPAN: Annotated CPAN documentation
359  
360 L<http://annocpan.org/dist/HTML-ExtractMeta>
361  
362 =item * CPAN Ratings
363  
364 L<http://cpanratings.perl.org/d/HTML-ExtractMeta>
365  
366 =item * Search CPAN
367  
368 L<http://search.cpan.org/dist/HTML-ExtractMeta/>
369  
370 =back
371  
372 =head1 LICENSE AND COPYRIGHT
373  
374 Copyright 2012-2016 Tore Aursand.
375  
376 This program is free software; you can redistribute it and/or modify it
377 under the terms of the the Artistic License (2.0). You may obtain a
378 copy of the full license at:
379  
380 L<http://www.perlfoundation.org/artistic_license_2_0>
381  
382 Any use, modification, and distribution of the Standard or Modified
383 Versions is governed by this Artistic License. By using, modifying or
384 distributing the Package, you accept this license. Do not use, modify,
385 or distribute the Package, if you do not accept this license.
386  
387 If your Modified Version has been derived from a Modified Version made
388 by someone other than you, you are nevertheless required to ensure that
389 your Modified Version complies with the requirements of this license.
390  
391 This license does not grant you the right to use any trademark, service
392 mark, tradename, or logo of the Copyright Holder.
393  
394 This license includes the non-exclusive, worldwide, free-of-charge
395 patent license to make, have made, use, offer to sell, sell, import and
396 otherwise transfer the Package with respect to any patent claims
397 licensable by the Copyright Holder that are necessarily infringed by the
398 Package. If you institute patent litigation (including a cross-claim or
399 counterclaim) against any party alleging that the Package constitutes
400 direct or contributory patent infringement, then this Artistic License
401 to you shall terminate on the date that such litigation is filed.
402  
403 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
404 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
405 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
406 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
407 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
408 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
409 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
410 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
411  
412 =cut
↑ Summary