2007-02-12 03:44:47 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::conditional;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2007-02-12 03:44:47 +01:00
|
|
|
use UNIVERSAL;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "conditional", call => \&getsetup);
|
2007-02-12 03:44:47 +01:00
|
|
|
hook(type => "preprocess", id => "if", call => \&preprocess_if);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess_if (@) {
|
2007-02-12 03:44:47 +01:00
|
|
|
my %params=@_;
|
|
|
|
|
2007-05-09 02:31:49 +02:00
|
|
|
foreach my $param (qw{test then}) {
|
|
|
|
if (! exists $params{$param}) {
|
2008-07-13 21:05:34 +02:00
|
|
|
error sprintf(gettext('%s parameter is required'), $param);
|
2007-05-09 02:31:49 +02:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $result=0;
|
2010-04-16 00:18:48 +02:00
|
|
|
if ((exists $params{all} && ! IkiWiki::yesno($params{all})) ||
|
2009-10-09 19:02:03 +02:00
|
|
|
# An optimisation to avoid needless looping over every page
|
2009-10-09 19:07:50 +02:00
|
|
|
# for simple uses of some of the tests.
|
2009-10-09 19:02:03 +02:00
|
|
|
$params{test} =~ /^([\s\!()]*((enabled|sourcepage|destpage|included)\([^)]*\)|(and|or))[\s\!()]*)+$/) {
|
2008-09-30 00:05:39 +02:00
|
|
|
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 {
|
2009-10-09 19:07:50 +02:00
|
|
|
$result=pagespec_match_list($params{page}, $params{test},
|
|
|
|
# stop after first match
|
|
|
|
num => 1,
|
|
|
|
sourcepage => $params{page},
|
|
|
|
destpage => $params{destpage},
|
|
|
|
);
|
2007-02-12 03:44:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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},
|
2007-05-17 21:55:11 +02:00
|
|
|
IkiWiki::filter($params{page}, $params{destpage}, $ret));
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
|
|
|
package IkiWiki::PageSpec;
|
|
|
|
|
2008-12-17 21:22:16 +01: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");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01: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=@_;
|
2009-01-10 20:36:03 +01:00
|
|
|
|
|
|
|
$glob=derel($glob, $params{location});
|
2007-04-27 04:55:52 +02:00
|
|
|
|
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");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01: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
|
|
|
|
2009-01-10 20:36:03 +01:00
|
|
|
$glob=derel($glob, $params{location});
|
|
|
|
|
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");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub match_included ($$;@) {
|
2007-04-27 04:55:52 +02:00
|
|
|
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");
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-02-12 03:44:47 +01:00
|
|
|
|
|
|
|
1
|