summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 293fbc1914597ef2364e81ad4978c381bf812f76 (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},
  49. IkiWiki::filter($params{page}, $ret));
  50. } # }}}
  51. package IkiWiki::PageSpec;
  52. sub match_enabled ($$) { #{{{
  53. shift;
  54. my $plugin=shift;
  55. # test if the plugin is enabled
  56. return UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import");
  57. } #}}}
  58. sub match_sourcepage ($$) { #{{{
  59. shift;
  60. my $glob=shift;
  61. return match_glob($IkiWiki::Plugin::conditional::sourcepage, $glob,
  62. $IkiWiki::Plugin::conditional::sourcepage);
  63. } #}}}
  64. sub match_destpage ($$) { #{{{
  65. shift;
  66. my $glob=shift;
  67. return match_glob($IkiWiki::Plugin::conditional::destpage, $glob,
  68. $IkiWiki::Plugin::conditional::sourcepage);
  69. } #}}}
  70. sub match_included ($$) { #{{{
  71. return $IkiWiki::Plugin::conditional::sourcepage ne $IkiWiki::Plugin::conditional::destpage;
  72. } #}}}
  73. 1