]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/conditional.pm
update
[ikiwiki.git] / IkiWiki / Plugin / conditional.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::conditional;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use UNIVERSAL;
8
9 sub import { #{{{
10         hook(type => "preprocess", id => "if", call => \&preprocess_if);
11 } # }}}
12
13 sub preprocess_if (@) { #{{{
14         my %params=@_;
15
16         if (! exists $params{test} || ! exists $params{then}) {
17                 return "[[if ".gettext('"test" and "then" parameters are required')."]]";
18         }
19
20         my $result=0;
21         # An optimisation to avoid needless looping over every page
22         # and adding of dependencies for simple uses of some of the
23         # tests.
24         if ($params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
25                 $result=pagespec_match($params{page}, $params{test},
26                                 location => $params{page},
27                                 sourcepage => $params{page},
28                                 destpage => $params{destpage});
29         }
30         else {
31                 add_depends($params{page}, $params{test});
32
33                 foreach my $page (keys %pagesources) {
34                         if (pagespec_match($page, $params{test}, 
35                                         location => $params{page},
36                                         sourcepage => $params{page},
37                                         destpage => $params{destpage})) {
38                                 $result=1;
39                                 last;
40                         }
41                 }
42         }
43
44         my $ret;
45         if ($result) {
46                 $ret=$params{then};
47         }
48         elsif (exists $params{else}) {
49                 $ret=$params{else};
50         }
51         else {
52                 $ret="";
53         }
54         return IkiWiki::preprocess($params{page}, $params{destpage}, 
55                 IkiWiki::filter($params{page}, $ret));
56 } # }}}
57
58 package IkiWiki::PageSpec;
59
60 sub match_enabled ($$;@) { #{{{
61         shift;
62         my $plugin=shift;
63         
64         # test if the plugin is enabled
65         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
66                 return IkiWiki::SuccessReason->new("$plugin is enabled");
67         }
68         else {
69                 return IkiWiki::FailReason->new("$plugin is not enabled");
70         }
71 } #}}}
72
73 sub match_sourcepage ($$;@) { #{{{
74         shift;
75         my $glob=shift;
76         my %params=@_;
77
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
87 sub match_destpage ($$;@) { #{{{
88         shift;
89         my $glob=shift;
90         my %params=@_;
91         
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
101 sub match_included ($$;$) { #{{{
102         shift;
103         shift;
104         my %params=@_;
105
106         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
107         if ($params{sourcepage} ne $params{destpage}) {
108                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
109         }
110         else {
111                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
112         }
113 } #}}}
114
115 1