2 package IkiWiki::Plugin::conditional;
10 hook(type => "preprocess", id => "if", call => \&preprocess_if);
13 sub preprocess_if (@) { #{{{
16 if (! exists $params{test} || ! exists $params{then}) {
17 return "[[if ".gettext('"test" and "then" parameters are required')."]]";
21 # An optimisation to avoid needless looping over every page
22 # and adding of dependencies for simple uses of some of the
24 if ($params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
25 $result=pagespec_match($params{page}, $params{test},
26 location => $params{page},
27 sourcepage => $params{page},
28 destpage => $params{destpage});
31 add_depends($params{page}, $params{test});
33 foreach my $page (keys %pagesources) {
34 if (pagespec_match($page, $params{test},
35 location => $params{page},
36 sourcepage => $params{page},
37 destpage => $params{destpage})) {
48 elsif (exists $params{else}) {
54 return IkiWiki::preprocess($params{page}, $params{destpage},
55 IkiWiki::filter($params{page}, $ret));
58 package IkiWiki::PageSpec;
60 sub match_enabled ($$;@) { #{{{
64 # test if the plugin is enabled
65 return 1 if UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import");
66 return IkiWiki::FailReason->new("$plugin is not enabled");
69 sub match_sourcepage ($$;@) { #{{{
74 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
75 return 1 if match_glob($params{sourcepage}, $glob, @_);
76 return IkiWiki::FailReason->new("sourcepage does not match $glob");
79 sub match_destpage ($$;@) { #{{{
84 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
85 return 1 if match_glob($params{destpage}, $glob, @_);
86 return IkiWiki::FailReason->new("destpage does not match $glob");
89 sub match_included ($$;$) { #{{{
94 return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
95 return 1 if $params{sourcepage} ne $params{destpage};
96 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");