2 package IkiWiki::Plugin::conditional;
10 hook(type => "getsetup", id => "conditional", call => \&getsetup);
11 hook(type => "preprocess", id => "if", call => \&preprocess_if);
23 sub preprocess_if (@) {
26 foreach my $param (qw{test then}) {
27 if (! exists $params{$param}) {
28 error sprintf(gettext('%s parameter is required'), $param);
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});
44 $result=pagespec_match_list($params{page}, $params{test},
45 # stop after first match
47 sourcepage => $params{page},
48 destpage => $params{destpage},
56 elsif (exists $params{else}) {
62 return IkiWiki::preprocess($params{page}, $params{destpage},
63 IkiWiki::filter($params{page}, $params{destpage}, $ret));
66 package IkiWiki::PageSpec;
68 sub match_enabled ($$;@) {
72 # test if the plugin is enabled
73 if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
74 return IkiWiki::SuccessReason->new("$plugin is enabled");
77 return IkiWiki::FailReason->new("$plugin is not enabled");
81 sub match_sourcepage ($$;@) {
86 $glob=derel($glob, $params{location});
88 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
89 if (match_glob($params{sourcepage}, $glob, @_)) {
90 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
93 return IkiWiki::FailReason->new("sourcepage does not match $glob");
97 sub match_destpage ($$;@) {
102 $glob=derel($glob, $params{location});
104 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
105 if (match_glob($params{destpage}, $glob, @_)) {
106 return IkiWiki::SuccessReason->new("destpage matches $glob");
109 return IkiWiki::FailReason->new("destpage does not match $glob");
113 sub match_included ($$;@) {
118 return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
119 if ($params{sourcepage} ne $params{destpage}) {
120 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
123 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");