summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 29223ace24ef419bfb23bdd468a061bdebfdac19 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::conditional;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. use UNIVERSAL;
  7. sub import { #{{{
  8. hook(type => "preprocess", id => "if", call => \&preprocess_if);
  9. } # }}}
  10. sub preprocess_if (@) { #{{{
  11. my %params=@_;
  12. if (! exists $params{test} || ! exists $params{then}) {
  13. return "[[if ".gettext('"test" and "then" parameters are required')."]]";
  14. }
  15. my $result=0;
  16. # An optimisation to avoid needless looping over every page
  17. # and adding of dependencies for simple uses of some of the
  18. # tests.
  19. if ($params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
  20. $result=pagespec_match($params{page}, $params{test},
  21. location => $params{page},
  22. sourcepage => $params{page},
  23. destpage => $params{destpage});
  24. }
  25. else {
  26. add_depends($params{page}, $params{test});
  27. foreach my $page (keys %pagesources) {
  28. if (pagespec_match($page, $params{test},
  29. location => $params{page},
  30. sourcepage => $params{page},
  31. destpage => $params{destpage})) {
  32. $result=1;
  33. last;
  34. }
  35. }
  36. }
  37. my $ret;
  38. if ($result) {
  39. $ret=$params{then};
  40. }
  41. elsif (exists $params{else}) {
  42. $ret=$params{else};
  43. }
  44. else {
  45. $ret="";
  46. }
  47. return IkiWiki::preprocess($params{page}, $params{destpage},
  48. IkiWiki::filter($params{page}, $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. my %params=@_;
  61. return unless exists $params{sourcepage};
  62. return match_glob($params{sourcepage}, $glob, @_);
  63. } #}}}
  64. sub match_destpage ($$;@) { #{{{
  65. shift;
  66. my $glob=shift;
  67. my %params=@_;
  68. return unless exists $params{destpage};
  69. return match_glob($params{destpage}, $glob, @_);
  70. } #}}}
  71. sub match_included ($$;$) { #{{{
  72. shift;
  73. shift;
  74. my %params=@_;
  75. return unless exists $params{sourcepage} && exists $params{destpage};
  76. return $params{sourcepage} ne $params{destpage};
  77. } #}}}
  78. 1