summaryrefslogtreecommitdiff
path: root/perl/IkiWiki/Plugin/farbar.pm
blob: 37f10139f2ebea3815e182494b6ac17602b850a9 (plain)
  1. #!/usr/bin/perl
  2. # Farbar 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::farbar;
  6. use warnings;
  7. use strict;
  8. use IkiWiki 3.00;
  9. sub import {
  10. hook(type => "getsetup", id => "farbar", call => \&getsetup);
  11. hook(type => "preprocess", id => "farbar", call => \&preprocess);
  12. hook(type => "pagetemplate", id => "farbar", call => \&pagetemplate);
  13. }
  14. sub getsetup () {
  15. return
  16. plugin => {
  17. safe => 1,
  18. rebuild => 1,
  19. },
  20. global_farbars => {
  21. type => "boolean",
  22. example => 1,
  23. description => "show farbar page on all pages?",
  24. safe => 1,
  25. rebuild => 1,
  26. },
  27. }
  28. my %pagefarbar;
  29. sub preprocess (@) {
  30. my %params=@_;
  31. my $page=$params{page};
  32. return "" unless $page eq $params{destpage};
  33. if (! defined $params{content}) {
  34. $pagefarbar{$page}=undef;
  35. }
  36. else {
  37. my $file = $pagesources{$page};
  38. my $type = pagetype($file);
  39. $pagefarbar{$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 farbar_content ($) {
  49. my $page=shift;
  50. return delete $pagefarbar{$page} if defined $pagefarbar{$page};
  51. return if ! exists $pagefarbar{$page} &&
  52. defined $config{global_farbars} && ! $config{global_farbars};
  53. my $farbar_page=bestlink($page, "farbar") || return;
  54. my $farbar_file=$pagesources{$farbar_page} || return;
  55. my $farbar_type=pagetype($farbar_file);
  56. if (defined $farbar_type) {
  57. # FIXME: This isn't quite right; it won't take into account
  58. # adding a new farbar page. So adding such a page
  59. # currently requires a wiki rebuild.
  60. add_depends($page, $farbar_page);
  61. my $content;
  62. if (defined $oldfile && $farbar_file eq $oldfile) {
  63. $content=$oldcontent;
  64. }
  65. else {
  66. $content=readfile(srcfile($farbar_file));
  67. $oldcontent=$content;
  68. $oldfile=$farbar_file;
  69. }
  70. return unless length $content;
  71. return IkiWiki::htmlize($farbar_page, $page, $farbar_type,
  72. IkiWiki::linkify($farbar_page, $page,
  73. IkiWiki::preprocess($farbar_page, $page,
  74. IkiWiki::filter($farbar_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 => "farbar")) {
  82. my $content=farbar_content($params{destpage});
  83. if (defined $content && length $content) {
  84. $template->param(farbar => $content);
  85. }
  86. }
  87. }
  88. 1