]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/plugins/contrib/siterel2pagerel.mdwn
automated tests added (and they highlight that the plugin should
[git.ikiwiki.info.git] / doc / plugins / contrib / siterel2pagerel.mdwn
1 [[!template id=plugin name=siterel2pagerel author="[[PaulWise]]"]]
3 This is a simple plugin to convert all site-relative links to page-relative
4 links (converts /foo into ../../../foo or similar). It works as a
5 postprocessing filter, allowing it to work on mdwn, wiki, html, rst and any
6 other format that produces html. The code is available here:
8         #!/usr/bin/perl
9         # quick HTML siterel2pagerel link hack by Paul Wise
10         package IkiWiki::Plugin::siterel2pagerel;
12         use warnings;
13         use strict;
14         use IkiWiki 2.00;
16         sub import {
17                 hook(type => "sanitize", id => "siterel2pagerel", call => \&siterel2pagerel);
18         }
20         sub siterel2pagerel (@) {
21                 my %params=@_;
22                 my $baseurl=IkiWiki::baseurl($params{page});
23                 my $content=$params{content};
24                 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"\/([^"]*)"/$1 href="$baseurl$2"/mig;
25                 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"\/([^"]*)"/$1 src="$baseurl$2"/mig;
26                 # FIXME: do <script and everything else that can have URLs in it
27                 return $content;
28         }
30         1