summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 2a25135fe31757ed06c21f211bab6a7d2ea79414 (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. foreach my $param (qw{test then}) {
  13. if (! exists $params{$param}) {
  14. return "[[if ".sprintf(gettext('%s parameter is required'), $param)."]]";
  15. }
  16. }
  17. my $result=0;
  18. if ((exists $params{all} && lc $params{all} eq "no") ||
  19. # An optimisation to avoid needless looping over every page
  20. # and adding of dependencies for simple uses of some of the
  21. # tests.
  22. $params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
  23. add_depends($params{page}, "$params{test} and $params{page}");
  24. $result=pagespec_match($params{page}, $params{test},
  25. location => $params{page},
  26. sourcepage => $params{page},
  27. destpage => $params{destpage});
  28. }
  29. else {
  30. add_depends($params{page}, $params{test});
  31. foreach my $page (keys %pagesources) {
  32. if (pagespec_match($page, $params{test},
  33. location => $params{page},
  34. sourcepage => $params{page},
  35. destpage => $params{destpage})) {
  36. $result=1;
  37. last;
  38. }
  39. }
  40. }
  41. my $ret;
  42. if ($result) {
  43. $ret=$params{then};
  44. }
  45. elsif (exists $params{else}) {
  46. $ret=$params{else};
  47. }
  48. else {
  49. $ret="";
  50. }
  51. return IkiWiki::preprocess($params{page}, $params{destpage},
  52. IkiWiki::filter($params{page}, $params{destpage}, $ret));
  53. } # }}}
  54. package IkiWiki::PageSpec;
  55. sub match_enabled ($$;@) { #{{{
  56. shift;
  57. my $plugin=shift;
  58. # test if the plugin is enabled
  59. if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
  60. return IkiWiki::SuccessReason->new("$plugin is enabled");
  61. }
  62. else {
  63. return IkiWiki::FailReason->new("$plugin is not enabled");
  64. }
  65. } #}}}
  66. sub match_sourcepage ($$;@) { #{{{
  67. shift;
  68. my $glob=shift;
  69. my %params=@_;
  70. return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
  71. if (match_glob($params{sourcepage}, $glob, @_)) {
  72. return IkiWiki::SuccessReason->new("sourcepage matches $glob");
  73. }
  74. else {
  75. return IkiWiki::FailReason->new("sourcepage does not match $glob");
  76. }
  77. } #}}}
  78. sub match_destpage ($$;@) { #{{{
  79. shift;
  80. my $glob=shift;
  81. my %params=@_;
  82. return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
  83. if (match_glob($params{destpage}, $glob, @_)) {
  84. return IkiWiki::SuccessReason->new("destpage matches $glob");
  85. }
  86. else {
  87. return IkiWiki::FailReason->new("destpage does not match $glob");
  88. }
  89. } #}}}
  90. sub match_included ($$;@) { #{{{
  91. shift;
  92. shift;
  93. my %params=@_;
  94. return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
  95. if ($params{sourcepage} ne $params{destpage}) {
  96. return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
  97. }
  98. else {
  99. return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
  100. }
  101. } #}}}
  102. 1