aboutsummaryrefslogtreecommitdiff
path: root/doc/plugins/contrib/siterel2pagerel.mdwn
blob: 9b09657bf0540aabc5ab4e5c8e3fcd575cde3a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[[!template id=plugin name=siterel2pagerel author="[[PaulWise]]"]]

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:

	#!/usr/bin/perl
	# quick HTML siterel2pagerel link hack by Paul Wise
	package IkiWiki::Plugin::siterel2pagerel;

	use warnings;
	use strict;
	use IkiWiki 2.00;

	sub import {
	        hook(type => "sanitize", id => "siterel2pagerel", call => \&siterel2pagerel);
	}

	sub siterel2pagerel (@) {
	        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;
	}

	1