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