force rebuild for theme change
[ikiwiki] / IkiWiki / Plugin / sidebar.pm
1 #!/usr/bin/perl
2 # Sidebar plugin.
3 # by Tuomo Valkonen <tuomov at iki dot fi>
4
5 package IkiWiki::Plugin::sidebar;
6
7 use warnings;
8 use strict;
9 use IkiWiki 3.00;
10
11 sub import {
12         hook(type => "getsetup", id => "sidebar", call => \&getsetup);
13         hook(type => "preprocess", id => "sidebar", call => \&preprocess);
14         hook(type => "pagetemplate", id => "sidebar", call => \&pagetemplate);
15 }
16
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 1,
22                 },
23                 global_sidebars => {
24                         type => "boolean",
25                         example => 1,
26                         description => "show sidebar page on all pages?",
27                         safe => 1,
28                         rebuild => 1,
29                 },
30 }
31
32 my %pagesidebar;
33
34 sub preprocess (@) {
35         my %params=@_;
36
37         my $page=$params{page};
38         return "" unless $page eq $params{destpage};
39         
40         if (! defined $params{content}) {
41                 $pagesidebar{$page}=undef;
42         }
43         else {
44                 my $file = $pagesources{$page};
45                 my $type = pagetype($file);
46
47                 $pagesidebar{$page}=
48                         IkiWiki::htmlize($page, $page, $type,
49                         IkiWiki::linkify($page, $page,
50                         IkiWiki::preprocess($page, $page,
51                         IkiWiki::filter($page, $page, $params{content}))));
52         }
53
54         return "";
55 }
56
57 my $oldfile;
58 my $oldcontent;
59
60 sub sidebar_content ($) {
61         my $page=shift;
62         
63         return delete $pagesidebar{$page} if defined $pagesidebar{$page};
64
65         return if ! exists $pagesidebar{$page} && 
66                 defined $config{global_sidebars} && ! $config{global_sidebars};
67
68         my $sidebar_page=bestlink($page, "sidebar") || return;
69         my $sidebar_file=$pagesources{$sidebar_page} || return;
70         my $sidebar_type=pagetype($sidebar_file);
71         
72         if (defined $sidebar_type) {
73                 # FIXME: This isn't quite right; it won't take into account
74                 # adding a new sidebar page. So adding such a page
75                 # currently requires a wiki rebuild.
76                 add_depends($page, $sidebar_page);
77
78                 my $content;
79                 if (defined $oldfile && $sidebar_file eq $oldfile) {
80                         $content=$oldcontent;
81                 }
82                 else {
83                         $content=readfile(srcfile($sidebar_file));
84                         $oldcontent=$content;
85                         $oldfile=$sidebar_file;
86                 }
87
88                 return unless length $content;
89                 return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
90                        IkiWiki::linkify($sidebar_page, $page,
91                        IkiWiki::preprocess($sidebar_page, $page,
92                        IkiWiki::filter($sidebar_page, $page, $content))));
93         }
94
95 }
96
97 sub pagetemplate (@) {
98         my %params=@_;
99
100         my $template=$params{template};
101         if ($params{destpage} eq $params{page} &&
102             $template->query(name => "sidebar")) {
103                 my $content=sidebar_content($params{destpage});
104                 if (defined $content && length $content) {
105                         $template->param(sidebar => $content);
106                 }
107         }
108 }
109
110 1