ExtractInfo.pm

filter: Statementstmt 91.1% 31 / 34 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 88.8% 48 / 54 CC 9 SCAR 14.6
CC
9
Cov
89%
extract_info
7.1
_get_value
3.1
BEGIN
1.0
CRAP 4.3
« Turndown/Tables.pm ↑ Summary Turndown.pm »
1 package Text::HTML::ExtractInfo 0.12;
2 7 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use stable 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our @EXPORT_OK = (qw(extract_info));
10  
11 =head1 SYNOPSIS
12  
13 use Text::HTML::ExtractInfo 'extract_info';
14  
15 my $tree = XML::LibXML->new->parse_html_string(
16 $input,
17 { recover => 2, encoding => 'UTF-8' }
18 );
19 say Dumper extract_info($tree, url => 'https://example.com' );
20 # {
21 # title => '...',
22 # external => {
23 # image => [ 'https://example.com/img1.jpg', ... ],
24 # }
25 # }
26  
27 =cut
28  
29 7 our %elements = (
30 title => {
31 single => 1,
32 q => [
33 '//title',
34 '//meta[@property="og:title"]/@content',
35 '//meta[@property="twitter:title"]/@content',
36 '//h1[1]',
37 ],
38 },
39  
40 url => {
41 default => 'url',
42 single => 1,
43 q => [
44 '//link[@rel="canonical"]/@href',
45 '//meta[@property="og:url"]/@content',
46 '//meta[@property="twitter:url"]/@content',
47 ],
48 },
49  
50 image => {
51 q => [
52 '//meta[@property="og:image"]/@content',
53 '//meta[@property="og:image:url"]/@content',
54 '//meta[@property="og:image:secure_url"]/@content',
55 '//meta[@property="twitter:image"]/@content',
56 ],
57 },
58  
59 authors =>{
60 q => [
61 ],
62 },
63 );
64  
65 8 ▶sub _get_value( $node ) {
Subroutine _get_value: called CC=3 SCAR=11.4
66 8 ▶ if( my $c = $node->can( 'value' )) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $c = $node->can('value')) { } 4 4 8
elsif ($c = $node->can('textContent')) { } 4 0 4
67 4 return $c->($node)
68 } elsif( $c = $node->can( 'textContent' )) {
69 4 return $c->($node)
70 } else {
71 0 croak "Don't know how to handle " . $node->toString
72 }
73 }
74  
75 10 ▶sub extract_info( $tree, %options ) {
Subroutine extract_info: called CC=7 SCAR=19.7
Pod undocumented
76 10 my %res;
77  
78 10 for my $k (keys( %elements )) {
79 40 ▶ if( my $d = $elements{ $k }->{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $Text::HTML::ExtractInfo::elements{$k}{"default"}) 10 30 40
80 $res{ $k } = $options{ $d }
81 10 ▶ if exists $options{ $d };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$d} 2 8 10
82 }
83 40 for my $q ( $elements{ $k }->{q}->@* ) {
84 97 my @nodes = $tree->findnodes($q);
85 97 ▶ if( @nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes) 8 89 97
86 8 ▶ if( $elements{ $k }->{single} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::HTML::ExtractInfo::elements{$k}{'single'}) { } 8 0 8
87 8 $res{ $k } = _get_value( $nodes[0] )
88 } else {
89 0 $res{ $k } = [ map { _get_value( $_ )} @nodes ];
90 }
91 last
92 8 }
93 }
94 }
95  
96 10 return \%res,
97 }
98  
99 7 1;
100  
101 =head1 SEE ALSO
102  
103 L<HTML::ExtractMeta> - extract information from C<< <META >> tags
104  
105 =cut
« Turndown/Tables.pm ↑ Summary Turndown.pm »