summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 7716fce1b4be7a612a84d71386c11f92adff51b3 (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 => "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*\!?\s*(enabled|sourcepage|destpage|included)\((.*)\)\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. return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
  79. if (match_glob($params{sourcepage}, $glob, @_)) {
  80. return IkiWiki::SuccessReason->new("sourcepage matches $glob");
  81. }
  82. else {
  83. return IkiWiki::FailReason->new("sourcepage does not match $glob");
  84. }
  85. } #}}}
  86. sub match_destpage ($$;@) { #{{{
  87. shift;
  88. my $glob=shift;
  89. my %params=@_;
  90. return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
  91. if (match_glob($params{destpage}, $glob, @_)) {
  92. return IkiWiki::SuccessReason->new("destpage matches $glob");
  93. }
  94. else {
  95. return IkiWiki::FailReason->new("destpage does not match $glob");
  96. }
  97. } #}}}
  98. sub match_included ($$;@) { #{{{
  99. shift;
  100. shift;
  101. my %params=@_;
  102. return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
  103. if ($params{sourcepage} ne $params{destpage}) {
  104. return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
  105. }
  106. else {
  107. return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
  108. }
  109. } #}}}
  110. 1