lib/HTML/SummaryBasic.pm

filter: Statementstmt 83.5% 71 / 85 Branchbran 60.5% 23 / 38 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 72.6% 106 / 146 CC 26 SCAR 27.6
CC
26
Cov
74%
_get_summary
24.5
_load_file
7.0
new
5.2
CRAP 15.8
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 package HTML::SummaryBasic;
5
6 1 our $VERSION = 0.2;
7
8 =head1 NAME
9
10 HTML::SummaryBasic - Basic summary info from HTML.
11
12 =head1 SYNOPSIS
13
14 use HTML::SummaryBasic;
15 my $p = new HTML::SummaryBasic {
16 PATH => "input.html",
17 # or HTML => '<html>...</html>',
18 NOT_AVAILABLE => undef,
19 };
20 foreach (keys %{$p->{SUMMARY}}){
21 warn "$_ ... $p->{SUMMARY}->{$_}\n";
22 }
23
24 =head1 DEPENDENCIES
25
26 use HTML::TokeParser;
27 use HTML::HeadParser;
28
29 =cut
30
31 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
33
34 =head1 DESCRIPTION
35
36 From a file or string of HTML, creates a hash of useful summary information from C<meta> and C<body> elements of an HTML document.
37
38 =head1 GLOBAL VARIABLE
39
40 =item $NOT_AVAILABLE
41
42 Value for empty fields. Default is C<[Not Available]>. May be over-ridden directly by supplying the constructor with a field of the same name.
43 See L<THE SUMMARY STRUCTURE>.
44
45 =cut
46
47 1 our $NOT_AVAILABLE = '[Not available]';
48
49 =head1 CONSTRUCTOR (new)
50
51 Accepts a hash-like structure...
52
53 =over 4
54
55 =item HTML or PATH
56
57 Ref to a scalar of HTML, or plain string that is the path to an HTML file to process.
58
59 =item SUMMARY
60
61 Filled after C<get_summary> is called (see L<METHOD get_summary> and
62 L<THE SUMMARY STRUCTURE>).
63
64 =item FIELDS
65
66 An array of C<meta> tag C<name>s whose C<content> value should be
67 placed into the respective slots of the C<SUMMARY> field after
68 C<get_summary> has been called.
69
70 =back
71
72 =head2 THE SUMMARY STRUCTURE
73
74 A field of the object which is a hash, with key/values as follows:
75
76 =over 4
77
78 =item AUTHOR
79
80 HTML C<meta> tag C<X-META-AUTHOR>.
81
82 =item TITLE
83
84 Text of the element of the same name.
85
86 =item DESCRIPTION
87
88 Content of the C<meta> tag named C<X-META-DESCRIPTION>.
89
90 =item LAST_MODIFIED_META, LAST_MODIFIED_FILE
91
92 Time since of the modification of the file,
93 respectively according to any C<meta> tag of the same name,
94 with a C<X-META-> prefix; failing that, according to the file system.
95
96 =item CREATED_META, CREATED_FILE
97
98 As above, but relating to the creation date of the file.
99
100 =item FIRST_PARA
101
102 The first HTML C<p> element of the document.
103
104 =item HEADLINE
105
106 The first C<h1> tag; failing that, the first C<h2>; failing that,
107 the value of C<$NOT_AVAILABLE>.
108
109 =item PLUS...
110
111 Any meta-fields specified in the C<FIELDS> field.
112
113 =back
114
115 =cut
116
117 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod undocumented
118 2 my $class = shift;
119 2 ▶ $class = ref($class)? ref($class) : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 2 2
120 2 my $self = bless {}, $class;
121 2 ▶ my $args = ref($_[0])? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
122
123 # Defaults
124 2 $self->{SUMMARY} = {};
125
126 # Load parameters
127 2 $self->{uc $_} = $args->{$_} foreach keys %$args;
128 2 ▶ croak "Required parameter field missing : $_" if not $self->{PATH};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PATH"} 0 2 2
129
130 2 $self->_get_summary();
131 2 return $self;
132 }
133
134
135 2 ▶sub _get_summary {
Subroutine _get_summary: called CC=18 SCAR=32.0
136 2 my ($self,$path) = @_;
137
138 2 my ($p,$token, $html);
139
140 2 ▶ if (defined $path){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 0 2 2
141 0 ▶ if (ref $path){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path) { } 0 0 0
142 0 $html = $path;
143 0 delete $self->{PATH};
144 } else {
145 0 $self->{PATH} = $path;
146 }
147 }
148
149 2 ▶ if ($self->{PATH}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PATH"}) 2 0 2
150 2 ▶ $html = $self->_load_file()
Branch: 1 branch
BranchTrueFalseTotal
$html = $self->_load_file or return undef 2 0 2
151 or return undef;
152 }
153
154 # Get first para
155 2 ▶ if (not $p = new HTML::TokeParser( $html ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($p = "HTML::TokeParser"->new($html)) 0 2 2
156 0 warn "HTML::TokeParser could not initiate: $!";
157 0 return undef;
158 }
159 2 ▶ if ($token = $p->get_tag('h1')){
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $p->get_tag('h1')) { } 1 1 2
160 1 $self->{SUMMARY}->{HEADLINE} = $p->get_trimmed_text;
161 } else {
162 1 $p = new HTML::TokeParser( $html );
163 1 ▶ if ($token = $p->get_tag('h2')){
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $p->get_tag('h2')) { } 0 1 1
164 0 $self->{SUMMARY}->{HEADLINE} = $p->get_trimmed_text;
165 } else {
166 1 $self->{SUMMARY}->{HEADLINE} = $self->{NOT_AVAILABLE};
167 }
168 }
169 2 ▶ if (not $p = new HTML::TokeParser( $html ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($p = "HTML::TokeParser"->new($html)) 0 2 2
170 0 warn "HTML::TokeParser could not initiate: $!";
171 0 return undef;
172 }
173 2 ▶ if ($token = $p->get_tag('p')){
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $p->get_tag('p')) { } 1 1 2
174 1 $self->{SUMMARY}->{FIRST_PARA} = $p->get_trimmed_text;
175 } else {
176 $self->{SUMMARY}->{FIRST_PARA} = $self->{NOT_AVAILABLE}
177 1 }
178
179 2 $p = new HTML::TokeParser( $html );
180 2 $p->get_tag('title');
181 2 ▶ $self->{SUMMARY}->{TITLE} = $p->get_text('/title') || $self->{NOT_AVAILABLE};
Condition66%2/3
$p->get_text('/title') || $self->{'NOT_AVAILABLE'}
l!l&&r!l&&!r
101
MC/DC50%1/2
$p->get_text('/title') || $self->{'NOT_AVAILABLE'}
$p->get_text('/title') $self->{'NOT_AVAILABLE'}
Truth table66%2/3
A || B
A= $p->get_text('/title')B= $self->{'NOT_AVAILABLE'}
ABresult
1X1
011
000
182
183 {
184 2 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
185 2 $atime,$mtime,$ctime,$blksize,$blocks) = stat $self->{PATH};
186
187 2 ▶ $self->{SUMMARY}->{LAST_MODIFIED_FILE} = scalar localtime ( $mtime ) || $self->{NOT_AVAILABLE};
Condition33%1/3
scalar localtime $mtime || $self->{'NOT_AVAILABLE'}
l!l&&r!l&&!r
200
MC/DC0%0/2
scalar localtime $mtime || $self->{'NOT_AVAILABLE'}
scalar localtime $mtime $self->{'NOT_AVAILABLE'}
Truth table33%1/3
A || B
A= scalar localtime $mtimeB= $self->{'NOT_AVAILABLE'}
ABresult
1X1
011
000
188 2 $self->{SUMMARY}->{LAST_MODIFIED_FILE} =~ s/\s+/ /g;
189
190 2 ▶ $self->{SUMMARY}->{CREATED_FILE} = scalar localtime ( $ctime ) || $self->{NOT_AVAILABLE};
Condition33%1/3
scalar localtime $ctime || $self->{'NOT_AVAILABLE'}
l!l&&r!l&&!r
200
MC/DC0%0/2
scalar localtime $ctime || $self->{'NOT_AVAILABLE'}
scalar localtime $ctime $self->{'NOT_AVAILABLE'}
Truth table33%1/3
A || B
A= scalar localtime $ctimeB= $self->{'NOT_AVAILABLE'}
ABresult
1X1
011
000
191 2 $self->{SUMMARY}->{CREATED_FILE} =~ s/\s+/ /g;
192 }
193
194 my $collect = {
195 8 map {$_=>1} (
196 2 keys %{$self->{FIELDS}},
197 qw(
198 AUTHOR DESCRIPTION
199 LAST-MODIFIED CREATED
200 )
201 )
202 };
203
204 2 $self->{SUMMARY}->{$_} = $self->{NOT_AVAILABLE} foreach keys %$collect;
205
206 2 $p = new HTML::TokeParser( $html );
207 2 ▶ while (my $tag = $p->get_tag('meta') ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $p->get_tag("meta")) 4 2 6
208 4 my $name = uc $tag->[1]->{name};
209 I:
210 4 for my $i (1..2){
211 7 ▶ $name =~ s/^X-META-//i if $i == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 2 3 4 7
212 7 ▶ if (exists $collect->{$name} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $collect->{$name}) 4 3 7
213 4 $self->{SUMMARY}->{$name} = $tag->[1]->{content};
214 4 last I;
215 }
216 }
217 }
218
219 2 $self->{SUMMARY}->{LAST_MODIFIED_META} = delete $self->{SUMMARY}->{"LAST-MODIFIED"};
220 2 $self->{SUMMARY}->{CREATED_META} = delete $self->{SUMMARY}->{"CREATED"};
221
222 2 return 1;
223 }
224
225
226 # Return a reference to a scalar of HTML, or C<undef> on failure, setting C<$!> with an error message.
227
228 2 ▶sub _load_file {
Subroutine _load_file: called CC=5 SCAR=19.4
229 2 my ($self,$path) = @_;
230 2 local *IN;
231 2 ▶ return $path if ref $path;
Branch: 1 branch
BranchTrueFalseTotal
if ref $path 0 2 2
232
233 2 ▶ if (defined $path){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $path) { } 0 2 2
elsif (not $self->{'PATH'}) { } 0 2 2
234 0 $self->{PATH} = $path
235 }
236 elsif (not $self->{PATH}){
237 0 warn "load_file requires a path argument, or that the PATH field be set";
238 0 return undef;
239 }
240 2 ▶ if (not open IN, $self->{PATH}){
Branch: 1 branch
BranchTrueFalseTotal
unless (open IN, $self->{"PATH"}) 0 2 2
241 0 warn "load_file could not open $self->{PATH}";
242 0 return undef;
243 }
244 2 read IN, $_, -s IN;
245 2 close IN;
246 2 return \$_;
247 }
248
249 1 1;
250
251 =head1 TODO
252
253 Maybe work on URI as well as file paths.
254
255 =head1 SEE ALSO
256
257 L<HTML::TokeParser>, L<HTML::HeadParser>.
258
259 =head1 AUTHOR
260
261 Lee Goddard (LGoddard@CPAN.org)
262
263 =head1 COPYRIGHT
264
265 Copyright 2000-2001 Lee Goddard.
266
267 This library is free software; you may use and redistribute it or modify it
268 undef the same terms as Perl itself.
269
270
271
↑ Summary