]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/sidebar.pm
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[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=@_;
36         my $content=shift;
37         shift;
39         my $page=$params{page};
40         return "" unless $page eq $params{destpage};
41         
42         if (! defined $content) {
43                 $pagesidebar{$page}=undef;
44         }
45         else {
46                 my $file = $pagesources{$page};
47                 my $type = pagetype($file);
49                 $pagesidebar{$page}=
50                         IkiWiki::htmlize($page, $page, $type,
51                         IkiWiki::linkify($page, $page,
52                         IkiWiki::preprocess($page, $page,
53                         IkiWiki::filter($page, $page, $content))));
54         }
56         return "";
57 }
59 my $oldfile;
60 my $oldcontent;
62 sub sidebar_content ($) {
63         my $page=shift;
64         
65         return $pagesidebar{$page} if defined $pagesidebar{$page};
67         return if ! exists $pagesidebar{$page} && 
68                 defined $config{global_sidebars} && ! $config{global_sidebars};
70         my $sidebar_page=bestlink($page, "sidebar") || return;
71         my $sidebar_file=$pagesources{$sidebar_page} || return;
72         my $sidebar_type=pagetype($sidebar_file);
73         
74         if (defined $sidebar_type) {
75                 # FIXME: This isn't quite right; it won't take into account
76                 # adding a new sidebar page. So adding such a page
77                 # currently requires a wiki rebuild.
78                 add_depends($page, $sidebar_page);
80                 my $content;
81                 if (defined $oldfile && $sidebar_file eq $oldfile) {
82                         $content=$oldcontent;
83                 }
84                 else {
85                         $content=readfile(srcfile($sidebar_file));
86                         $oldcontent=$content;
87                         $oldfile=$sidebar_file;
88                 }
90                 return unless length $content;
91                 return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
92                        IkiWiki::linkify($sidebar_page, $page,
93                        IkiWiki::preprocess($sidebar_page, $page,
94                        IkiWiki::filter($sidebar_page, $page, $content))));
95         }
97 }
99 sub pagetemplate (@) {
100         my %params=@_;
102         my $page=$params{page};
103         my $template=$params{template};
104         
105         if ($template->query(name => "sidebar")) {
106                 my $content=sidebar_content($page);
107                 if (defined $content && length $content) {
108                         $template->param(sidebar => $content);
109                 }
110         }