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 ((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
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 add_depends($params{page}, $params{test});
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})) {
61 elsif (exists $params{else}) {
67 return IkiWiki::preprocess($params{page}, $params{destpage},
68 IkiWiki::filter($params{page}, $params{destpage}, $ret));
71 package IkiWiki::PageSpec;
73 sub match_enabled ($$;@) {
77 # test if the plugin is enabled
78 if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
79 return IkiWiki::SuccessReason->new("$plugin is enabled");
82 return IkiWiki::FailReason->new("$plugin is not enabled");
86 sub match_sourcepage ($$;@) {
91 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
92 if (match_glob($params{sourcepage}, $glob, @_)) {
93 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
96 return IkiWiki::FailReason->new("sourcepage does not match $glob");
100 sub match_destpage ($$;@) {
105 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
106 if (match_glob($params{destpage}, $glob, @_)) {
107 return IkiWiki::SuccessReason->new("destpage matches $glob");
110 return IkiWiki::FailReason->new("destpage does not match $glob");
114 sub match_included ($$;@) {
119 return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
120 if ($params{sourcepage} ne $params{destpage}) {
121 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
124 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");