remove debug
[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, $params{content})));
51         }
52
53         return "";
54 }
55
56 my $oldfile;
57 my $oldcontent;
58
59 sub sidebar_content ($) {
60         my $page=shift;
61         
62         return delete $pagesidebar{$page} if defined $pagesidebar{$page};
63
64         return if ! exists $pagesidebar{$page} && 
65                 defined $config{global_sidebars} && ! $config{global_sidebars};
66
67         my $sidebar_page=bestlink($page, "sidebar") || return;
68         my $sidebar_file=$pagesources{$sidebar_page} || return;
69         my $sidebar_type=pagetype($sidebar_file);
70         
71         if (defined $sidebar_type) {
72                 # FIXME: This isn't quite right; it won't take into account
73                 # adding a new sidebar page. So adding such a page
74                 # currently requires a wiki rebuild.
75                 add_depends($page, $sidebar_page);
76
77                 my $content;
78                 if (defined $oldfile && $sidebar_file eq $oldfile) {
79                         $content=$oldcontent;
80                 }
81                 else {
82                         $content=readfile(srcfile($sidebar_file));
83                         $oldcontent=$content;
84                         $oldfile=$sidebar_file;
85                 }
86
87                 return unless length $content;
88                 return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
89                        IkiWiki::linkify($sidebar_page, $page,
90                        IkiWiki::preprocess($sidebar_page, $page,
91                        IkiWiki::filter($sidebar_page, $page, $content))));
92         }
93
94 }
95
96 sub pagetemplate (@) {
97         my %params=@_;
98
99         my $template=$params{template};
100         if ($params{destpage} eq $params{page} &&
101             $template->query(name => "sidebar")) {
102                 my $content=sidebar_content($params{destpage});
103                 if (defined $content && length $content) {
104                         $template->param(sidebar => $content);
105                 }
106         }
107 }
108
109 1