aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/sidebar.pm
blob: c1146b7b4394c7203f452ef36606c5a51242ea14 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl
# Sidebar plugin.
# by Tuomo Valkonen <tuomov at iki dot fi>

package IkiWiki::Plugin::sidebar;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type => "getsetup", id => "sidebar", call => \&getsetup);
	hook(type => "preprocess", id => "sidebar", call => \&preprocess);
	hook(type => "pagetemplate", id => "sidebar", call => \&pagetemplate);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => 1,
		},
		global_sidebars => {
			type => "boolean",
			example => 1,
			description => "show sidebar page on all pages?",
			safe => 1,
			rebuild => 1,
		},
}

my %pagesidebar;

sub preprocess (@) {
	my %params=@_;

	my $page=$params{page};
	return "" unless $page eq $params{destpage};
	
	if (! defined $params{content}) {
		$pagesidebar{$page}=undef;
	}
	else {
		my $file = $pagesources{$page};
		my $type = pagetype($file);

		$pagesidebar{$page}=
			IkiWiki::htmlize($page, $page, $type,
			IkiWiki::linkify($page, $page,
			IkiWiki::preprocess($page, $page, $params{content})));
	}

	return "";
}

my $oldfile;
my $oldcontent;

sub sidebar_content ($) {
	my $page=shift;
	
	return delete $pagesidebar{$page} if defined $pagesidebar{$page};

	return if ! exists $pagesidebar{$page} && 
		defined $config{global_sidebars} && ! $config{global_sidebars};

	my $sidebar_page=bestlink($page, "sidebar") || return;
	my $sidebar_file=$pagesources{$sidebar_page} || return;
	my $sidebar_type=pagetype($sidebar_file);
	
	if (defined $sidebar_type) {
		# FIXME: This isn't quite right; it won't take into account
		# adding a new sidebar page. So adding such a page
		# currently requires a wiki rebuild.
		add_depends($page, $sidebar_page);

		my $content;
		if (defined $oldfile && $sidebar_file eq $oldfile) {
			$content=$oldcontent;
		}
		else {
			$content=readfile(srcfile($sidebar_file));
			$oldcontent=$content;
			$oldfile=$sidebar_file;
		}

		return unless length $content;
		return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
		       IkiWiki::linkify($sidebar_page, $page,
		       IkiWiki::preprocess($sidebar_page, $page,
		       IkiWiki::filter($sidebar_page, $page, $content))));
	}

}

sub pagetemplate (@) {
	my %params=@_;

	my $template=$params{template};
	if ($params{destpage} eq $params{page} &&
	    $template->query(name => "sidebar")) {
		my $content=sidebar_content($params{destpage});
		if (defined $content && length $content) {
		        $template->param(sidebar => $content);
		}
	}
}

1