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