aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
diff options
context:
space:
mode:
authorjoey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>2007-02-12 02:44:47 +0000
committerjoey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>2007-02-12 02:44:47 +0000
commit479c7a1ea62d8fce3ef54f9deae89230d0b52a5d (patch)
treef8932f522aa856e9cf35d7db3420d491dd05518e /IkiWiki/Plugin/conditional.pm
parentfe62c0ff209bbf01a3e5eb4a7b9f3b3d99acebca (diff)
downloadikiwiki-479c7a1ea62d8fce3ef54f9deae89230d0b52a5d.tar
ikiwiki-479c7a1ea62d8fce3ef54f9deae89230d0b52a5d.tar.gz
* Allow plugins to add new types of tests that can be used in PageSpecs.
* Add a "conditional" plugin, which allows displaying text if a condition is true. It is enabled by default so conditional can be used in the basewiki. * Use conditionals in the template for plugins, so that plugin pages say if they're currently enabled or not, and in various other places in the wiki.
Diffstat (limited to 'IkiWiki/Plugin/conditional.pm')
-rw-r--r--IkiWiki/Plugin/conditional.pm89
1 files changed, 89 insertions, 0 deletions
diff --git a/IkiWiki/Plugin/conditional.pm b/IkiWiki/Plugin/conditional.pm
new file mode 100644
index 000000000..35418a3ba
--- /dev/null
+++ b/IkiWiki/Plugin/conditional.pm
@@ -0,0 +1,89 @@
+#!/usr/bin/perl
+package IkiWiki::Plugin::conditional;
+
+use warnings;
+use strict;
+use IkiWiki;
+use UNIVERSAL;
+
+# Globals used to pass information into the PageSpec functions.
+our ($sourcepage, $destpage);
+
+sub import { #{{{
+ hook(type => "preprocess", id => "if", call => \&preprocess_if);
+} # }}}
+
+sub preprocess_if (@) { #{{{
+ my %params=@_;
+
+ if (! exists $params{test} || ! exists $params{then}) {
+ return "[[if requires \"test\" and \"then\" parameters]]";
+ }
+
+ my $result=0;
+ $sourcepage=$params{page};
+ $destpage=$params{destpage};
+ # An optimisation to avoid needless looping over every page
+ # and adding of dependencies for simple uses of some of the
+ # tests.
+ if ($params{test} =~ /^(enabled|sourcepage|destpage)\((.*)\)$/) {
+ $result=eval "IkiWiki::PageSpec::match_$1(undef, ".
+ IkiWiki::safequote($2).")";
+ }
+ else {
+ add_depends($params{page}, $params{test});
+
+ foreach my $page (keys %pagesources) {
+ if (pagespec_match($page, $params{test}, $params{page})) {
+ $result=1;
+ last;
+ }
+ }
+ }
+ $sourcepage="";
+ $destpage="";
+
+ my $ret;
+ if ($result) {
+ $ret=$params{then};
+ }
+ elsif (exists $params{else}) {
+ $ret=$params{else};
+ }
+ else {
+ $ret="";
+ }
+ return IkiWiki::preprocess($params{page}, $params{destpage}, $ret);
+} # }}}
+
+package IkiWiki::PageSpec;
+
+sub match_enabled ($$) { #{{{
+ shift;
+ my $plugin=shift;
+
+ # test if the plugin is enabled
+ return UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import");
+} #}}}
+
+sub match_sourcepage ($$) { #{{{
+ shift;
+ my $glob=shift;
+
+ return match_glob($IkiWiki::Plugin::conditional::sourcepage, $glob,
+ $IkiWiki::Plugin::conditional::sourcepage);
+} #}}}
+
+sub match_destpage ($$) { #{{{
+ shift;
+ my $glob=shift;
+
+ return match_glob($IkiWiki::Plugin::conditional::destpage, $glob,
+ $IkiWiki::Plugin::conditional::sourcepage);
+} #}}}
+
+sub match_included ($$) { #{{{
+ return $IkiWiki::Plugin::conditional::sourcepage ne $IkiWiki::Plugin::conditional::destpage;
+} #}}}
+
+1