summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/sidebar.pm
blob: c1146b7b4394c7203f452ef36606c5a51242ea14 (plain)
  1. #!/usr/bin/perl
  2. # Sidebar plugin.
  3. # by Tuomo Valkonen <tuomov at iki dot fi>
  4. package IkiWiki::Plugin::sidebar;
  5. use warnings;
  6. use strict;
  7. use IkiWiki 3.00;
  8. sub import {
  9. hook(type => "getsetup", id => "sidebar", call => \&getsetup);
  10. hook(type => "preprocess", id => "sidebar", call => \&preprocess);
  11. hook(type => "pagetemplate", id => "sidebar", call => \&pagetemplate);
  12. }
  13. sub getsetup () {
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => 1,
  18. },
  19. global_sidebars => {
  20. type => "boolean",
  21. example => 1,
  22. description => "show sidebar page on all pages?",
  23. safe => 1,
  24. rebuild => 1,
  25. },
  26. }
  27. my %pagesidebar;
  28. sub preprocess (@) {
  29. my %params=@_;
  30. my $page=$params{page};
  31. return "" unless $page eq $params{destpage};
  32. if (! defined $params{content}) {
  33. $pagesidebar{$page}=undef;
  34. }
  35. else {
  36. my $file = $pagesources{$page};
  37. my $type = pagetype($file);
  38. $pagesidebar{$page}=
  39. IkiWiki::htmlize($page, $page, $type,
  40. IkiWiki::linkify($page, $page,
  41. IkiWiki::preprocess($page, $page, $params{content})));
  42. }
  43. return "";
  44. }
  45. my $oldfile;
  46. my $oldcontent;
  47. sub sidebar_content ($) {
  48. my $page=shift;
  49. return delete $pagesidebar{$page} if defined $pagesidebar{$page};
  50. return if ! exists $pagesidebar{$page} &&
  51. defined $config{global_sidebars} && ! $config{global_sidebars};
  52. my $sidebar_page=bestlink($page, "sidebar") || return;
  53. my $sidebar_file=$pagesources{$sidebar_page} || return;
  54. my $sidebar_type=pagetype($sidebar_file);
  55. if (defined $sidebar_type) {
  56. # FIXME: This isn't quite right; it won't take into account
  57. # adding a new sidebar page. So adding such a page
  58. # currently requires a wiki rebuild.
  59. add_depends($page, $sidebar_page);
  60. my $content;
  61. if (defined $oldfile && $sidebar_file eq $oldfile) {
  62. $content=$oldcontent;
  63. }
  64. else {
  65. $content=readfile(srcfile($sidebar_file));
  66. $oldcontent=$content;
  67. $oldfile=$sidebar_file;
  68. }
  69. return unless length $content;
  70. return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
  71. IkiWiki::linkify($sidebar_page, $page,
  72. IkiWiki::preprocess($sidebar_page, $page,
  73. IkiWiki::filter($sidebar_page, $page, $content))));
  74. }
  75. }
  76. sub pagetemplate (@) {
  77. my %params=@_;
  78. my $template=$params{template};
  79. if ($params{destpage} eq $params{page} &&
  80. $template->query(name => "sidebar")) {
  81. my $content=sidebar_content($params{destpage});
  82. if (defined $content && length $content) {
  83. $template->param(sidebar => $content);
  84. }
  85. }
  86. }
  87. 1