blib/lib/HTML/Quoted.pm

filter: Statementstmt 75.5% 108 / 143 Branchbran 60.0% 30 / 50 Conditioncond 47.6% 10 / 21 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 80.0% 12 / 15 Podpod 100% 2 / 2 totaltotal 68.1% 167 / 245 SCAR 42.2
CC
34
Cov
69%
combine
42.0
decode_entities
12.0
handle_start
9.2
CRAP 67.9
↑ Summary
1 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package HTML::Quoted;
6  
7 3 our $VERSION = '0.04';
8  
9 =head1 NAME
10  
11 HTML::Quoted - extract structure of quoted HTML mail message
12  
13 =head1 SYNOPSIS
14  
15 use HTML::Quoted;
16 my $html = '...';
17 my $struct = HTML::Quoted->extract( $html );
18  
19 =head1 DESCRIPTION
20  
21 Parses and extracts quotation structure out of a HTML message.
22 Purpose and returned structures are very similar to
23 L<Text::Quoted>.
24  
25 =head1 SUPPORTED FORMATS
26  
27 Variouse MUAs use quite different approaches for quoting in mails.
28  
29 Some use I<blockquote> tag and it's quite easy to parse.
30  
31 Some wrap text into I<p> tags and add '>' in the beginning of the
32 paragraphs.
33  
34 Things gettign messier when it's an HTML reply on plain text mail
35 thread.
36  
37 If B<you found format> that is not supported then file a bug report
38 via rt.cpan.org with as short as possible example. B<Test file>
39 is even better. Test file with patch is the best. Not obviouse patches
40 without tests suck.
41  
42 =head1 METHODS
43  
44 =head2 extract
45  
46 my $struct = HTML::Quoted->extract( $html );
47  
48 Takes a string with HTML and returns array reference. Each element
49 in the array either array or hash. For example:
50  
51  
52 [
53 { 'raw' => 'Hi,' },
54 { 'raw' => '<div><br><div>On date X wrote:<br>' },
55 [
56 { 'raw' => '<blockquote>' },
57 { 'raw' => 'Hello,' },
58 { 'raw' => '<div>How are you?</div>' },
59 { 'raw' => '</blockquote>' }
60 ],
61 ...
62 ]
63  
64 Hashes represent a part of the html. The following keys are
65 meaningful at the moment:
66  
67 =over 4
68  
69 =item * raw - raw HTML
70  
71 =item * quoter_raw, quoter - raw and decoded (entities are converted) quoter if block is prefixed with quoting characters
72  
73 =back
74  
75 =cut
76  
77 sub extract {
78 22 ▶ my $self = shift;
Subroutine extract: called CC=1 SCAR=0.0
Pod documented
79 22 my $parser = HTML::Quoted::Parser->new(
80 api_version => 3,
81 handlers => {
82 start_document => [handle_doc_start => 'self'],
83 end_document => [handle_doc_end => 'self'],
84 start => [handle_start => 'self, tagname, attr, attrseq, text'],
85 end => [handle_end => 'self, tagname, text'],
86 text => [handle_text => 'self, text, is_cdata'],
87 default => [handle_default => 'self, event, text'],
88 },
89 );
90 22 $parser->empty_element_tags(1);
91 22 $parser->parse($_[0]);
92 22 $parser->eof;
93  
94 22 return $parser->{'html_quoted_parser'}{'result'};
95 }
96  
97 =head2 combine_hunks
98  
99 my $html = HTML::Quoted->combine_hunks( $arrayref_of_hunks );
100  
101 Takes the output of C<extract> and turns it back into HTML.
102  
103 =cut
104  
105 sub combine_hunks {
106 13 ▶ my ($self, $hunks) = @_;
Subroutine combine_hunks: called CC=2 SCAR=6.9
Pod documented
107  
108 join "",
109 13 ▶ map {; ref $_ eq 'HASH' ? $_->{raw} : $self->combine_hunks($_) } @$hunks;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 25 2 27
110 }
111  
112 package HTML::Quoted::Parser;
113 3 ▶use base "HTML::Parser";
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 sub handle_doc_start {
116 22 ▶ my ($self) = @_;
Subroutine handle_doc_start: called CC=1 SCAR=0.0
117 22 my $meta = $self->{'html_quoted_parser'} = {};
118 22 my $res = $meta->{'result'} = [{}];
119 22 $meta->{'current'} = $res->[0];
120 22 $meta->{'stack'} = [$res];
121 22 $meta->{'in'} = { quote => 0, block => [0] };
122 }
123  
124 sub handle_doc_end {
125 22 ▶ my ($self) = @_;
Subroutine handle_doc_end: called CC=3 SCAR=11.0
126  
127 22 my $meta = $self->{'html_quoted_parser'};
128 22 ▶ pop @{ $meta->{'result'} } if ref $meta->{'result'}[-1] eq 'HASH' && !keys %{ $meta->{'result'}[-1] };
Branch: 1 branch
BranchTrueFalseTotal
if ref $meta->{"result"}[-1] eq "HASH" and not keys %{$$meta{"result"}[-1];} 12 10 22
Condition66%2/3
ref $meta->{"result"}[-1] eq "HASH" and not keys %{$$meta{"result"}[-1];}
!ll&&!rl&&r
01012
MC/DC50%1/2
ref $meta->{"result"}[-1] eq "HASH" and not keys %{$$meta{"result"}[-1];}
ref $meta->{"result"}[-1] eq "HASH" not keys %{$$meta{"result"}[-1];}
Truth table66%2/3
A and not(B)
A= ref $meta->{"result"}[-1] eq "HASH"B= not keys %{$$meta{"result"}[-1];}
ABresult
0X0
100
111
129 22 $self->organize( $meta->{'result'} );
130 }
131  
132 sub organize {
133 26 ▶ my ($self, $list) = @_;
Subroutine organize: called CC=7 SCAR=21.8
134  
135 26 my $prev = undef;
136 26 foreach my $e ( splice @$list ) {
137 54 ▶ if ( ref $e eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $e eq 'ARRAY') { } 4 50 54
elsif ($e->{'block'}) { } 20 30 50
elsif (defined $e->{'quoter'}) { } 0 30 30
138 4 push @$list, $self->organize($e);
139 4 $prev = undef;
140 }
141 elsif ( $e->{'block'} ) {
142 20 push @$list, $e;
143 20 $prev = undef;
144 }
145 elsif ( defined $e->{'quoter'} ) {
146 0 ▶ if ( !$prev || $self->combine( $prev, $e ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $prev or $self->combine($prev, $e)) 0 0 0
Condition0%0/3
not $prev or $self->combine($prev, $e)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $prev or $self->combine($prev, $e)
not $prev $self->combine($prev, $e)
Truth table0%0/3
not(A) or B
A= not $prevB= $self->combine($prev, $e)
ABresult
1X1
011
000
147 0 push @$list, $prev = [ $e ];
148 }
149 } else {
150 30 push @$list, $e;
151 30 $prev = undef;
152 }
153 }
154 26 return $list;
155 }
156  
157 sub combine {
158 0 ▶ my ($self, $list, $e) = @_;
Subroutine combine: not called CC=6 SCAR=37.4
159 0 my ($last) = grep ref $_ eq 'HASH', reverse @$list;
160 0 ▶ if ( $last->{'quoter'} eq $e->{'quoter'} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($last->{'quoter'} eq $e->{'quoter'}) { } 0 0 0
elsif (rindex($last->{'quoter'}, $e->{'quoter'}, 0) == 0) { } 0 0 0
elsif (rindex($e->{'quoter'}, $last->{'quoter'}, 0) == 0) { } 0 0 0
161 0 push @$list, $e;
162 0 return ();
163 }
164 elsif ( rindex( $last->{'quoter'}, $e->{'quoter'}, 0) == 0 ) {
165 0 @$list = ( [@$list], $e );
166 0 return ();
167 }
168 elsif ( rindex( $e->{'quoter'}, $last->{'quoter'}, 0) == 0 ) {
169 0 ▶ if ( ref $list->[-1] eq 'ARRAY' && !$self->combine( $list->[-1], $e ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list->[-1] eq "ARRAY" and not $self->combine($list->[-1], $e)) 0 0 0
Condition0%0/3
ref $list->[-1] eq "ARRAY" and not $self->combine($list->[-1], $e)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $list->[-1] eq "ARRAY" and not $self->combine($list->[-1], $e)
ref $list->[-1] eq "ARRAY" not $self->combine($list->[-1], $e)
Truth table0%0/3
A and not(B)
A= ref $list->[-1] eq "ARRAY"B= not $self->combine($list->[-1], $e)
ABresult
0X0
100
111
170 0 return ();
171 }
172 0 push @$list, [ $e ];
173 0 return ();
174 }
175 else {
176 0 return $e;
177 }
178 }
179  
180 # XXX: p is treated as inline tag as it's groupping tag that
181 # can not contain blocks inside, use span for groupping
182 3 my %INLINE_TAG = map {$_ => 1 } qw(
183 a br span bdo map img
184 tt i b big small
185 em strong dfn code q
186 samp kbd var cite abbr acronym sub sup
187 p
188 );
189  
190 3 my %ENTITIES = (
191 '&gt;' => '>',
192 '&#62;' => '>',
193 '&#x3e;' => '>',
194 );
195  
196 3 my $re_amp = join '|', map "\Q$_\E", '>', grep $ENTITIES{$_} eq '>', keys %ENTITIES;
197 3 $re_amp = qr{$re_amp};
198 3 my $re_quote_char = qr{[!#%=|:]};
199 3 my $re_quote_chunk = qr{ $re_quote_char(?!\w) | \w*$re_amp+ }x;
200 3 my $re_quoter = qr{ $re_quote_chunk (?:[ \\t]* $re_quote_chunk)* }x;
201  
202 sub handle_start {
203 40 ▶ my ($self, $tag, $attr, $attrseq, $text) = @_;
Subroutine handle_start: called CC=9 SCAR=22.2
204  
205 40 my $meta = $self->{'html_quoted_parser'};
206 40 my $stack = $meta->{'stack'};
207  
208 40 ▶ if ( $meta->{'in'}{'br'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"in"}{"br"}) 0 40 40
209 0 $meta->{'in'}{'br'} = 0;
210 0 push @{ $stack->[-1] }, $meta->{'current'} = {};
211 }
212  
213 40 ▶ if ( $tag eq 'blockquote' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag eq 'blockquote') { } 4 36 40
elsif ($tag eq 'br' and not $meta->{'in'}{'block'}[-1]) { } 14 22 36
elsif (not $INLINE_TAG{$tag}) { } 18 4 22
214 4 my $new = [{ quote => 1, block => 1 }];
215 4 push @{ $stack->[-1] }, $new;
216 4 push @$stack, $new; # HACK: everything pushed into this
217 4 $meta->{'current'} = $new->[0];
218 4 $meta->{'in'}{'quote'}++;
219 4 push @{ $meta->{'in'}{'block'} }, 0;
220 4 $meta->{'current'}{'raw'} .= $text;
221 4 push @{ $stack->[-1] }, $meta->{'current'} = {};
222 }
223 ▶ elsif ( $tag eq 'br' && !$meta->{'in'}{'block'}[-1] ) {
Condition100%3/3
$tag eq "br" and not $meta->{"in"}{"block"}[-1]
!ll&&!rl&&r
18414
MC/DC100%2/2
$tag eq "br" and not $meta->{"in"}{"block"}[-1]
$tag eq "br" not $meta->{"in"}{"block"}[-1]
Truth table100%3/3
A and not(B)
A= $tag eq "br"B= not $meta->{"in"}{"block"}[-1]
ABresult
0X0
100
111
224 14 $meta->{'current'}{'raw'} .= $text;
225 14 my $line = $meta->{'current'}{'raw'};
226 14 ▶ if ( $line =~ /^\n*($re_quoter)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\n*($re_quoter)/) 0 14 14
227 0 $meta->{'current'}{'quoter_raw'} = $1;
228 $meta->{'current'}{'quoter'} = $self->decode_entities(
229 0 $meta->{'current'}{'quoter_raw'}
230 );
231 }
232 14 $meta->{'in'}{'br'} = 1;
233 }
234 elsif ( !$INLINE_TAG{ $tag } ) {
235 18 ▶ if ( !$meta->{'in'}{'block'}[-1] && keys %{ $meta->{'current'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta->{"in"}{"block"}[-1] and keys %{$$meta{"current"};}) 4 14 18
Condition100%3/3
not $meta->{"in"}{"block"}[-1] and keys %{$$meta{"current"};}
!ll&&!rl&&r
684
MC/DC100%2/2
not $meta->{"in"}{"block"}[-1] and keys %{$$meta{"current"};}
not $meta->{"in"}{"block"}[-1] keys %{$$meta{"current"};}
Truth table100%3/3
not(A) and B
A= not $meta->{"in"}{"block"}[-1]B= keys %{$$meta{"current"};}
ABresult
0X0
100
111
236 4 push @{ $stack->[-1] }, $meta->{'current'} = { raw => '' };
237 }
238 18 $meta->{'current'}{'block'} = 1;
239 18 $meta->{'current'}{'raw'} .= $text;
240  
241 18 $meta->{'in'}{'block'}[-1]++;
242 }
243 else {
244 4 $meta->{'current'}{'raw'} .= $text;
245 }
246 }
247  
248 sub handle_end {
249 32 ▶ my ($self, $tag, $text) = @_;
Subroutine handle_end: called CC=8 SCAR=21.7
250  
251 32 my $meta = $self->{'html_quoted_parser'};
252 32 my $stack = $meta->{'stack'};
253  
254 32 ▶ if ( $meta->{'in'}{'br'} && $tag ne 'br' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"in"}{"br"} and $tag ne "br") 0 32 32
Condition66%2/3
$meta->{"in"}{"br"} and $tag ne "br"
!ll&&!rl&&r
22100
MC/DC0%0/2
$meta->{"in"}{"br"} and $tag ne "br"
$meta->{"in"}{"br"} $tag ne "br"
Truth table66%2/3
A and B
A= $meta->{"in"}{"br"}B= $tag ne "br"
ABresult
0X0
100
111
255 0 $meta->{'in'}{'br'} = 0;
256 0 push @{ $stack->[-1] }, $meta->{'current'} = {}
257 }
258  
259 32 $meta->{'current'}{'raw'} .= $text;
260  
261 32 ▶ if ( $tag eq 'blockquote' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag eq 'blockquote') { } 4 28 32
elsif ($tag eq 'br') { } 10 18 28
elsif ($tag eq 'p') { } 0 18 18
elsif (not $INLINE_TAG{$tag}) { } 18 0 18
262 4 pop @$stack;
263 4 pop @{ $meta->{'in'}{'block'} };
264 4 push @{ $stack->[-1] }, $meta->{'current'} = {};
265 4 $meta->{'in'}{'quote'}--;
266 }
267 elsif ( $tag eq 'br' ) {
268 10 $meta->{'in'}{'br'} = 0;
269 10 push @{ $stack->[-1] }, $meta->{'current'} = {}
270 }
271 elsif ( $tag eq 'p' ) {
272 0 push @{ $stack->[-1] }, $meta->{'current'} = {}
273 }
274 elsif ( !$INLINE_TAG{ $tag } ) {
275 18 $meta->{'in'}{'block'}[-1]--;
276 18 ▶ if ( $meta->{'in'}{'block'}[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{'in'}{'block'}[-1]) { } 6 12 18
277 6 $meta->{'current'}{'block'} = 1;
278 } else {
279 12 push @{ $stack->[-1] }, $meta->{'current'} = {};
280 }
281 }
282 }
283  
284 sub decode_entities {
285 0 ▶ my ($self, $string) = @_;
Subroutine decode_entities: not called CC=3 SCAR=24.8
286 0 ▶ $string =~ s/(&(?:[a-z]+|#[0-9]|#x[0-9a-f]+);)/ $ENTITIES{$1} || $ENTITIES{lc $1} || $1 /ge;
Branch: 1 branch
BranchTrueFalseTotal
$ENTITIES{$1} or $ENTITIES{lc $1} or $1 0 0 0
Condition0%0/3
$ENTITIES{$1} or $ENTITIES{lc $1}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENTITIES{$1} or $ENTITIES{lc $1}
$ENTITIES{$1} $ENTITIES{lc $1}
Truth table0%0/3
A or B
A= $ENTITIES{$1}B= $ENTITIES{lc $1}
ABresult
1X1
011
000
287 0 return $string;
288 }
289  
290 sub handle_text {
291 36 ▶ my ($self, $text) = @_;
Subroutine handle_text: called CC=2 SCAR=6.9
292 36 my $meta = $self->{'html_quoted_parser'};
293 36 ▶ if ( $meta->{'in'}{'br'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"in"}{"br"}) 2 34 36
294 2 $meta->{'in'}{'br'} = 0;
295 2 push @{ $meta->{'stack'}[-1] }, $meta->{'current'} = {};
296 }
297 36 $self->{'html_quoted_parser'}{'current'}{'raw'} .= $text;
298 }
299  
300 sub handle_default {
301 0 ▶ my ($self, $event, $text) = @_;
Subroutine handle_default: not called CC=2 SCAR=17.9
302 0 my $meta = $self->{'html_quoted_parser'};
303 0 ▶ if ( $meta->{'in'}{'br'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"in"}{"br"}) 0 0 0
304 0 $meta->{'in'}{'br'} = 0;
305 0 push @{ $meta->{'stack'}[-1] }, $meta->{'current'} = {};
306 }
307 0 $self->{'html_quoted_parser'}{'current'}{'raw'} .= $text;
308 }
309  
310 =head1 AUTHOR
311  
312 Ruslan.Zakirov E<lt>ruz@bestpractical.comE<gt>
313  
314 =head1 LICENSE
315  
316 Under the same terms as perl itself.
317  
318 =cut
319  
320 3 1;
↑ Summary