Skip Menu |

This queue is for tickets about the HTML-FormatText-WithLinks CPAN distribution.

Report information
The Basics
Id: 37634
Status: resolved
Priority: 0/
Queue: HTML-FormatText-WithLinks

People
Owner: struan [...] cpan.org
Requestors: janus [...] errornet.de
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



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');
Hi, Thanks for this, it's in the 0.10 release which is on its way to the CPAN now. Struan
From: janus [...] errornet.de
On Mon Jul 21 17:09:20 2008, STRUAN wrote: Show quoted text
> Hi, > > Thanks for this, it's in the 0.10 release which is on its way to the > CPAN now. > > Struan
Hi, looks like there went something wrong as the only addition in 0.10 was the format_tables thing. Anyway, i've attached a new patch that applies against the latest version. Regards, Simon
--- lib/HTML/FormatText/WithLinks.pm.orig Tue Jan 22 23:01:32 2008 +++ lib/HTML/FormatText/WithLinks.pm Wed Jul 30 11:30:00 2008 @@ -38,10 +38,12 @@ sub configure { $self->{format_tables} = 0; + $self->{anchor_links} = 1; + $self->{_link_track} = {}; foreach ( qw( before_link after_link footnote link_num_generator - with_emphasis unique_links format_tables ) ) { + with_emphasis unique_links format_tables anchor_links ) ) { $self->{ $_ } = $hash->{ $_ } if exists $hash->{ $_ }; delete $hash->{ $_ }; } @@ -65,8 +67,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}) @@ -390,6 +395,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 Wed Jul 30 11:30:27 2008 +++ t/15_parse_with_local_links.t Wed Jul 30 11:30:27 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');
Subject: Re: [rt.cpan.org #37634] Patch to skip anchor links
Date: Fri, 1 Aug 2008 14:13:36 +0100
To: Simon Dassow via RT <bug-HTML-FormatText-WithLinks [...] rt.cpan.org>
From: Struan Donald <struan [...] exo.org.uk>
* at 30/07 05:34 -0400 Simon Dassow via RT said: Show quoted text
> Queue: HTML-FormatText-WithLinks > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=37634 > > > On Mon Jul 21 17:09:20 2008, STRUAN wrote:
> > Hi, > > > > Thanks for this, it's in the 0.10 release which is on its way to the > > CPAN now. > > > > Struan
> > Hi, > looks like there went something wrong as the only addition in 0.10 was > the format_tables thing. > Anyway, i've attached a new patch that applies against the latest version.
Indeed it did. Apparently my release management procedure majors on the release bit and not the management bit :( However, I've now released, and checked, 0.11 and it does have your changes in. The format_tables thing was a failed experiment that shouldn't have seen the light of day. Struan
Closing bug.