File Coverage

blib/lib/HTML/Tidy/Message.pm
Criterion Covered Total %
statement 34 34 100.0
branch 2 2 100.0
condition 6 7 85.7
subroutine 11 11 100.0
pod 8 8 100.0
total 61 62 98.3


line stmt bran cond sub pod time code
1             package HTML::Tidy::Message;
2              
3 20     20   67 use warnings;
  20         22  
  20         536  
4 20     20   62 use strict;
  20         19  
  20         540  
5             use overload
6 20         118 q{""} => \&as_string,
7 20     20   17453 fallback => 'sounds like a good idea';
  20         14175  
8              
9             =head1 NAME
10              
11             HTML::Tidy::Message - Message object for the Tidy functionality
12              
13             =head1 SYNOPSIS
14              
15             See L<HTML::Tidy|HTML::Tidy> for all the gory details.
16              
17             =head1 EXPORTS
18              
19             None. It's all object-based.
20              
21             =head1 METHODS
22              
23             Almost everything is an accessor.
24              
25             =head2 new( $file, $line, $column, $text )
26              
27             Create an object. It's not very exciting.
28              
29             =cut
30              
31             sub new {
32 166     166 1 1190 my $class = shift;
33              
34 166         118 my $file = shift;
35 166         124 my $type = shift;
36 166   100     276 my $line = shift || 0;
37 166   100     228 my $column = shift || 0;
38 166         117 my $text = shift;
39              
40             # Add an element that says what tag caused the error (B, TR, etc)
41             # so that we can match 'em up down the road.
42 166         402 my $self = {
43             _file => $file,
44             _type => $type,
45             _line => $line,
46             _column => $column,
47             _text => $text,
48             };
49              
50 166         166 bless $self, $class;
51              
52 166         291 return $self;
53             }
54              
55             =head2 where()
56              
57             Returns a formatted string that describes where in the file the
58             error has occurred.
59              
60             For example,
61              
62             (14:23)
63              
64             for line 14, column 23.
65              
66             The terrible thing about this function is that it's both a plain
67             ol' formatting function as in
68              
69             my $str = where( 14, 23 );
70              
71             AND it's an object method, as in:
72              
73             my $str = $error->where();
74              
75             I don't know what I was thinking when I set it up this way, but
76             it's bad practice.
77              
78             =cut
79              
80             sub where {
81 370     370 1 235 my $self = shift;
82              
83 370 100 66     361 return '-' unless $self->line && $self->column;
84              
85 343         399 return sprintf( '(%d:%d)', $self->line, $self->column );
86             }
87              
88             =head2 as_string()
89              
90             Returns a nicely-formatted string for printing out to stdout or some similar user thing.
91              
92             =cut
93              
94             sub as_string {
95 370     370 1 3005 my $self = shift;
96              
97 370         564 my %strings = (
98             1 => 'Info',
99             2 => 'Warning',
100             3 => 'Error',
101             );
102              
103             return sprintf( '%s %s %s: %s',
104 370         401 $self->file, $self->where, $strings{$self->type}, $self->text );
105             }
106              
107             =head2 file()
108              
109             Returns the filename of the error, as set by the caller.
110              
111             =head2 type()
112              
113             Returns the type of the error. This will either be C<TIDY_ERROR>,
114             or C<TIDY_WARNING>.
115              
116             =head2 line()
117              
118             Returns the line number of the error, or 0 if there isn't an applicable
119             line number.
120              
121             =head2 column()
122              
123             Returns the column number, or 0 if there isn't an applicable column
124             number.
125              
126             =head2 text()
127              
128             Returns the text of the message. This does not include a type string,
129             like "Info: ".
130              
131             =cut
132              
133 372     372 1 983 sub file { my $self = shift; return $self->{_file} }
  372         533  
134 413     413 1 949 sub type { my $self = shift; return $self->{_type} }
  413         765  
135 715     715 1 1131 sub line { my $self = shift; return $self->{_line} }
  715         1175  
136 688     688 1 1205 sub column { my $self = shift; return $self->{_column} }
  688         1790  
137 395     395 1 943 sub text { my $self = shift; return $self->{_text} }
  395         1433  
138              
139              
140             =head1 COPYRIGHT & LICENSE
141              
142             Copyright 2005-2013 Andy Lester.
143              
144             This program is free software; you can redistribute it and/or modify
145             it under the terms of the Artistic License v2.0.
146              
147             =head1 AUTHOR
148              
149             Andy Lester, C<< <andy@petdance.com> >>
150              
151             =cut
152              
153             1; # happy