]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/conditional.pm
57db010544b52d436e7dac2ed42e34ef80445360
[git.ikiwiki.info.git] / IkiWiki / Plugin / conditional.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::conditional;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use UNIVERSAL;
9 sub import { #{{{
10         hook(type => "preprocess", id => "if", call => \&preprocess_if);
11 } # }}}
13 sub preprocess_if (@) { #{{{
14         my %params=@_;
16         foreach my $param (qw{test then}) {
17                 if (! exists $params{$param}) {
18                         return "[[if ".sprintf(gettext('%s parameter is required'), $param)."]]";
19                 }
20         }
22         my $result=0;
23         if ((exists $params{all} && lc $params{all} eq "no") ||
24                 # An optimisation to avoid needless looping over every page
25                 # and adding of dependencies for simple uses of some of the
26                 # tests.
27                 $params{test} =~ /^\s*\!?\s*(enabled|sourcepage|destpage|included)\((.*)\)\s*$/) {
28                 add_depends($params{page}, "$params{test} and $params{page}");
29                 $result=pagespec_match($params{page}, $params{test},
30                                 location => $params{page},
31                                 sourcepage => $params{page},
32                                 destpage => $params{destpage});
33         }
34         else {
35                 add_depends($params{page}, $params{test});
37                 foreach my $page (keys %pagesources) {
38                         if (pagespec_match($page, $params{test}, 
39                                         location => $params{page},
40                                         sourcepage => $params{page},
41                                         destpage => $params{destpage})) {
42                                 $result=1;
43                                 last;
44                         }
45                 }
46         }
48         my $ret;
49         if ($result) {
50                 $ret=$params{then};
51         }
52         elsif (exists $params{else}) {
53                 $ret=$params{else};
54         }
55         else {
56                 $ret="";
57         }
58         return IkiWiki::preprocess($params{page}, $params{destpage}, 
59                 IkiWiki::filter($params{page}, $params{destpage}, $ret));
60 } # }}}
62 package IkiWiki::PageSpec;
64 sub match_enabled ($$;@) { #{{{
65         shift;
66         my $plugin=shift;
67         
68         # test if the plugin is enabled
69         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
70                 return IkiWiki::SuccessReason->new("$plugin is enabled");
71         }
72         else {
73                 return IkiWiki::FailReason->new("$plugin is not enabled");
74         }
75 } #}}}
77 sub match_sourcepage ($$;@) { #{{{
78         shift;
79         my $glob=shift;
80         my %params=@_;
82         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
83         if (match_glob($params{sourcepage}, $glob, @_)) {
84                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
85         }
86         else {
87                 return IkiWiki::FailReason->new("sourcepage does not match $glob");
88         }
89 } #}}}
91 sub match_destpage ($$;@) { #{{{
92         shift;
93         my $glob=shift;
94         my %params=@_;
95         
96         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
97         if (match_glob($params{destpage}, $glob, @_)) {
98                 return IkiWiki::SuccessReason->new("destpage matches $glob");
99         }
100         else {
101                 return IkiWiki::FailReason->new("destpage does not match $glob");
102         }
103 } #}}}
105 sub match_included ($$;@) { #{{{
106         shift;
107         shift;
108         my %params=@_;
110         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
111         if ($params{sourcepage} ne $params{destpage}) {
112                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
113         }
114         else {
115                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
116         }
117 } #}}}