summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 026078b3ccf6751c26418d329a62f715b1bb5252 (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. section => "widget",
  17. },
  18. }
  19. sub preprocess_if (@) {
  20. my %params=@_;
  21. foreach my $param (qw{test then}) {
  22. if (! exists $params{$param}) {
  23. error sprintf(gettext('%s parameter is required'), $param);
  24. }
  25. }
  26. my $result=0;
  27. if ((exists $params{all} && ! IkiWiki::yesno($params{all})) ||
  28. # An optimisation to avoid needless looping over every page
  29. # for simple uses of some of the 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. $result=pagespec_match_list($params{page}, $params{test},
  39. # stop after first match
  40. num => 1,
  41. sourcepage => $params{page},
  42. destpage => $params{destpage},
  43. );
  44. }
  45. my $ret;
  46. if ($result) {
  47. $ret=$params{then};
  48. }
  49. elsif (exists $params{else}) {
  50. $ret=$params{else};
  51. }
  52. else {
  53. $ret="";
  54. }
  55. return IkiWiki::preprocess($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