]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/conditional.pm
(no commit message)
[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 3.00;
7 use UNIVERSAL;
9 sub import {
10         hook(type => "getsetup", id => "conditional", call => \&getsetup);
11         hook(type => "preprocess", id => "if", call => \&preprocess_if);
12 }
14 sub getsetup {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                         section => "widget",
20                 },
21 }
23 sub preprocess_if (@) {
24         my %params=@_;
26         foreach my $param (qw{test then}) {
27                 if (! exists $params{$param}) {
28                         error sprintf(gettext('%s parameter is required'), $param);
29                 }
30         }
32         my $result=0;
33         if (! IkiWiki::yesno($params{all}) ||
34             # An optimisation to avoid needless looping over every page
35             # for simple uses of some of the tests.
36             $params{test} =~ /^([\s\!()]*((enabled|sourcepage|destpage|included)\([^)]*\)|(and|or))[\s\!()]*)+$/) {
37                 add_depends($params{page}, "($params{test}) and $params{page}");
38                 $result=pagespec_match($params{page}, $params{test},
39                                 location => $params{page},
40                                 sourcepage => $params{page},
41                                 destpage => $params{destpage});
42         }
43         else {
44                 $result=pagespec_match_list($params{page}, $params{test},
45                         # stop after first match
46                         num => 1,
47                         sourcepage => $params{page},
48                         destpage => $params{destpage},
49                 );
50         }
52         my $ret;
53         if ($result) {
54                 $ret=$params{then};
55         }
56         elsif (exists $params{else}) {
57                 $ret=$params{else};
58         }
59         else {
60                 $ret="";
61         }
62         return IkiWiki::preprocess($params{page}, $params{destpage}, 
63                 IkiWiki::filter($params{page}, $params{destpage}, $ret));
64 }
66 package IkiWiki::PageSpec;
68 sub match_enabled ($$;@) {
69         shift;
70         my $plugin=shift;
71         
72         # test if the plugin is enabled
73         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
74                 return IkiWiki::SuccessReason->new("$plugin is enabled");
75         }
76         else {
77                 return IkiWiki::FailReason->new("$plugin is not enabled");
78         }
79 }
81 sub match_sourcepage ($$;@) {
82         shift;
83         my $glob=shift;
84         my %params=@_;
85         
86         $glob=derel($glob, $params{location});
88         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
89         if (match_glob($params{sourcepage}, $glob, @_)) {
90                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
91         }
92         else {
93                 return IkiWiki::FailReason->new("sourcepage does not match $glob");
94         }
95 }
97 sub match_destpage ($$;@) {
98         shift;
99         my $glob=shift;
100         my %params=@_;
101         
102         $glob=derel($glob, $params{location});
104         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
105         if (match_glob($params{destpage}, $glob, @_)) {
106                 return IkiWiki::SuccessReason->new("destpage matches $glob");
107         }
108         else {
109                 return IkiWiki::FailReason->new("destpage does not match $glob");
110         }
113 sub match_included ($$;@) {
114         shift;
115         shift;
116         my %params=@_;
118         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
119         if ($params{sourcepage} ne $params{destpage}) {
120                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
121         }
122         else {
123                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
124         }