From: Manoj Srivastava <srivasta@acm.org>
Date: Wed, 2 Apr 2008 17:01:25 +0000 (-0500)
Subject: Bug#473987: [PATCH] Links relative to baseurl mangled in atom/rss feeds
X-Git-Tag: 2.42~31
X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/commitdiff_plain/c207086282b2f058f647b7fa810f3da54fe5fe4b?ds=inline;hp=-c

Bug#473987: [PATCH] Links relative to baseurl mangled in atom/rss feeds

tag 473987 +patch
thanks
Hi,

The issue is that we need to convert relative links to absolute
ones for atom and rss feeds -- but there are two types of
relative links. The first kind, relative to the current
document ( href="some/path") is handled correctly. The second
kind of relative url is is relative to the http server
base (href="/semi-abs/path"), and that broke.

It broke because we just prepended the url of the current
document to the href (http://host/path/to/this-doc/ + link),
which gave us, in the first place:
 http://host/path/to/this-doc/some/path        [correct], and
 http://host/path/to/this-doc//semi-abs/path   [wrong]

The fix is to calculate the base for the http server (the base of
the wiki does not help, since the base of the wiki can be
different from the base of the http server -- I have, for example,
"url => http://host.name.mine/blog/manoj/"), and prepend that to
the relative references that start with a /.

This has been tested.

Signed-off-by: Manoj Srivastava <srivasta@debian.org>
---

c207086282b2f058f647b7fa810f3da54fe5fe4b
diff --git a/IkiWiki/Plugin/inline.pm b/IkiWiki/Plugin/inline.pm
index c734bd9a6..8f7cd826e 100644
--- a/IkiWiki/Plugin/inline.pm
+++ b/IkiWiki/Plugin/inline.pm
@@ -388,9 +388,18 @@ sub absolute_urls ($$) { #{{{
 
 	my $url=$baseurl;
 	$url=~s/[^\/]+$//;
-	
+
+        # what is the non path part of the url? (need it for relative url's starting with /
+        my $top_uri = URI->new($url);
+        $top_uri->path_query("/"); # reset the path
+        my $urltop = $top_uri->as_string;
+        $urltop=~s/\/*$//;
+
 	$content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
-	$content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^"]+)"/$1 href="$url$2"/mig;
+        # Relative URL
+	$content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
+        # relative to the top of the site
+	$content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
 	$content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^"]+)"/$1 src="$url$2"/mig;
 	return $content;
 } #}}}