init from git://atelier.gresille.org/gresille-ikiwiki
[ikiwiki/sidebar2.git] / sidebar2.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::sidebar2;
3
4 =head1 NAME
5
6 IkiWiki::Plugin::sidebar2 - Improved version of IkiWiki::Plugin::sidebar
7
8 =head1 VERSION
9
10 This describes version B<0.1> of IkiWiki::Plugin::sidebar2
11
12 =cut
13
14 our $VERSION = '0.1';
15
16 =head1 DESCRIPTION
17
18 Improved version of IkiWiki::Plugin::sidebar2. Main features are:
19
20 - allowing several sidebars;
21 - enabling sidebars using pagespects.
22
23 See doc/plugins/sidebar2.mdwn for documentation.
24
25 =head1 PREREQUISITES
26
27 IkiWiki
28
29 =head1 URL
30
31 http://atelier.gresille.org/projects/gresille-ikiwiki/wiki/Sidebar2
32 http://ikiwiki.info/plugins/contrib/sidebar2/
33
34 =head1 AUTHOR
35
36 Tuomo Valkonen wrote the original Ikiwiki::plugin::sidebar.
37 Others (on http://ikiwiki.info) helped to improve it.
38 Louis Paternault (spalax) <spalax at gresille dot org> improved it to write Ikiwiki::plugin::sidebar2.
39
40 =head1 COPYRIGHT
41
42 Copyright 2006 Tuomo Valkonen <tuomov at iki dot fi>
43 Copyright 2013 by Louis Paternault <spalax at gresille dot org>
44
45 This program is free software; you can redistribute it and/or modify
46 it under the terms of the GNU General Public License as published by
47 the Free Software Foundation; either version 2 of the License, or
48 (at your option) any later version.
49
50 This program is distributed in the hope that it will be useful,
51 but WITHOUT ANY WARRANTY; without even the implied warranty of
52 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 GNU General Public License for more details.
54
55 You should have received a copy of the GNU General Public License
56 along with this program; if not, write to the Free Software
57 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
58
59 =cut
60
61 use warnings;
62 use strict;
63 use IkiWiki 3.00;
64
65 sub import {
66 hook(type => "checkconfig", id => "sidebar2", call => \&checkconfig);
67 hook(type => "getsetup", id => "sidebar2", call => \&getsetup);
68 hook(type => "preprocess", id => "sidebar", call => \&preprocess);
69 hook(type => "pagetemplate", id => "sidebar2", call => \&pagetemplate);
70 }
71
72 sub checkconfig () {
73 # Parsing "sidebars"
74 my %sidebars;
75 if (defined $config{global_sidebars} and (ref($config{global_sidebars}) eq "ARRAY")) {
76 my $length = $#{$config{global_sidebars}}+1;
77 if (($length % 3) != 0) {
78 error("'sidebars' length must be a multiple of 3.");
79 }
80 for(my $i=0; $i<$length/3;$i += 1) {
81 unless(exists($sidebars{$config{global_sidebars}[3*$i]})) {
82 $sidebars{$config{global_sidebars}[3*$i]} = ();
83 }
84 push(
85 @{$sidebars{$config{global_sidebars}[3*$i]}},
86 @{[[
87 $config{global_sidebars}[3*$i+1],
88 $config{global_sidebars}[3*$i+2],
89 ]]}
90 );
91 }
92 } else {
93 if (not defined $config{global_sidebars}) {
94 $config{global_sidebars} = 1;
95 }
96 if (IkiWiki::yesno($config{global_sidebars})) {
97 %sidebars = (
98 "sidebar" => [["sidebar", "*"]]
99 );
100 }
101 }
102
103 @{$config{sidebars}} = %sidebars;
104 }
105
106 sub getsetup () {
107 return
108 plugin => {
109 safe => 1,
110 rebuild => 1,
111 },
112 global_sidebars => {
113 type => "boolean",
114 example => 1,
115 description => "show sidebar page on all pages?",
116 safe => 1,
117 rebuild => 1,
118 },
119 }
120
121 my %pagesidebar;
122
123 sub preprocess (@) {
124 my %params=@_;
125
126 my $page=$params{page};
127 return "" unless $page eq $params{destpage};
128
129 if (! defined $params{var}) {
130 $params{var} = "sidebar";
131 }
132 if (! defined $params{content}) {
133 $pagesidebar{$page}{$params{var}}=undef;
134 }
135 else {
136 my $file = $pagesources{$page};
137 my $type = pagetype($file);
138
139 unless(exists($pagesidebar{$page})) {
140 $pagesidebar{$page} = ();
141 }
142 $pagesidebar{$page}{$params{var}} = IkiWiki::htmlize($page, $page, $type,
143 IkiWiki::linkify($page, $page,
144 IkiWiki::preprocess($page, $page, $params{content})));
145 }
146
147 return "";
148 }
149
150 my $oldfile;
151 my $oldcontent;
152
153 sub sidebar_content ($$$$) {
154 my $templatevar=shift;
155 my $page=shift;
156 my $included=shift;
157 my $pagespec=shift;
158
159 return delete $pagesidebar{$page}{$templatevar} if defined $pagesidebar{$page}{$templatevar};
160
161 return if ! exists $pagesidebar{$page}{$templatevar} &&
162 ! pagespec_match($page, $pagespec)
163 ;
164
165 my $sidebar_page=bestlink($page, $included) || return;
166 my $sidebar_file=$pagesources{$sidebar_page} || return;
167 my $sidebar_type=pagetype($sidebar_file);
168
169 if (defined $sidebar_type) {
170 # FIXME: This isn't quite right; it won't take into account
171 # adding a new sidebar page. So adding such a page
172 # currently requires a wiki rebuild.
173 add_depends($page, $sidebar_page);
174
175 my $content;
176 if (defined $oldfile && $sidebar_file eq $oldfile) {
177 $content=$oldcontent;
178 }
179 else {
180 $content=readfile(srcfile($sidebar_file));
181 $oldcontent=$content;
182 $oldfile=$sidebar_file;
183 }
184
185 return unless length $content;
186 return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
187 IkiWiki::linkify($sidebar_page, $page,
188 IkiWiki::preprocess($sidebar_page, $page,
189 IkiWiki::filter($sidebar_page, $page, $content))));
190 }
191
192 }
193
194 sub pagetemplate (@) {
195 my %params=@_;
196
197 my $template=$params{template};
198 my %sidebars = @{$config{sidebars}};
199 if ($params{destpage} eq $params{page}) {
200 foreach my $templatevar (keys(%sidebars)) {
201 if ($template->query(name => $templatevar) and exists($sidebars{$templatevar})) {
202 for my $data (@{$sidebars{$templatevar}}) {
203 my $content=sidebar_content($templatevar, $params{destpage}, @{$data}[0], @{$data}[1]);
204 if (defined $content && length $content) {
205 $template->param($templatevar => $content);
206 last;
207 }
208 }
209 }
210 }
211 }
212 }
213
214 1