summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: a7ed6590e52c26a6ab28b037c30e441dd73597f5 (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. if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
  56. return IkiWiki::SuccessReason->new("$plugin is enabled");
  57. }
  58. else {
  59. return IkiWiki::FailReason->new("$plugin is not enabled");
  60. }
  61. } #}}}
  62. sub match_sourcepage ($$;@) { #{{{
  63. shift;
  64. my $glob=shift;
  65. my %params=@_;
  66. return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
  67. if (match_glob($params{sourcepage}, $glob, @_)) {
  68. return IkiWiki::SuccessReason->new("sourcepage matches $glob");
  69. }
  70. else {
  71. return IkiWiki::FailReason->new("sourcepage does not match $glob");
  72. }
  73. } #}}}
  74. sub match_destpage ($$;@) { #{{{
  75. shift;
  76. my $glob=shift;
  77. my %params=@_;
  78. return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
  79. if (match_glob($params{destpage}, $glob, @_)) {
  80. return IkiWiki::SuccessReason->new("destpage matches $glob");
  81. }
  82. else {
  83. return IkiWiki::FailReason->new("destpage does not match $glob");
  84. }
  85. } #}}}
  86. sub match_included ($$;$) { #{{{
  87. shift;
  88. shift;
  89. my %params=@_;
  90. return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
  91. if ($params{sourcepage} ne $params{destpage}) {
  92. return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
  93. }
  94. else {
  95. return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
  96. }
  97. } #}}}
  98. 1