From 479c7a1ea62d8fce3ef54f9deae89230d0b52a5d Mon Sep 17 00:00:00 2001 From: joey Date: Mon, 12 Feb 2007 02:44:47 +0000 Subject: * 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. --- IkiWiki/Plugin/conditional.pm | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 IkiWiki/Plugin/conditional.pm (limited to 'IkiWiki/Plugin') 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 -- cgit v1.2.3