]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/conditional.pm
Merge branch 'master' of git://github.com/joeyh/ikiwiki
[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 3.00;
7 use UNIVERSAL;
8
9 sub import {
10         hook(type => "getsetup", id => "conditional", call => \&getsetup);
11         hook(type => "preprocess", id => "if", call => \&preprocess_if);
12 }
13
14 sub getsetup {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 }
21
22 sub preprocess_if (@) {
23         my %params=@_;
24
25         foreach my $param (qw{test then}) {
26                 if (! exists $params{$param}) {
27                         error sprintf(gettext('%s parameter is required'), $param);
28                 }
29         }
30
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});
45
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         }
56
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 }
70
71 package IkiWiki::PageSpec;
72
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 }
85
86 sub match_sourcepage ($$;@) {
87         shift;
88         my $glob=shift;
89         my %params=@_;
90         
91         $glob=derel($glob, $params{location});
92
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         }
100 }
101
102 sub match_destpage ($$;@) {
103         shift;
104         my $glob=shift;
105         my %params=@_;
106         
107         $glob=derel($glob, $params{location});
108
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         }
116 }
117
118 sub match_included ($$;@) {
119         shift;
120         shift;
121         my %params=@_;
122
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         }
130 }
131
132 1