]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/sidebar.pm
Merge remote branch 'davrieb/autotag' into autotag
[git.ikiwiki.info.git] / IkiWiki / Plugin / sidebar.pm
1 #!/usr/bin/perl
2 # Sidebar plugin.
3 # by Tuomo Valkonen <tuomov at iki dot fi>
5 package IkiWiki::Plugin::sidebar;
7 use warnings;
8 use strict;
9 use IkiWiki 3.00;
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 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 1,
22                 },
23                 global_sidebars => {
24                         type => "boolean",
25                         examples => 1,
26                         description => "show sidebar page on all pages?",
27                         safe => 1,
28                         rebuild => 1,
29                 },
30 }
32 my %pagesidebar;
34 sub preprocess (@) {
35         my %params=@_;
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);
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         }
54         return "";
55 }
57 my $oldfile;
58 my $oldcontent;
60 sub sidebar_content ($) {
61         my $page=shift;
62         
63         return delete $pagesidebar{$page} if defined $pagesidebar{$page};
65         return if ! exists $pagesidebar{$page} && 
66                 defined $config{global_sidebars} && ! $config{global_sidebars};
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);
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                 }
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         }
95 }
97 sub pagetemplate (@) {
98         my %params=@_;
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         }