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