Subject: | Patch to skip anchor links |
Hi,
the attached patch implements an additional 'anchor_links' option to
allow the ignorance of links pointing to local anchors.
Right now you get:
'This is a mail of some sort with a [1]link to a local anchor.
1. #top'
But what i think is better with my patch is:
'This is a mail of some sort with a link to a local anchor.'
Besides the implementation itself i've added POD bits and a test case.
Furthermore i changed a regular expression to be compiled (faster) and
let it respect https links too.
Would be nice to see this in some form going in; what do you think?
Regards,
Simon
Subject: | HTML-FormatText-WithLinks.diff |
--- lib/HTML/FormatText/WithLinks.pm.orig Wed Apr 18 23:26:59 2007
+++ lib/HTML/FormatText/WithLinks.pm Sat Jul 12 10:52:16 2008
@@ -36,10 +36,12 @@ sub configure {
$self->{unique_links} = 0;
+ $self->{anchor_links} = 1;
+
$self->{_link_track} = {};
foreach ( qw( before_link after_link footnote link_num_generator
- with_emphasis unique_links ) ) {
+ with_emphasis unique_links anchor_links ) ) {
$self->{ $_ } = $hash->{ $_ } if exists $hash->{ $_ };
delete $hash->{ $_ };
}
@@ -63,8 +65,11 @@ sub a_start {
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 = '';
+ }
if ( $href ) {
- if ($href !~ m#^http:|^mailto:#) {
+ if ($href !~ m#^https?:|^mailto:#o) {
$href = URI::WithBase->new($href, $self->{base})->abs();
}
if ($self->{unique_links})
@@ -340,6 +345,11 @@ If set to 1 then italicised text will be surrounded by
=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 1 then links pointing to local anchors will be skipped.
+The default behaviour is to include all links.
=back
--- t/15_parse_with_local_links.t.orig Sat Jul 12 11:00:51 2008
+++ t/15_parse_with_local_links.t Sat Jul 12 11:00:42 2008
@@ -0,0 +1,48 @@
+# $Id$
+
+use Test::More tests => 6;
+use HTML::FormatText::WithLinks;
+
+my $html = new_html();
+my $f = HTML::FormatText::WithLinks->new( leftmargin => 0 );
+
+ok($f, 'object created');
+
+my $text = $f->parse($html);
+
+my $correct_text = qq!This is a mail of some sort with a [1]link to a local anchor.
+
+
+
+1. #top
+
+
+!;
+
+ok($text, 'html formatted');
+is($text, $correct_text, 'html correctly formatted');
+
+sub new_html {
+return <<'HTML';
+<html>
+<body>
+<p>
+This is a mail of some sort with a <a href="#top">link to a local anchor</a>.
+</p>
+</body>
+</html>
+HTML
+}
+
+my $f2 = HTML::FormatText::WithLinks->new( leftmargin => 0, anchor_links => 0 );
+
+ok($f2, 'object created');
+
+my $text2 = $f2->parse($html);
+
+my $correct_text2 = qq!This is a mail of some sort with a link to a local anchor.
+
+!;
+
+ok($text2, 'html formatted');
+is($text2, $correct_text2, 'html correctly formatted');