blib/lib/HTML/ExtractMain.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 95.8% 23 / 24 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 94.3% 116 / 123 CC 19 SCAR 26.5
CC
19
Cov
95%
extract_main_html
19.1
BEGIN
1.0
BEGIN
1.0
CRAP 14.2
↑ Summary
1 #!perl
2  
3 package HTML::ExtractMain;
4 4 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Object::Destroyer 2.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util qw( blessed refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our @EXPORT_OK = qw( extract_main_html );
13  
14 11 ▶sub extract_main_html {
Subroutine extract_main_html: called CC=19 SCAR=29.5
Pod documented
15 11 my $arg = shift;
16  
17 11 ▶ unless ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 1 10 11
18 1 carp 'extract_main_html requires HTML content as an argument';
19 1 return;
20 }
21  
22 10 my $tree;
23 10 ▶ if ( ref $arg and blessed $arg and $arg->isa('HTML::TreeBuilder') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg and builtin::blessed($arg) and $arg->isa('HTML::TreeBuilder')) { } 1 9 10
Condition66%4/6
ref $arg and builtin::blessed($arg) and $arg->isa("HTML::TreeBuilder")
!ll&&!rl&&r
901
ref $arg and builtin::blessed($arg)
!ll&&!rl&&r
901
MC/DC33%1/3
ref $arg and builtin::blessed($arg) and $arg->isa("HTML::TreeBuilder")
ref $arg builtin::blessed($arg) $arg->isa("HTML::TreeBuilder")
Truth table50%2/4
A and B and C
A= ref $argB= builtin::blessed($arg)C= $arg->isa("HTML::TreeBuilder")
ABCresult
0XX0
10X0
1100
1111
24 1 $tree = $arg;
25 } else {
26 9 my $raw_html = $arg;
27  
28 9 $tree = eval { HTML::TreeBuilder->new_from_content($raw_html) };
29 9 ▶ if ( !$tree ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree) 0 9 9
30 0 carp 'check HTML input, could not create new HTML::TreeBuilder';
31 0 return;
32 }
33 }
34  
35 10 my %options = @_;
36 10 ▶ if ( defined $options{output_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'output_type'}) { } 2 8 10
37 2 $options{output_type} = lc( $options{output_type} );
38 } else {
39 8 $options{output_type} = "xhtml";
40 }
41  
42 # Remove any lingering circular references. Details at:
43 # http://www.perl.com/pub/2007/06/07/better-code-through-destruction.html
44 10 my $sentry = Object::Destroyer->new( $tree, 'delete' );
45  
46 # Use the Readability algorithm, inspired by:
47 # http://lab.arc90.com/experiments/readability/js/readability.js
48  
49 # Study all the paragraphs and find the chunk that has the best score.
50 # A score is determined by things like: Number of <p>'s, commas,
51 # class names, etc.
52  
53 10 my %parents;
54 10 foreach my $p ( $tree->find_by_tag_name('p') ) {
55 194 my $parent = $p->parent;
56 194 my $parent_id = refaddr($parent);
57  
58 194 ▶ if ( !defined $parents{$parent_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parents{$parent_id}) 102 92 194
59 102 $parents{$parent_id}->{element} = $parent;
60 102 $parents{$parent_id}->{readability} = 0;
61  
62 my $text_to_scan = join q{ },
63 102 grep {defined}
64 ( $parent->attr('class'), $parent->attr('id') );
65  
66 102 ▶ if ( $text_to_scan =~ m/\b(?:comment|meta|footer|footnote)\b/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text_to_scan =~ /\b(?:comment|meta|footer|footnote)\b/) { } 72 30 102
elsif ($text_to_scan =~ /\b(post|hentry|entry[-]?(content|text|body)?|article[-]?(content|text|body)?)\b/) { } 4 26 30
67 72 $parents{$parent_id}->{readability} -= 50;
68 } elsif ( $text_to_scan
69 =~ m/\b(post|hentry|entry[-]?(content|text|body)?|article[-]?(content|text|body)?)\b/
70 ) {
71 4 $parents{$parent_id}->{readability} += 25;
72 }
73 }
74  
75 # add point for each para found
76 194 $parents{$parent_id}->{readability}++;
77  
78 # add a point for each comma found in the paragraph
79 194 foreach my $text_ref ( $p->content_refs_list ) {
80 375 my $num_commas = ( ${$text_ref} =~ m/,/g );
81 375 $parents{$parent_id}->{readability} += $num_commas;
82 }
83 }
84  
85 10 my $best_parent;
86 10 foreach my $id ( keys %parents ) {
87 102 ▶ if ( !$best_parent
Branch: 1 branch
BranchTrueFalseTotal
if (not $best_parent or $parents{$id}{"readability"} > $best_parent->{"readability"}) 21 81 102
Condition100%3/3
not $best_parent or $parents{$id}{"readability"} > $best_parent->{"readability"}
l!l&&r!l&&!r
91281
MC/DC100%2/2
not $best_parent or $parents{$id}{"readability"} > $best_parent->{"readability"}
not $best_parent $parents{$id}{"readability"} > $best_parent->{"readability"}
Truth table100%3/3
not(A) or B
A= not $best_parentB= $parents{$id}{"readability"} > $best_parent->{"readability"}
ABresult
1X1
011
000
88 || $parents{$id}->{readability} > $best_parent->{readability} ) {
89 21 $best_parent = $parents{$id};
90 }
91 }
92  
93 10 ▶ if ($best_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_parent) { } 9 1 10
94 9 my $best_parent_element = $best_parent->{element};
95 9 $best_parent_element->detach;
96  
97 9 my $output;
98 9 ▶ if ( $options{output_type} eq 'tree' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'output_type'} eq 'tree') { } 1 8 9
elsif ($options{'output_type'} eq 'html') { } 1 7 8
99 1 $output = $best_parent_element;
100 } elsif ( $options{output_type} eq 'html' ) {
101 1 $output = $best_parent_element->as_HTML;
102 } else {
103 7 $output = $best_parent_element->as_XML;
104 }
105  
106 9 ▶ unless ( $options{output_type} eq 'tree' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"output_type"} eq "tree") 8 1 9
107 8 $output =~ s{^<body>(.*)</body>\s*$}{$1}s; # kill wrapping <body>
108 8 $best_parent_element->delete;
109 }
110  
111 9 return $output;
112 } else {
113 1 return;
114 }
115 }
116  
117 =head1 NAME
118  
119 HTML::ExtractMain - Extract the main content of a web page
120  
121 =head1 VERSION
122  
123 Version 0.63
124  
125 =cut
126  
127 4 our $VERSION = '0.63';
128  
129 =head1 SYNOPSIS
130  
131 use HTML::ExtractMain qw( extract_main_html );
132  
133 my $html = <<'END';
134 <div id="header">Header</div>
135 <div id="nav"><a href="/">Home</a></div>
136 <div id="body">
137 <p>Foo</p>
138 <p>Baz</p>
139 </div>
140 <div id="footer">Footer</div>
141 END
142  
143 my $main_html = extract_main_html($html, output_type => 'xhtml');
144 if (defined $main_html) {
145 # do something with $main_html here
146 # $main_html is '<div id="body"><p>Foo</p><p>Baz</p></div>'
147 }
148  
149 =head1 EXPORT
150  
151 C<extract_main_html> is optionally exported
152  
153 =head1 FUNCTIONS
154  
155 =head2 extract_main_html
156  
157 C<extract_main_html> takes HTML content, and uses the Readability
158 algorithm to detect the main body of the page, usually skipping
159 headers, footers, navigation, etc.
160  
161 The first argument is either an HTML string, or an
162 HTML::TreeBuilder tree. (If passed a tree, the tree will be modified
163 and destroyed.)
164  
165 Remaining arguments are optional and represent key/value options. The
166 available options are:
167  
168 =head3 output_type
169  
170 This determines what format to return data in. If not specified then
171 xhtml format will be used. Valid formats are:
172  
173 =over 4
174  
175 =item C<xhtml>
176  
177 =item C<html>
178  
179 =item C<tree>
180  
181 =back
182  
183 If C<tree> is selected, then an L<HTML::Element> object will be
184 returned instead of a string.
185  
186 If the HTML's main content is found, it's returned in the chosen
187 output format. The returned HTML/XHTML will I<not> look like what you put
188 in. (Source formatting, e.g. indentation, will be removed.)
189  
190 If a most relevant block of content is not found, C<extract_main_html>
191 returns undef.
192  
193 =cut
194  
195 =head1 AUTHOR
196  
197 Anirvan Chatterjee, C<< <anirvan at cpan.org> >>
198  
199 =head1 BUGS
200  
201 Please report any bugs or feature requests to
202 C<bug-html-extractmain at rt.cpan.org>, or through the web interface
203 at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-ExtractMain>.
204 I will be notified, and then you'll automatically be notified of
205 progress on your bug as I make changes.
206  
207 =head1 SUPPORT
208  
209 You can find documentation for this module with the perldoc command.
210  
211 perldoc HTML::ExtractMain
212  
213 You can also look for information at:
214  
215 =over 4
216  
217 =item * RT: CPAN's request tracker
218  
219 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-ExtractMain>
220  
221 =item * AnnoCPAN: Annotated CPAN documentation
222  
223 L<http://annocpan.org/dist/HTML-ExtractMain>
224  
225 =item * CPAN Ratings
226  
227 L<http://cpanratings.perl.org/d/HTML-ExtractMain>
228  
229 =item * Search CPAN
230  
231 L<http://search.cpan.org/dist/HTML-ExtractMain/>
232  
233 =back
234  
235 =head1 SEE ALSO
236  
237 =over 4
238  
239 =item * C<HTML::Feature>
240  
241 =item * C<HTML::ExtractContent>
242  
243 =back
244  
245 =head1 ACKNOWLEDGEMENTS
246  
247 The Readability algorithm is ported from Arc90's JavaScript original,
248 built as part of the excellent Readability application, online at
249 L<http://lab.arc90.com/experiments/readability/>, repository at
250 L<http://code.google.com/p/arc90labs-readability/>.
251  
252 =head1 COPYRIGHT & LICENSE
253  
254 Copyright 2009-2013 Anirvan Chatterjee, Rupert Lane, kryde, all rights
255 reserved.
256  
257 This program is free software; you can redistribute it and/or modify it
258 under the same terms as Perl itself.
259  
260 =cut
261  
262 4 1; # End of HTML::ExtractMain
263  
264 # Local Variables:
265 # mode: perltidy
266 # End:
↑ Summary