HTML::...thLinks(3pm)
β¨ HTML::FormatText::WithLinks β the perl (3pm) page but make it cute β¨
okay so bestie, this is the manual for HTML::FormatText::WithLinks and it's literally the moment HTML gets turned into plain text but like... with the links saved as footnotes so nothing gets lost. iconic behavior honestly π
β¨ NAME β¨
HTML::FormatText::WithLinks β turning HTML into text and keeping the links as footnotes π
πΈ 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/
π¦ DESCRIPTION π¦
HTML::FormatText::WithLinks takes your HTML and turns it into plain text, but the way it handles links is chef's kiss β it prints them all out as footnotes instead of just yeeting the URLs into the void. By default it's lowkey trying to mimic how the lynx text browser does its --dump thing. no cap, that's the vibe π
π METHODS π
π new
my $f = HTML::FormatText::WithLinks->new( %options );
Returns a shiny new instance bestie β¨ It takes every single option that HTML::FormatText accepts PLUS these extra ones:
baseβ a base option. Set this to a URI and it'll turn any relative URIs in the HTML into absolute ones. super useful fr fr.doc_overrides_baseβ if there's a base element in the doc and it has anhrefattribute, setting this to true makes the document's own base win. defaults to false (so like, thebaseoption is the main character by default).before_link(default:'[%n]') β a string printed before a link, like when the<a>tag shows up.after_link(default:'') β printed after the link ends (when</a>shows up).footnote(default:'[%n] %l') β printed when the footnotes are being printed out.
For all three of these: "%n" gets replaced by the link number and "%l" gets replaced by the link itself. and if you set footnote to ''? no footnotes get printed at all bestie. gone. π
link_num_generator(default:sub { return shift() + 1 }) β a sub that returns the value printed for a given link number. the internal store starts numbering at 0 (so it's zero-indexed, iykyk).with_emphasisβ set to 1 and italicised text gets wrapped in/and bolded text gets wrapped in_. you can change those markers with the"italic_marker"and"bold_marker"options πunique_linksβ set to 1 and you only get 1 footnote per unique URI instead of the default (which is a footnote per URI, no dedupe, periodt).anchor_linksβ set to 0 and links pointing to local anchors get skipped. default is to include all links (iconic inclusivity behavior).skip_linked_urlsβ set to 1 and links where the text equals the href value get skipped. default is to include all links.
π parse
my $text = $f->parse($html);
Takes some HTML and hands you back text. Returns undef on error. also returns undef if you pass it undef (fair enough bestie). returns an empty string if you pass it an empty string π«Ά
πΈ parse_file
my $text = $f->parse_file($filename);
Takes a filename and returns the contents as plain text. returns undef on error.
π¦ error
$f->error();
Returns the last error that happened. in practice it's probs gonna be either a warning that parse_file couldn't find the file, or that HTML::TreeBuilder failed π
π CAVEATS π
okay so real talk β when you pass HTML fragments, the results can be a little unpredictable ngl. I've tried to work around the most egregious of the issues, but unexpected results are welcome (fr, send them my way).
Also note: if for some reason there's an <a> tag in the document that does NOT have an href attribute, it will be quietly ignored π«₯ If that's genuinely a problem for anyone, let me know and I'll see if I can think of a sensible thing to do. no promises but like, I'll try π
π AUTHOR π
Struan Donald. <struan@cpan.org>
Ian Malpass <ian@indecorous.com> was responsible for the custom formatting bits AND the nudge to release the code. king behavior honestly π
Simon Dassow <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 <trs@bestpractical.com> for patches for skipping links that are their urls and for changing the delimiters for bold and italic text π
π SOURCE CODE π
The source code for this module is hosted on GitHub at <http://github.com/struan/html-formattext-withlinks> β go check it out bestie π«Ά
π COPYRIGHT π
Copyright (C) 2003-2010 Struan Donald and Ian Malpass. All rights reserved.
β LICENSE β
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay π
π₯ SEE ALSO π₯
perl(1), HTML::Formatter.
and that's the whole vibe bestie π this page is from perl v5.42.0, dated 2015-04-14, and it's served up in all its HTML::...thLinks(3pm) glory πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-HTML-FormatText-WithLinks 0.15-bp160.1.11 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms