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