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