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 if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
66 return IkiWiki::SuccessReason->new("$plugin is enabled");
69 return IkiWiki::FailReason->new("$plugin is not enabled");
73 sub match_sourcepage ($$;@) { #{{{
78 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
79 if (match_glob($params{sourcepage}, $glob, @_)) {
80 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
83 return IkiWiki::FailReason->new("sourcepage does not match $glob");
87 sub match_destpage ($$;@) { #{{{
92 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
93 if (match_glob($params{destpage}, $glob, @_)) {
94 return IkiWiki::SuccessReason->new("destpage matches $glob");
97 return IkiWiki::FailReason->new("destpage does not match $glob");
101 sub match_included ($$;$) { #{{{
106 return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
107 if ($params{sourcepage} ne $params{destpage}) {
108 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
111 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");