2 package IkiWiki::Plugin::conditional;
10 hook(type => "preprocess", id => "if", call => \&preprocess_if);
13 sub preprocess_if (@) { #{{{
16 foreach my $param (qw{test then}) {
17 if (! exists $params{$param}) {
18 error sprintf(gettext('%s parameter is required'), $param);
23 if ((exists $params{all} && lc $params{all} eq "no") ||
24 # An optimisation to avoid needless looping over every page
25 # and adding of dependencies for simple uses of some of the
27 $params{test} =~ /^\s*\!?\s*(enabled|sourcepage|destpage|included)\((.*)\)\s*$/) {
28 add_depends($params{page}, "$params{test} and $params{page}");
29 $result=pagespec_match($params{page}, $params{test},
30 location => $params{page},
31 sourcepage => $params{page},
32 destpage => $params{destpage});
35 add_depends($params{page}, $params{test});
37 foreach my $page (keys %pagesources) {
38 if (pagespec_match($page, $params{test},
39 location => $params{page},
40 sourcepage => $params{page},
41 destpage => $params{destpage})) {
52 elsif (exists $params{else}) {
58 return IkiWiki::preprocess($params{page}, $params{destpage},
59 IkiWiki::filter($params{page}, $params{destpage}, $ret));
62 package IkiWiki::PageSpec;
64 sub match_enabled ($$;@) { #{{{
68 # test if the plugin is enabled
69 if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
70 return IkiWiki::SuccessReason->new("$plugin is enabled");
73 return IkiWiki::FailReason->new("$plugin is not enabled");
77 sub match_sourcepage ($$;@) { #{{{
82 return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
83 if (match_glob($params{sourcepage}, $glob, @_)) {
84 return IkiWiki::SuccessReason->new("sourcepage matches $glob");
87 return IkiWiki::FailReason->new("sourcepage does not match $glob");
91 sub match_destpage ($$;@) { #{{{
96 return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
97 if (match_glob($params{destpage}, $glob, @_)) {
98 return IkiWiki::SuccessReason->new("destpage matches $glob");
101 return IkiWiki::FailReason->new("destpage does not match $glob");
105 sub match_included ($$;@) { #{{{
110 return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
111 if ($params{sourcepage} ne $params{destpage}) {
112 return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
115 return IkiWiki::FailReason->new("page $params{sourcepage} is not included");