]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/conditional.pm
Rebuild for jessie-backports.
[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;
8 sub import {
9         hook(type => "getsetup", id => "conditional", call => \&getsetup);
10         hook(type => "preprocess", id => "if", call => \&preprocess_if);
11 }
13 sub getsetup {
14         return
15                 plugin => {
16                         safe => 1,
17                         rebuild => undef,
18                         section => "widget",
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} && ! 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                 $result=pagespec_match($params{page}, $params{test},
37                                 location => $params{page},
38                                 sourcepage => $params{page},
39                                 destpage => $params{destpage});
40                 my $i = $result->influences;
41                 foreach my $k (keys %$i) {
42                         # minor optimization: influences are always simple dependencies
43                         $IkiWiki::depends_simple{$params{page}}{lc $k} |= $i->{$k};
44                 }
45         }
46         else {
47                 $result=pagespec_match_list($params{page}, $params{test},
48                         # stop after first match
49                         num => 1,
50                         sourcepage => $params{page},
51                         destpage => $params{destpage},
52                 );
53         }
55         my $ret;
56         if ($result) {
57                 $ret=$params{then};
58         }
59         elsif (exists $params{else}) {
60                 $ret=$params{else};
61         }
62         else {
63                 $ret="";
64         }
65         return IkiWiki::preprocess($params{page}, $params{destpage}, $ret);
66 }
68 package IkiWiki::PageSpec;
70 sub match_enabled ($$;@) {
71         shift;
72         my $plugin=shift;
73         
74         # test if the plugin is enabled
75         if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
76                 return IkiWiki::SuccessReason->new("$plugin is enabled");
77         }
78         else {
79                 return IkiWiki::FailReason->new("$plugin is not enabled");
80         }
81 }
83 sub match_sourcepage ($$;@) {
84         shift;
85         my $glob=shift;
86         my %params=@_;
87         
88         $glob=derel($glob, $params{location});
90         return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
91         if (match_glob($params{sourcepage}, $glob, @_)) {
92                 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
93         }
94         else {
95                 return IkiWiki::FailReason->new("sourcepage does not match $glob");
96         }
97 }
99 sub match_destpage ($$;@) {
100         shift;
101         my $glob=shift;
102         my %params=@_;
103         
104         $glob=derel($glob, $params{location});
106         return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
107         if (match_glob($params{destpage}, $glob, @_)) {
108                 return IkiWiki::SuccessReason->new("destpage matches $glob");
109         }
110         else {
111                 return IkiWiki::FailReason->new("destpage does not match $glob");
112         }
115 sub match_included ($$;@) {
116         shift;
117         shift;
118         my %params=@_;
120         return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
121         if ($params{sourcepage} ne $params{destpage}) {
122                 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
123         }
124         else {
125                 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
126         }