summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/theme.pm
blob: ee94547e90c51dad608b020e1be6bc0a434d38d7 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::theme;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. sub import {
  7. hook(type => "getsetup", id => "theme", call => \&getsetup);
  8. hook(type => "checkconfig", id => "theme", call => \&checkconfig);
  9. hook(type => "needsbuild", id => "theme", call => \&needsbuild);
  10. }
  11. sub getsetup () {
  12. return
  13. plugin => {
  14. safe => 1,
  15. rebuild => 0,
  16. section => "web",
  17. },
  18. theme => {
  19. type => "string",
  20. example => "actiontabs",
  21. description => "name of theme to enable",
  22. safe => 1,
  23. rebuild => 0,
  24. },
  25. }
  26. my $added=0;
  27. sub checkconfig () {
  28. if (! $added && exists $config{theme} && $config{theme} =~ /^\w+$/) {
  29. add_underlay("themes/".$config{theme});
  30. $added=1;
  31. }
  32. }
  33. sub needsbuild ($) {
  34. my $needsbuild=shift;
  35. if (($config{theme} || '') ne ($wikistate{theme}{currenttheme} || '')) {
  36. # theme changed; ensure all files in the theme are built
  37. my %needsbuild=map { $_ => 1 } @$needsbuild;
  38. if ($config{theme}) {
  39. foreach my $file (glob("$config{underlaydirbase}/themes/$config{theme}/*")) {
  40. if (-f $file) {
  41. my $f=IkiWiki::basename($file);
  42. push @$needsbuild, $f
  43. unless $needsbuild{$f};
  44. }
  45. }
  46. }
  47. elsif ($wikistate{theme}{currenttheme}) {
  48. foreach my $file (glob("$config{underlaydirbase}/themes/$wikistate{theme}{currenttheme}/*")) {
  49. my $f=IkiWiki::basename($file);
  50. if (-f $file && defined eval { srcfile($f) }) {
  51. push @$needsbuild, $f;
  52. }
  53. }
  54. }
  55. $wikistate{theme}{currenttheme}=$config{theme};
  56. }
  57. return $needsbuild;
  58. }
  59. 1