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