ikiwiki/doc/plugins/contrib/siterel2pagerel.mdwn

31 lines
1.1 KiB
Plaintext
Raw Normal View History

[[!template id=plugin name=siterel2pagerel author="[[PaulWise]]"]]
2008-02-11 05:00:30 +01:00
This is a simple plugin to convert all site-relative links to page-relative
links (converts /foo into ../../../foo or similar). It works as a
postprocessing filter, allowing it to work on mdwn, wiki, html, rst and any
other format that produces html. The code is available here:
2007-08-09 08:42:36 +02:00
#!/usr/bin/perl
2007-08-09 09:03:38 +02:00
# quick HTML siterel2pagerel link hack by Paul Wise
2007-08-09 08:42:36 +02:00
package IkiWiki::Plugin::siterel2pagerel;
use warnings;
use strict;
use IkiWiki 2.00;
sub import {
2007-08-09 08:42:36 +02:00
hook(type => "sanitize", id => "siterel2pagerel", call => \&siterel2pagerel);
}
2007-08-09 08:42:36 +02:00
sub siterel2pagerel (@) {
2007-08-09 08:42:36 +02:00
my %params=@_;
my $baseurl=IkiWiki::baseurl($params{page});
my $content=$params{content};
$content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"\/([^"]*)"/$1 href="$baseurl$2"/mig;
$content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"\/([^"]*)"/$1 src="$baseurl$2"/mig;
# FIXME: do <script and everything else that can have URLs in it
return $content;
}
2007-08-09 08:42:36 +02:00
1