]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/conditional.pm
search: Added googlesearch option, which makes it search google rather than using...
[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
8 sub import {
9         hook(type => "getsetup", id => "conditional", call => \&getsetup);
10         hook(type => "preprocess", id => "if", call => \&preprocess_if);
11 }
12
13 sub getsetup {
14         return
15                 plugin => {
16                         safe => 1,
17                         rebuild => undef,
18                         section => "widget",
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} && ! IkiWiki::yesno($params{all})) ||
33             # An optimisation to avoid needless looping over every page
34             # for simple uses of some of the tests.
35             $params{test} =~ /^([\s\!()]*((enabled|sourcepage|destpage|included)\([^)]*\)|(and|or))[\s\!()]*)+$/) {
36                 add_depends($params{page}, "($params{test}) and $params{page}");
37                 $result=pagespec_match($params{page}, $params{test},
38                                 location => $params{page},
39                                 sourcepage => $params{page},
40                                 destpage => $params{destpage});
41         }
42         else {
43                 $result=pagespec_match_list($params{page}, $params{test},
44                         # stop after first match
45                         num => 1,
46                         sourcepage => $params{page},
47                         destpage => $params{destpage},
48                 );
49         }
50
51         my $ret;
52         if ($result) {
53                 $ret=$params{then};
54         }
55         elsif (exists $params{else}) {
56                 $ret=$params{else};
57         }
58         else {
59                 $ret="";
60         }
61         return IkiWiki::preprocess($params{page}, $params{destpage}, $ret);
62 }
63
64 package IkiWiki::PageSpec;
65
66 sub match_enabled ($$;@) {
67         shift;
68         my $plugin=shift;
69         
70         # test if the plugin is enabled
71         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
72                 return IkiWiki::SuccessReason->new("$plugin is enabled");
73         }
74         else {
75                 return IkiWiki::FailReason->new("$plugin is not enabled");
76         }
77 }
78
79 sub match_sourcepage ($$;@) {
80         shift;
81         my $glob=shift;
82         my %params=@_;
83         
84         $glob=derel($glob, $params{location});
85
86         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
87         if (match_glob($params{sourcepage}, $glob, @_)) {
88                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
89         }
90         else {
91                 return IkiWiki::FailReason->new("sourcepage does not match $glob");
92         }
93 }
94
95 sub match_destpage ($$;@) {
96         shift;
97         my $glob=shift;
98         my %params=@_;
99         
100         $glob=derel($glob, $params{location});
101
102         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
103         if (match_glob($params{destpage}, $glob, @_)) {
104                 return IkiWiki::SuccessReason->new("destpage matches $glob");
105         }
106         else {
107                 return IkiWiki::FailReason->new("destpage does not match $glob");
108         }
109 }
110
111 sub match_included ($$;@) {
112         shift;
113         shift;
114         my %params=@_;
115
116         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
117         if ($params{sourcepage} ne $params{destpage}) {
118                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
119         }
120         else {
121                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
122         }
123 }
124
125 1