summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/sidebar.pm
blob: 9697e1198d24db690cec373e9980516e077ad936 (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 2.00;
  8. sub import { #{{{
  9. hook(type => "getsetup", id => "sidebar", call => \&getsetup);
  10. hook(type => "pagetemplate", id => "sidebar", call => \&pagetemplate);
  11. } # }}}
  12. sub getsetup () { #{{{
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 1,
  17. },
  18. } #}}}
  19. sub sidebar_content ($) { #{{{
  20. my $page=shift;
  21. my $sidebar_page=bestlink($page, "sidebar") || return;
  22. my $sidebar_file=$pagesources{$sidebar_page} || return;
  23. my $sidebar_type=pagetype($sidebar_file);
  24. if (defined $sidebar_type) {
  25. # FIXME: This isn't quite right; it won't take into account
  26. # adding a new sidebar page. So adding such a page
  27. # currently requires a wiki rebuild.
  28. add_depends($page, $sidebar_page);
  29. my $content=readfile(srcfile($sidebar_file));
  30. return unless length $content;
  31. return IkiWiki::htmlize($sidebar_page, $page, $sidebar_type,
  32. IkiWiki::linkify($sidebar_page, $page,
  33. IkiWiki::preprocess($sidebar_page, $page,
  34. IkiWiki::filter($sidebar_page, $page, $content))));
  35. }
  36. } # }}}
  37. sub pagetemplate (@) { #{{{
  38. my %params=@_;
  39. my $page=$params{page};
  40. my $template=$params{template};
  41. if ($template->query(name => "sidebar")) {
  42. my $content=sidebar_content($page);
  43. if (defined $content && length $content) {
  44. $template->param(sidebar => $content);
  45. }
  46. }
  47. } # }}}
  48. 1