response
[ikiwiki.git] / IkiWiki / Plugin / sidebar.pm
blobc1146b7b4394c7203f452ef36606c5a51242ea14
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);
17 sub getsetup () {
18 return
19 plugin => {
20 safe => 1,
21 rebuild => 1,
23 global_sidebars => {
24 type => "boolean",
25 example => 1,
26 description => "show sidebar page on all pages?",
27 safe => 1,
28 rebuild => 1,
32 my %pagesidebar;
34 sub preprocess (@) {
35 my %params=@_;
37 my $page=$params{page};
38 return "" unless $page eq $params{destpage};
40 if (! defined $params{content}) {
41 $pagesidebar{$page}=undef;
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, $params{content})));
53 return "";
56 my $oldfile;
57 my $oldcontent;
59 sub sidebar_content ($) {
60 my $page=shift;
62 return delete $pagesidebar{$page} if defined $pagesidebar{$page};
64 return if ! exists $pagesidebar{$page} &&
65 defined $config{global_sidebars} && ! $config{global_sidebars};
67 my $sidebar_page=bestlink($page, "sidebar") || return;
68 my $sidebar_file=$pagesources{$sidebar_page} || return;
69 my $sidebar_type=pagetype($sidebar_file);
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);
77 my $content;
78 if (defined $oldfile && $sidebar_file eq $oldfile) {
79 $content=$oldcontent;
81 else {
82 $content=readfile(srcfile($sidebar_file));
83 $oldcontent=$content;
84 $oldfile=$sidebar_file;
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))));
96 sub pagetemplate (@) {
97 my %params=@_;
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);