]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/conditional.pm
* Make pagespec_match on failure return a value that is false, but in a
[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         return 1 if UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import");
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
74         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
75         return 1 if match_glob($params{sourcepage}, $glob, @_);
76         return IkiWiki::FailReason->new("sourcepage does not match $glob");
77 } #}}}
78
79 sub match_destpage ($$;@) { #{{{
80         shift;
81         my $glob=shift;
82         my %params=@_;
83         
84         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
85         return 1 if match_glob($params{destpage}, $glob, @_);
86         return IkiWiki::FailReason->new("destpage does not match $glob");
87 } #}}}
88
89 sub match_included ($$;$) { #{{{
90         shift;
91         shift;
92         my %params=@_;
93
94         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
95         return 1 if $params{sourcepage} ne $params{destpage};
96         return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
97 } #}}}
98
99 1