/usr/share/perl5/HTML/FormatText/WithLinks.pm is in libhtml-formattext-withlinks-perl 0.15-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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | package HTML::FormatText::WithLinks;
use strict;
use URI::WithBase;
use HTML::TreeBuilder;
use base qw(HTML::FormatText);
use vars qw($VERSION);
$VERSION = '0.15';
sub new {
my $proto = shift;
my $class = ref( $proto ) || $proto;
my $self = $class->SUPER::new( @_ );
$self->configure() unless @_;
bless ( $self, $class );
return $self;
}
sub configure {
my ($self, $hash) = @_;
# a base uri so we can resolve relative uris
$self->{base} = $hash->{base};
delete $hash->{base};
$self->{base} =~ s#(.*?)/[^/]*$#$1/# if $self->{base};
$self->{doc_overrides_base} = $hash->{doc_overrides_base};
delete $hash->{doc_overrides_base};
$self->{before_link} = '[%n]';
$self->{after_link} = '';
$self->{footnote} = '%n. %l';
$self->{link_num_generator} = sub { return shift() + 1 };
$self->{unique_links} = 0;
$self->{anchor_links} = 1;
$self->{skip_linked_urls} = 0;
$self->{_link_track} = {};
$self->{bold_marker} = '_';
$self->{italic_marker} = '/';
foreach ( qw( before_link after_link footnote link_num_generator
with_emphasis bold_marker italic_marker
unique_links anchor_links skip_linked_urls ) ) {
$self->{ $_ } = $hash->{ $_ } if exists $hash->{ $_ };
delete $hash->{ $_ };
}
$self->SUPER::configure($hash);
}
# we need to do this as if you pass an HTML fragment without any
# containing block level markup (e.g. a p tag) then no indentation
# takes place so if we've not got a cur_pos we indent.
sub textflow {
my $self = shift;
$self->goto_lm unless defined $self->{cur_pos};
$self->SUPER::textflow(@_);
}
sub head_start {
my ($self) = @_;
$self->SUPER::head_start();
# we don't care about what the documents says it's base is
if ( $self->{base} and not $self->{doc_overrides_base} ) {
return 0;
}
# descend into <head> for possible <base> there, even if superclass not
# interested (as of HTML::FormatText 2.04 it's not)
return 1;
}
# <base> is supposed to be inside <head>, but no need to demand that.
# "lynx -source" sticks a <base> at the very start of the document, before
# even <html>, so accepting <base> anywhere lets that work.
sub base_start {
my ($self, $node) = @_;
if (my $href = $node->attr('href')) {
$self->{base} = $href;
}
# allow for no superclass base_start() in HTML::FormatText 2.04
if (! HTML::FormatText->can('base_start')) {
return 0;
}
# chain up if it exists in the future
return $self->SUPER::base_start();
}
sub a_start {
my $self = shift;
my $node = shift;
# local urls are no use so we have to make them absolute
my $href = $node->attr('href') || '';
if ($href && $self->{anchor_links} == 0 && $href =~ m/^#/o) {
$href = '';
}
elsif ($href and $self->{skip_linked_urls} and $href eq $node->as_text) {
$href = '';
}
if ( $href ) {
if ($href !~ m#^https?:|^mailto:#o) {
$href = URI::WithBase->new($href, $self->{base})->abs();
}
if ($self->{unique_links})
{
if (defined $self->{_link_track}->{$href})
{
$self->out( $self->text('before_link', $self->{_link_track}->{$href}, $href ) );
} else {
push @{$self->{_links}}, $href;
$self->{_link_track}->{$href} = $#{$self->{_links}};
$self->out( $self->text('before_link', $#{$self->{_links}}, $href ) );
}
} else {
push @{$self->{_links}}, $href;
$self->out( $self->text('before_link') );
}
}
$self->SUPER::a_start();
}
sub a_end {
my $self = shift;
my $node = shift;
my $text;
unless ($self->{skip_linked_urls} and $node->attr('href') eq $node->as_text) {
if ($self->{unique_links})
{
my $href = $node->attr('href');
$text = $self->text('after_link', $self->{_link_track}->{$href}, $href);
} else {
$text = $self->text('after_link');
}
# If we're just dealing with a fragment of HTML, with a link at the
# end, we get a space before the first footnote link if we do
# $self->out( '' )
if ($text ne '') {
$self->out( $text );
}
}
$self->SUPER::a_end();
}
sub b_start {
my $self = shift;
$self->out( $self->{'bold_marker'} ) if $self->{ with_emphasis };
$self->SUPER::b_start();
}
sub b_end {
my $self = shift;
$self->out( $self->{'bold_marker'} ) if $self->{ with_emphasis };
$self->SUPER::b_end();
}
sub i_start {
my $self = shift;
$self->out( $self->{'italic_marker'} ) if $self->{ with_emphasis };
$self->SUPER::i_start();
}
sub i_end {
my $self = shift;
$self->out( $self->{'italic_marker'} ) if $self->{ with_emphasis };
$self->SUPER::i_end();
}
# print out our links
sub html_end {
my $self = shift;
if ( $self->{_links} and @{$self->{_links}} and $self->{footnote} ) {
$self->nl; $self->nl; # be tidy
$self->goto_lm;
for (0 .. $#{$self->{_links}}) {
$self->goto_lm;
$self->out(
$self->text( 'footnote', $_, $self->{_links}->[$_] )
);
$self->nl;
}
}
$self->SUPER::end();
}
sub _link_num {
my ($self, $num) = @_;
$num = $#{$self->{_links}} unless defined $num;
return &{$self->{link_num_generator}}($num);
}
sub text {
my ($self, $type, $num, $href) = @_;
if ($self->{_links} and @{$self->{_links}}) {
$href = $self->{_links}->[$#{$self->{_links}}]
unless (defined $num and defined $href);
}
$num = $self->_link_num($num);
my $text = $self->{$type};
$text =~ s/%n/$num/g;
$text =~ s/%l/$href/g;
return $text;
}
sub parse {
my $self = shift;
my $text = shift;
return undef unless defined $text;
return '' if $text eq '';
my $tree = HTML::TreeBuilder->new->parse( $text );
return $self->_parse( $tree );
}
sub parse_file {
my $self = shift;
my $file = shift;
unless (-e $file and -f $file) {
$self->error("$file not found or not a regular file");
return undef;
}
my $tree = HTML::TreeBuilder->new->parse_file( $file );
return $self->_parse( $tree );
}
sub _parse {
my $self = shift;
my $tree = shift;
$self->{_link_track} = {};
$self->{_links} = [];
unless ( $tree ) {
$self->error( "HTML::TreeBuilder problem" . ( $! ? ": $!" : '' ) );
return undef;
}
$tree->eof();
my $return_text = $self->format( $tree );
$tree->delete;
return $return_text;
}
sub error {
my $self = shift;
if (@_) {
$self->{error} = shift;
}
return $self->{error};
}
1;
__END__
=head1 NAME
HTML::FormatText::WithLinks - HTML to text conversion with links as footnotes
=head1 SYNOPSIS
use HTML::FormatText::WithLinks;
my $f = HTML::FormatText::WithLinks->new();
my $html = qq(
<html>
<body>
<p>
Some html with a <a href="http://example.com/">link</a>
</p>
</body>
</html>
);
my $text = $f->parse($html);
print $text;
# results in something like
Some html with a [1]link
1. http://example.com/
my $f2 = HTML::FormatText::WithLinks->new(
before_link => '',
after_link => ' [%l]',
footnote => ''
);
$text = $f2->parse($html);
print $text;
# results in something like
Some html with a link [http://example.com/]
my $f3 = HTML::FormatText::WithLinks->new(
link_num_generator => sub {
return "*" x (shift() + 1);
},
footnote => '[%n] %l'
);
$text = $f3->parse($html);
print $text;
# results in something like
Some html with a [*]link
[*] http://example.com/
=head1 DESCRIPTION
HTML::FormatText::WithLinks takes HTML and turns it into plain text
but prints all the links in the HTML as footnotes. By default, it attempts
to mimic the format of the lynx text based web browser's --dump option.
=head1 METHODS
=head2 new
my $f = HTML::FormatText::WithLinks->new( %options );
Returns a new instance. It accepts all the options of HTML::FormatText plus
=over
=item base
a base option. This should be set to a URI which will be used to turn any
relative URIs on the HTML to absolute ones.
=item doc_overrides_base
If a base element is found in the document and it has an href attribute
then setting doc_overrides_base to true will cause the document's base
to be used. This defaults to false.
=item before_link (default: '[%n]')
=item after_link (default: '')
=item footnote (default: '[%n] %l')
a string to print before a link (i.e. when the <a> is found),
after link has ended (i.e. when then </a> is found) and when printing
out footnotes.
"%n" will be replaced by the link number, "%l" will be replaced by the
link itself.
If footnote is set to '', no footnotes will be printed.
=item link_num_generator (default: sub { return shift() + 1 })
link_num_generator is a sub that returns the value to be printed for a
given link number. The internal store starts numbering at 0.
=item with_emphasis
If set to 1 then italicised text will be surrounded by C</> and bolded text by
C<_>. You can change these markers by using the C<italic_marker> and
C<bold_marker> options.
=item unique_links
If set to 1 then will only generate 1 footnote per unique URI as oppose to the default behaviour which is to generate a footnote per URI.
=item anchor_links
If set to 0 then links pointing to local anchors will be skipped.
The default behaviour is to include all links.
=item skip_linked_urls
If set to 1, then links where the text equals the href value will be skipped.
The default behaviour is to include all links.
=back
=head2 parse
my $text = $f->parse($html);
Takes some HTML and returns it as text. Returns undef on error.
Will also return undef if you pass it undef. Returns an empty
string if passed an empty string.
=head2 parse_file
my $text = $f->parse_file($filename);
Takes a filename and returns the contents of the file as plain text.
Returns undef on error.
=head2 error
$f->error();
Returns the last error that occurred. In practice this is likely to be
either a warning that parse_file couldn't find the file or that
HTML::TreeBuilder failed.
=head1 CAVEATS
When passing HTML fragments the results may be a little unpredictable.
I've tried to work round the most egregious of the issues but any
unexpected results are welcome.
Also note that if for some reason there is an a tag in the document
that does not have an href attribute then it will be quietly ignored.
If this is really a problem for anyone then let me know and I'll see
if I can think of a sensible thing to do in this case.
=head1 AUTHOR
Struan Donald. E<lt>struan@cpan.orgE<gt>
L<http://www.exo.org.uk/code/>
Ian Malpass E<lt>ian@indecorous.comE<gt> was responsible for the custom
formatting bits and the nudge to release the code.
Simon Dassow E<lt>janus@errornet.de<gt> for the anchor_links option plus
a few bugfixes and optimisations
Kevin Ryde for the code for pulling the base out the document.
Thomas Sibley E<lt>trs@bestpractical.comE<gt> patches for skipping links that are their urls and to change the delimiters for bold and italic text..
=head1 SOURCE CODE
The source code for this module is hosted on GitHub
L<http://github.com/struan/html-formattext-withlinks>
=head1 COPYRIGHT
Copyright (C) 2003-2010 Struan Donald and Ian Malpass. All rights reserved.
=head1 LICENSE
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
=head1 SEE ALSO
perl(1), HTML::Formatter.
=cut
|