summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/sidebar.pm
blob: 2d495db2cc0d02e1bc1abbc3653627374a8179ba (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,
  42. IkiWiki::filter($page, $page, $params{content}))));
  43. }
  44. return "";
  45. }
  46. my $oldfile;
  47. my $oldcontent;
  48. sub sidebar_content ($) {
  49. my $page=shift;
  50. return delete $pagesidebar{$page} if defined $pagesidebar{$page};
  51. return if ! exists $pagesidebar{$page} &&
  52. defined $config{global_sidebars} && ! $config{global_sidebars};
  53. my $sidebar_page=bestlink($page, "sidebar") || return;
  54. my $sidebar_file=$pagesources{$sidebar_page} || return;
  55. my $sidebar_type=pagetype($sidebar_file);
  56. if (defined $sidebar_type) {
  57. # FIXME: This isn't quite right; it won't take into account
  58. # adding a new sidebar page. So adding such a page
  59. # currently requires a wiki rebuild.
  60. add_depends($page, $sidebar_page);
  61. my $content;
  62. if (defined $oldfile && $sidebar_file eq $oldfile) {
  63. $content=$oldcontent;
  64. }
  65. else {
  66. $content=readfile(srcfile($sidebar_file));
  67. $oldcontent=$content;
  68. $oldfile=$sidebar_file;
  69. }
  70. return unless length $content;
  71. return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
  72. IkiWiki::linkify($sidebar_page, $page,
  73. IkiWiki::preprocess($sidebar_page, $page,
  74. IkiWiki::filter($sidebar_page, $page, $content))));
  75. }
  76. }
  77. sub pagetemplate (@) {
  78. my %params=@_;
  79. my $template=$params{template};
  80. if ($params{destpage} eq $params{page} &&
  81. $template->query(name => "sidebar")) {
  82. my $content=sidebar_content($params{destpage});
  83. if (defined $content && length $content) {
  84. $template->param(sidebar => $content);
  85. }
  86. }
  87. }
  88. 1