2007-02-12 03:44:47 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::conditional;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2007-04-27 04:55:52 +02:00
|
|
|
use IkiWiki 2.00;
|
2007-02-12 03:44:47 +01:00
|
|
|
use UNIVERSAL;
|
|
|
|
|
|
|
|
sub import { #{{{
|
|
|
|
hook(type => "preprocess", id => "if", call => \&preprocess_if);
|
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub preprocess_if (@) { #{{{
|
|
|
|
my %params=@_;
|
|
|
|
|
2007-05-09 02:31:49 +02:00
|
|
|
foreach my $param (qw{test then}) {
|
|
|
|
if (! exists $params{$param}) {
|
|
|
|
return "[[if ".sprintf(gettext('%s parameter is required'), $param)."]]";
|
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $result=0;
|
2007-05-09 02:31:49 +02:00
|
|
|
if ((exists $params{all} && lc $params{all} eq "no") ||
|
|
|
|
# An optimisation to avoid needless looping over every page
|
|
|
|
# and adding of dependencies for simple uses of some of the
|
|
|
|
# tests.
|
|
|
|
$params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
|
|
|
|
add_depends($params{page}, "$params{test} and $params{page}");
|
2007-04-27 04:55:52 +02:00
|
|
|
$result=pagespec_match($params{page}, $params{test},
|
|
|
|
location => $params{page},
|
|
|
|
sourcepage => $params{page},
|
|
|
|
destpage => $params{destpage});
|
2007-02-12 03:44:47 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
add_depends($params{page}, $params{test});
|
|
|
|
|
|
|
|
foreach my $page (keys %pagesources) {
|
2007-04-27 04:55:52 +02:00
|
|
|
if (pagespec_match($page, $params{test},
|
|
|
|
location => $params{page},
|
|
|
|
sourcepage => $params{page},
|
|
|
|
destpage => $params{destpage})) {
|
2007-02-12 03:44:47 +01:00
|
|
|
$result=1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $ret;
|
|
|
|
if ($result) {
|
|
|
|
$ret=$params{then};
|
|
|
|
}
|
|
|
|
elsif (exists $params{else}) {
|
|
|
|
$ret=$params{else};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ret="";
|
|
|
|
}
|
2007-02-14 01:11:19 +01:00
|
|
|
return IkiWiki::preprocess($params{page}, $params{destpage},
|
|
|
|
IkiWiki::filter($params{page}, $ret));
|
2007-02-12 03:44:47 +01:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
package IkiWiki::PageSpec;
|
|
|
|
|
2007-04-27 04:55:52 +02:00
|
|
|
sub match_enabled ($$;@) { #{{{
|
2007-02-12 03:44:47 +01:00
|
|
|
shift;
|
|
|
|
my $plugin=shift;
|
|
|
|
|
|
|
|
# test if the plugin is enabled
|
2007-04-27 10:34:09 +02:00
|
|
|
if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
|
|
|
|
return IkiWiki::SuccessReason->new("$plugin is enabled");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("$plugin is not enabled");
|
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
} #}}}
|
|
|
|
|
2007-04-27 04:55:52 +02:00
|
|
|
sub match_sourcepage ($$;@) { #{{{
|
2007-02-12 03:44:47 +01:00
|
|
|
shift;
|
|
|
|
my $glob=shift;
|
2007-04-27 04:55:52 +02:00
|
|
|
my %params=@_;
|
|
|
|
|
2007-04-27 09:55:40 +02:00
|
|
|
return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
|
2007-04-27 10:34:09 +02:00
|
|
|
if (match_glob($params{sourcepage}, $glob, @_)) {
|
|
|
|
return IkiWiki::SuccessReason->new("sourcepage matches $glob");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("sourcepage does not match $glob");
|
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
} #}}}
|
|
|
|
|
2007-04-27 04:55:52 +02:00
|
|
|
sub match_destpage ($$;@) { #{{{
|
2007-02-12 03:44:47 +01:00
|
|
|
shift;
|
|
|
|
my $glob=shift;
|
2007-04-27 04:55:52 +02:00
|
|
|
my %params=@_;
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2007-04-27 09:55:40 +02:00
|
|
|
return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
|
2007-04-27 10:34:09 +02:00
|
|
|
if (match_glob($params{destpage}, $glob, @_)) {
|
|
|
|
return IkiWiki::SuccessReason->new("destpage matches $glob");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("destpage does not match $glob");
|
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
} #}}}
|
|
|
|
|
2007-04-27 04:55:52 +02:00
|
|
|
sub match_included ($$;$) { #{{{
|
|
|
|
shift;
|
|
|
|
shift;
|
|
|
|
my %params=@_;
|
|
|
|
|
2007-04-27 09:55:40 +02:00
|
|
|
return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
|
2007-04-27 10:34:09 +02:00
|
|
|
if ($params{sourcepage} ne $params{destpage}) {
|
|
|
|
return IkiWiki::SuccessReason->new("page $params{sourcepage} is included");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return IkiWiki::FailReason->new("page $params{sourcepage} is not included");
|
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
} #}}}
|
|
|
|
|
|
|
|
1
|