summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 35418a3ba448aee3dd8191ab3e7f3ece876d0b9b (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::conditional;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use UNIVERSAL;
  7. # Globals used to pass information into the PageSpec functions.
  8. our ($sourcepage, $destpage);
  9. sub import { #{{{
  10. hook(type => "preprocess", id => "if", call => \&preprocess_if);
  11. } # }}}
  12. sub preprocess_if (@) { #{{{
  13. my %params=@_;
  14. if (! exists $params{test} || ! exists $params{then}) {
  15. return "[[if requires \"test\" and \"then\" parameters]]";
  16. }
  17. my $result=0;
  18. $sourcepage=$params{page};
  19. $destpage=$params{destpage};
  20. # An optimisation to avoid needless looping over every page
  21. # and adding of dependencies for simple uses of some of the
  22. # tests.
  23. if ($params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
  24. $result=eval "IkiWiki::PageSpec::match_$1(undef, ".
  25. IkiWiki::safequote($2).")";
  26. }
  27. else {
  28. add_depends($params{page}, $params{test});
  29. foreach my $page (keys %pagesources) {
  30. if (pagespec_match($page, $params{test}, $params{page})) {
  31. $result=1;
  32. last;
  33. }
  34. }
  35. }
  36. $sourcepage="";
  37. $destpage="";
  38. my $ret;
  39. if ($result) {
  40. $ret=$params{then};
  41. }
  42. elsif (exists $params{else}) {
  43. $ret=$params{else};
  44. }
  45. else {
  46. $ret="";
  47. }
  48. return IkiWiki::preprocess($params{page}, $params{destpage}, $ret);
  49. } # }}}
  50. package IkiWiki::PageSpec;
  51. sub match_enabled ($$) { #{{{
  52. shift;
  53. my $plugin=shift;
  54. # test if the plugin is enabled
  55. return UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import");
  56. } #}}}
  57. sub match_sourcepage ($$) { #{{{
  58. shift;
  59. my $glob=shift;
  60. return match_glob($IkiWiki::Plugin::conditional::sourcepage, $glob,
  61. $IkiWiki::Plugin::conditional::sourcepage);
  62. } #}}}
  63. sub match_destpage ($$) { #{{{
  64. shift;
  65. my $glob=shift;
  66. return match_glob($IkiWiki::Plugin::conditional::destpage, $glob,
  67. $IkiWiki::Plugin::conditional::sourcepage);
  68. } #}}}
  69. sub match_included ($$) { #{{{
  70. return $IkiWiki::Plugin::conditional::sourcepage ne $IkiWiki::Plugin::conditional::destpage;
  71. } #}}}
  72. 1