blib/lib/HTML/T5.pm

filter: Statementstmt 100% 121 / 121 Branchbran 94.0% 47 / 50 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 98.5% 206 / 209 CC 31 SCAR 17.9
CC
31
Cov
98%
_parse_errors
13.0
new
5.0
ignore
5.0
CRAP 6.0
↑ Summary blib/lib/Test/HTML/T5.pm »
1 package HTML::T5;
2  
3 25 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 25 ▶use HTML::T5::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 HTML::T5 - HTML validation in a Perl object
13  
14 =head1 VERSION
15  
16 Version 0.014
17  
18 =cut
19  
20 25 our $VERSION = '0.014';
21  
22 =head1 SYNOPSIS
23  
24 use HTML::T5;
25  
26 my $tidy = HTML::T5->new( {config_file => 'path/to/config'} );
27 $tidy->ignore( type => TIDY_WARNING, type => TIDY_INFO );
28 $tidy->parse( "foo.html", $contents_of_foo );
29  
30 for my $message ( $tidy->messages ) {
31 print $message->as_string;
32 }
33  
34 =head1 DESCRIPTION
35  
36 C<HTML::T5> is an HTML checker in a handy dandy object. It's meant
37 as a replacement for L<HTML::Lint|HTML::Lint>. If you're currently
38 an L<HTML::Lint|HTML::Lint> user looking to migrate, see the section
39 L</Converting from HTML::Lint>.
40  
41 C<HTML::T5> was forked from L<HTML::Tidy5> by Andy Lester (PETDANCE), thanks.
42  
43 =head1 EXPORTS
44  
45 Message types C<TIDY_ERROR>, C<TIDY_WARNING> and C<TIDY_INFO>.
46  
47 Everything else is an object method.
48  
49 =cut
50  
51 25 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 25 ▶use constant TIDY_ERROR => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 25 ▶use constant TIDY_WARNING => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 25 ▶use constant TIDY_INFO => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 25 our @EXPORT = qw( TIDY_ERROR TIDY_WARNING TIDY_INFO );
58  
59 =head1 METHODS
60  
61 =head2 new()
62  
63 Create an HTML::T5 object.
64  
65 my $tidy = HTML::T5->new();
66  
67 Optionally you can give a hashref of configuration parms.
68  
69 my $tidy = HTML::T5->new( {config_file => 'path/to/tidy.cfg'} );
70  
71 This configuration file will be read and used when you clean or parse an HTML file.
72  
73 You can also pass options directly to tidy.
74  
75 my $tidy = HTML::T5->new( {
76 output_xhtml => 1,
77 tidy_mark => 0,
78 } );
79  
80 See C<tidy -help-config> for the list of options supported by tidy.
81  
82 The following options are not supported by C<HTML::T5>:
83  
84 =over 4
85  
86 =item * quiet
87  
88 =back
89  
90 =cut
91  
92 42 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
93 42 my $class = shift;
94 42 ▶ my $args = shift || {};
Condition100%2/2
shift() || {}
l!l
1923
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
95 42 my @unsupported_options = qw(
96 force-output
97 gnu-emacs-file
98 gnu-emacs
99 keep-time
100 quiet
101 slide-style
102 write-back
103 ); # REVIEW perhaps a list of supported options would be better
104  
105 42 my $self = bless {
106 messages => [],
107 ignore_type => [],
108 ignore_text => [],
109 config_file => '',
110 tidy_options => {},
111 }, $class;
112  
113 42 for my $key (keys %{$args} ) {
114 40 ▶ if ($key eq 'config_file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "config_file") 5 35 40
115 5 $self->{config_file} = $args->{$key};
116 5 next;
117 }
118  
119 35 my $newkey = $key;
120 35 $newkey =~ tr/_/-/;
121  
122 35 ▶ if ( grep {$newkey eq $_} @unsupported_options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$newkey eq $_;} @unsupported_options) 7 28 35
123 7 Carp::croak( "Unsupported option: $newkey" );
124 }
125  
126 28 $self->{tidy_options}->{$newkey} = $args->{$key};
127 }
128  
129 35 return $self;
130 }
131  
132 =head2 messages()
133  
134 Returns the messages accumulated.
135  
136 =cut
137  
138 28 ▶sub messages {
Subroutine messages: called CC=1 SCAR=0.0
Pod documented
139 28 my $self = shift;
140  
141 28 return @{$self->{messages}};
142 }
143  
144 =head2 clear_messages()
145  
146 Clears the list of messages, in case you want to print and clear, print
147 and clear. If you don't clear the messages, then each time you call
148 L<parse()|parse( $filename, $str [, $str...] )> you'll be accumulating more in the list.
149  
150 =cut
151  
152 12 ▶sub clear_messages {
Subroutine clear_messages: called CC=1 SCAR=0.0
Pod documented
153 12 my $self = shift;
154  
155 12 $self->{messages} = [];
156  
157 12 return;
158 }
159  
160 =head2 ignore( parm => value [, parm => value ] )
161  
162 Specify types of messages to ignore. Note that the ignore flags must be
163 set B<before> calling C<parse()>. You can call C<ignore()> as many times
164 as necessary to set up all your restrictions; the options will stack up.
165  
166 =over 4
167  
168 =item * type => TIDY_INFO|TIDY_WARNING|TIDY_ERROR
169  
170 Specifies the type of messages you want to ignore, either info or warnings
171 or errors. If you wanted, you could call ignore on all three and get
172 no messages at all.
173  
174 $tidy->ignore( type => TIDY_WARNING );
175  
176 =item * text => qr/regex/
177  
178 =item * text => [ qr/regex1/, qr/regex2/, ... ]
179  
180 Checks the text of the message against the specified regex or regexes,
181 and ignores the message if there's a match. The value for the I<text>
182 parm may be either a regex, or a reference to a list of regexes.
183  
184 $tidy->ignore( text => qr/DOCTYPE/ );
185 $tidy->ignore( text => [ qr/unsupported/, qr/proprietary/i ] );
186  
187 =back
188  
189 =cut
190  
191 16 ▶sub ignore {
Subroutine ignore: called CC=5 SCAR=16.1
Pod documented
192 16 my $self = shift;
193 16 my @parms = @_;
194  
195 16 ▶ while ( @parms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parms) 16 15 31
196 16 my $parm = shift @parms;
197 16 my $value = shift @parms;
198 16 ▶ my @values = ref($value) eq 'ARRAY' ? @{$value} : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 15 16
199  
200 16 ▶ Carp::croak( qq{Invalid ignore type of "$parm"} )
Branch: 1 branch
BranchTrueFalseTotal
unless $parm eq "text" or $parm eq "type" 1 15 16
Condition100%3/3
$parm eq "text" or $parm eq "type"
l!l&&r!l&&!r
4111
MC/DC100%2/2
$parm eq "text" or $parm eq "type"
$parm eq "text" $parm eq "type"
Truth table100%3/3
A or B
A= $parm eq "text"B= $parm eq "type"
ABresult
1X1
011
000
201 unless ($parm eq 'text') or ($parm eq 'type');
202  
203 15 push( @{$self->{"ignore_$parm"}}, @values );
204 } # while
205  
206 15 return;
207 } # ignore
208  
209 =head2 parse( $filename, $str [, $str...] )
210  
211 Parses a string, or list of strings, that make up a single HTML file.
212  
213 The I<$filename> parm is only used as an identifier for your use.
214 The file is not actually read and opened.
215  
216 Returns true if all went OK, or false if there was some problem calling
217 tidy, or parsing tidy's output.
218  
219 =cut
220  
221 22 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
222 22 my $self = shift;
223 22 my $filename = shift;
224 22 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 1 21 22
225 1 Carp::croak('Usage: parse($filename,$str [, $str...])');
226 }
227 21 my $html = join( '', @_ );
228  
229 21 ▶ utf8::encode($html) if utf8::is_utf8($html);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($html) 1 20 21
230 21 my ($errorblock,$newline) = _tidy_messages( $html, $self->{config_file}, $self->{tidy_options} );
231 21 ▶ return 1 unless defined $errorblock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errorblock 2 19 21
232  
233 19 utf8::decode($errorblock);
234  
235 19 return !$self->_parse_errors($filename, $errorblock, $newline);
236 }
237  
238 33 ▶sub _parse_errors {
Subroutine _parse_errors: called CC=13 SCAR=25.7
239 33 my $self = shift;
240 33 my $filename = shift;
241 33 my $errs = shift;
242 33 my $newline = shift;
243  
244 33 my $parse_errors;
245  
246 33 my @lines = split( /$newline/, $errs );
247  
248 33 for my $line ( @lines ) {
249 190 chomp $line;
250  
251 190 my $message;
252 190 ▶ if ( $line =~ /^line (\d+) column (\d+) - (Warning|Error|Info): (.+)$/ ) { ## no critic ( ControlStructures::ProhibitCascadingIfElse )
Branch: 7 branches
BranchTrueFalseTotal
if ($line =~ /^line (\d+) column (\d+) - (Warning|Error|Info): (.+)$/) { } 160 30 190
elsif ($line =~ /^Info: (.+)$/) { } 15 15 30
elsif ($line =~ /^Tidy found \d+ warnings? and \d+ errors?!/) { } 8 7 15
elsif ($line eq 'No warnings or errors were found.') { } 5 2 7
elsif ($line eq 'This document has errors that must be fixed before') { } 0 2 2
elsif ($line eq 'using HTML Tidy to generate a tidied up version.') { } 0 2 2
elsif ($line =~ /^\s*$/) { } 1 1 2
253 160 my ($line, $col, $type, $text) = ($1, $2, $3, $4);
254 160 ▶ $type =
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'Warning' ? : 133 27 160
$type eq 'Info' ? : 19 8 27
255 ($type eq 'Warning') ? TIDY_WARNING :
256 ($type eq 'Info') ? TIDY_INFO :
257 TIDY_ERROR;
258 160 $message = HTML::T5::Message->new( $filename, $type, $line, $col, $text );
259  
260 }
261 elsif ( $line =~ m/^Info: (.+)$/ ) {
262 # Info line we don't want
263  
264 15 my $text = $1;
265 15 $message = HTML::T5::Message->new( $filename, TIDY_INFO, undef, undef, $text );
266 }
267 elsif ( $line =~ /^Tidy found \d+ warnings? and \d+ errors?!/ ) {
268 # Summary line we don't want
269 # We should take these counts from the summary and make sure they match what we parsed.
270 }
271 elsif ( $line eq 'No warnings or errors were found.' ) {
272 # Summary line we don't want
273  
274 }
275 elsif ( $line eq 'This document has errors that must be fixed before' ) {
276 # Summary line we don't want
277  
278 }
279 elsif ( $line eq 'using HTML Tidy to generate a tidied up version.' ) {
280 # Summary line we don't want
281  
282 }
283 elsif ( $line =~ m/^\s*$/ ) {
284 # Blank line we don't want
285 }
286 else {
287 1 Carp::carp "HTML::T5: Unknown error type: $line";
288 1 ++$parse_errors;
289 }
290 190 ▶ push( @{$self->{messages}}, $message )
Branch: 1 branch
BranchTrueFalseTotal
if $message and $self->_is_keeper($message) 152 38 190
Condition100%3/3
$message and $self->_is_keeper($message)
!ll&&!rl&&r
1523152
MC/DC100%2/2
$message and $self->_is_keeper($message)
$message $self->_is_keeper($message)
Truth table100%3/3
A and B
A= $messageB= $self->_is_keeper($message)
ABresult
0X0
100
111
291 if $message && $self->_is_keeper( $message );
292 } # for
293 33 return $parse_errors;
294 }
295  
296 =head2 clean( $str [, $str...] )
297  
298 Cleans a string, or list of strings, that make up a single HTML file.
299  
300 Returns the cleaned string as a single string.
301  
302 =cut
303  
304 14 ▶sub clean {
Subroutine clean: called CC=4 SCAR=13.9
Pod documented
305 14 my $self = shift;
306  
307 14 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 1 13 14
308 1 Carp::croak('Usage: clean($str [, $str...])');
309 }
310 13 my $text = join( '', @_ );
311  
312 13 ▶ utf8::encode($text) if utf8::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($text) 2 11 13
313 13 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 13 0 13
314 13 $text .= "\n";
315 }
316  
317 my ($cleaned, $errbuf, $newline) = _tidy_clean( $text,
318 $self->{config_file},
319 13 $self->{tidy_options});
320 13 utf8::decode($cleaned);
321 13 utf8::decode($errbuf);
322  
323 13 $self->_parse_errors('', $errbuf, $newline);
324 13 return $cleaned;
325 }
326  
327 # Tells whether a given message object is one that we should keep.
328  
329 175 ▶sub _is_keeper {
Subroutine _is_keeper: called CC=5 SCAR=16.1
330 175 my $self = shift;
331  
332 175 my $message = shift;
333  
334 175 my @ignore_types = @{$self->{ignore_type}};
335 175 ▶ if ( @ignore_types ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ignore_types) 45 130 175
336 45 ▶ return 0 if grep { $message->type == $_ } @ignore_types;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$message->type == $_;} @ignore_types 18 27 45
337 }
338  
339 157 my @ignore_texts = @{$self->{ignore_text}};
340 157 ▶ if ( @ignore_texts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ignore_texts) 15 142 157
341 15 ▶ return 0 if grep { $message->text =~ $_ } @ignore_texts;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$message->text =~ /$_/;} @ignore_texts 5 10 15
342 }
343  
344 152 return 1;
345 }
346  
347 =head2 tidy_library_version()
348  
349 Returns the version of the underling tidy library.
350  
351 =cut
352  
353 3 ▶sub tidy_library_version {
Subroutine tidy_library_version: called CC=1 SCAR=0.0
Pod documented
354 3 my $version_str = _tidy_library_version();
355  
356 3 return $version_str;
357 }
358  
359 25 require XSLoader;
360 25 XSLoader::load('HTML::T5', $VERSION);
361  
362 25 1;
363  
364 __END__
↑ Summary blib/lib/Test/HTML/T5.pm »