2 package IkiWiki::Plugin::conditional;
10 hook(type => "getsetup", id => "conditional", call => \&getsetup);
11 hook(type => "preprocess", id => "if", call => \&preprocess_if);
22 sub preprocess_if (@) {
25 foreach my $param (qw{test then}) {
26 if (! exists $params{$param}) {
27 error sprintf(gettext('%s parameter is required'), $param);
32 if (! 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});
43 $result=pagespec_match_list($params{page}, $params{test},
44 # stop after first match
46 sourcepage => $params{page},
47 destpage => $params{destpage},
55 elsif (exists $params{else}) {
61 return IkiWiki::preprocess($params{page}, $params{destpage},
62 IkiWiki::filter($params{page}, $params{destpage}, $ret));
65 package IkiWiki::PageSpec;
67 sub match_enabled ($$;@) {
71 # test if the plugin is enabled
72 if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
73 return IkiWiki::SuccessReason->new("$plugin is enabled");
76 return IkiWiki::FailReason->new("$plugin is not enabled");
80 sub match_sourcepage ($$;@) {
85 $glob=derel($glob, $params{location});
87 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
88 if (match_glob($params{sourcepage}, $glob, @_)) {
89 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
92 return IkiWiki::FailReason->new("sourcepage does not match $glob");
96 sub match_destpage ($$;@) {
101 $glob=derel($glob, $params{location});
103 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
104 if (match_glob($params{destpage}, $glob, @_)) {
105 return IkiWiki::SuccessReason->new("destpage matches $glob");
108 return IkiWiki::FailReason->new("destpage does not match $glob");
112 sub match_included ($$;@) {
117 return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
118 if ($params{sourcepage} ne $params{destpage}) {
119 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
122 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");