]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/conditional.pm
Merge branch 'master' into po
[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                 },
20 }
22 sub preprocess_if (@) {
23         my %params=@_;
25         foreach my $param (qw{test then}) {
26                 if (! exists $params{$param}) {
27                         error sprintf(gettext('%s parameter is required'), $param);
28                 }
29         }
31         my $result=0;
32         if ((exists $params{all} && lc $params{all} eq "no") ||
33                 # An optimisation to avoid needless looping over every page
34                 # and adding of dependencies for simple uses of some of the
35                 # 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                 add_depends($params{page}, $params{test});
46                 foreach my $page (keys %pagesources) {
47                         if (pagespec_match($page, $params{test}, 
48                                         location => $params{page},
49                                         sourcepage => $params{page},
50                                         destpage => $params{destpage})) {
51                                 $result=1;
52                                 last;
53                         }
54                 }
55         }
57         my $ret;
58         if ($result) {
59                 $ret=$params{then};
60         }
61         elsif (exists $params{else}) {
62                 $ret=$params{else};
63         }
64         else {
65                 $ret="";
66         }
67         return IkiWiki::preprocess($params{page}, $params{destpage}, 
68                 IkiWiki::filter($params{page}, $params{destpage}, $ret));
69 }
71 package IkiWiki::PageSpec;
73 sub match_enabled ($$;@) {
74         shift;
75         my $plugin=shift;
76         
77         # test if the plugin is enabled
78         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
79                 return IkiWiki::SuccessReason->new("$plugin is enabled");
80         }
81         else {
82                 return IkiWiki::FailReason->new("$plugin is not enabled");
83         }
84 }
86 sub match_sourcepage ($$;@) {
87         shift;
88         my $glob=shift;
89         my %params=@_;
90         
91         $glob=derel($glob, $params{location});
93         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
94         if (match_glob($params{sourcepage}, $glob, @_)) {
95                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
96         }
97         else {
98                 return IkiWiki::FailReason->new("sourcepage does not match $glob");
99         }
102 sub match_destpage ($$;@) {
103         shift;
104         my $glob=shift;
105         my %params=@_;
106         
107         $glob=derel($glob, $params{location});
109         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
110         if (match_glob($params{destpage}, $glob, @_)) {
111                 return IkiWiki::SuccessReason->new("destpage matches $glob");
112         }
113         else {
114                 return IkiWiki::FailReason->new("destpage does not match $glob");
115         }
118 sub match_included ($$;@) {
119         shift;
120         shift;
121         my %params=@_;
123         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
124         if ($params{sourcepage} ne $params{destpage}) {
125                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
126         }
127         else {
128                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
129         }