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