]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/conditional.pm
Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[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 ((exists $params{all} && ! 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}, $ret);
63 }
65 package IkiWiki::PageSpec;
67 sub match_enabled ($$;@) {
68         shift;
69         my $plugin=shift;
70         
71         # test if the plugin is enabled
72         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
73                 return IkiWiki::SuccessReason->new("$plugin is enabled");
74         }
75         else {
76                 return IkiWiki::FailReason->new("$plugin is not enabled");
77         }
78 }
80 sub match_sourcepage ($$;@) {
81         shift;
82         my $glob=shift;
83         my %params=@_;
84         
85         $glob=derel($glob, $params{location});
87         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
88         if (match_glob($params{sourcepage}, $glob, @_)) {
89                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
90         }
91         else {
92                 return IkiWiki::FailReason->new("sourcepage does not match $glob");
93         }
94 }
96 sub match_destpage ($$;@) {
97         shift;
98         my $glob=shift;
99         my %params=@_;
100         
101         $glob=derel($glob, $params{location});
103         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
104         if (match_glob($params{destpage}, $glob, @_)) {
105                 return IkiWiki::SuccessReason->new("destpage matches $glob");
106         }
107         else {
108                 return IkiWiki::FailReason->new("destpage does not match $glob");
109         }
112 sub match_included ($$;@) {
113         shift;
114         shift;
115         my %params=@_;
117         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
118         if ($params{sourcepage} ne $params{destpage}) {
119                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
120         }
121         else {
122                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
123         }