summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: aad617812fc69229aa9ffa6979570d078f090023 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::conditional;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. use UNIVERSAL;
  7. sub import {
  8. hook(type => "getsetup", id => "conditional", call => \&getsetup);
  9. hook(type => "preprocess", id => "if", call => \&preprocess_if);
  10. }
  11. sub getsetup {
  12. return
  13. plugin => {
  14. safe => 1,
  15. rebuild => undef,
  16. },
  17. }
  18. sub preprocess_if (@) {
  19. my %params=@_;
  20. foreach my $param (qw{test then}) {
  21. if (! exists $params{$param}) {
  22. error sprintf(gettext('%s parameter is required'), $param);
  23. }
  24. }
  25. my $result=0;
  26. if (! IkiWiki::yesno($params{all}) ||
  27. # An optimisation to avoid needless looping over every page
  28. # for simple uses of some of the tests.
  29. $params{test} =~ /^([\s\!()]*((enabled|sourcepage|destpage|included)\([^)]*\)|(and|or))[\s\!()]*)+$/) {
  30. add_depends($params{page}, "($params{test}) and $params{page}");
  31. $result=pagespec_match($params{page}, $params{test},
  32. location => $params{page},
  33. sourcepage => $params{page},
  34. destpage => $params{destpage});
  35. }
  36. else {
  37. $result=pagespec_match_list($params{page}, $params{test},
  38. # stop after first match
  39. num => 1,
  40. sourcepage => $params{page},
  41. destpage => $params{destpage},
  42. );
  43. }
  44. my $ret;
  45. if ($result) {
  46. $ret=$params{then};
  47. }
  48. elsif (exists $params{else}) {
  49. $ret=$params{else};
  50. }
  51. else {
  52. $ret="";
  53. }
  54. return IkiWiki::preprocess($params{page}, $params{destpage},
  55. IkiWiki::filter($params{page}, $params{destpage}, $ret));
  56. }
  57. package IkiWiki::PageSpec;
  58. sub match_enabled ($$;@) {
  59. shift;
  60. my $plugin=shift;
  61. # test if the plugin is enabled
  62. if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
  63. return IkiWiki::SuccessReason->new("$plugin is enabled");
  64. }
  65. else {
  66. return IkiWiki::FailReason->new("$plugin is not enabled");
  67. }
  68. }
  69. sub match_sourcepage ($$;@) {
  70. shift;
  71. my $glob=shift;
  72. my %params=@_;
  73. $glob=derel($glob, $params{location});
  74. return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
  75. if (match_glob($params{sourcepage}, $glob, @_)) {
  76. return IkiWiki::SuccessReason->new("sourcepage matches $glob");
  77. }
  78. else {
  79. return IkiWiki::FailReason->new("sourcepage does not match $glob");
  80. }
  81. }
  82. sub match_destpage ($$;@) {
  83. shift;
  84. my $glob=shift;
  85. my %params=@_;
  86. $glob=derel($glob, $params{location});
  87. return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
  88. if (match_glob($params{destpage}, $glob, @_)) {
  89. return IkiWiki::SuccessReason->new("destpage matches $glob");
  90. }
  91. else {
  92. return IkiWiki::FailReason->new("destpage does not match $glob");
  93. }
  94. }
  95. sub match_included ($$;@) {
  96. shift;
  97. shift;
  98. my %params=@_;
  99. return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
  100. if ($params{sourcepage} ne $params{destpage}) {
  101. return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
  102. }
  103. else {
  104. return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
  105. }
  106. }
  107. 1