aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 892b1cff9e1d9958f19c9916f805415442cbce41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl
package IkiWiki::Plugin::conditional;

use warnings;
use strict;
use IkiWiki 3.00;
use UNIVERSAL;

sub import {
	hook(type => "getsetup", id => "conditional", call => \&getsetup);
	hook(type => "preprocess", id => "if", call => \&preprocess_if);
}

sub getsetup {
	return
		plugin => {
			safe => 1,
			rebuild => undef,
			section => "widget",
		},
}

sub preprocess_if (@) {
	my %params=@_;

	foreach my $param (qw{test then}) {
		if (! exists $params{$param}) {
			error sprintf(gettext('%s parameter is required'), $param);
		}
	}

	my $result=0;
	if (! IkiWiki::yesno($params{all}) ||
	    # An optimisation to avoid needless looping over every page
	    # for simple uses of some of the tests.
	    $params{test} =~ /^([\s\!()]*((enabled|sourcepage|destpage|included)\([^)]*\)|(and|or))[\s\!()]*)+$/) {
		add_depends($params{page}, "($params{test}) and $params{page}");
		$result=pagespec_match($params{page}, $params{test},
				location => $params{page},
				sourcepage => $params{page},
				destpage => $params{destpage});
	}
	else {
		$result=pagespec_match_list($params{page}, $params{test},
			# stop after first match
			num => 1,
			sourcepage => $params{page},
			destpage => $params{destpage},
		);
	}

	my $ret;
	if ($result) {
		$ret=$params{then};
	}
	elsif (exists $params{else}) {
		$ret=$params{else};
	}
	else {
		$ret="";
	}
	return IkiWiki::preprocess($params{page}, $params{destpage}, 
		IkiWiki::filter($params{page}, $params{destpage}, $ret));
}

package IkiWiki::PageSpec;

sub match_enabled ($$;@) {
	shift;
	my $plugin=shift;
	
	# test if the plugin is enabled
	if (UNIVERSAL::can("IkiWiki::Plugin::".$plugin, "import")) {
		return IkiWiki::SuccessReason->new("$plugin is enabled");
	}
	else {
		return IkiWiki::FailReason->new("$plugin is not enabled");
	}
}

sub match_sourcepage ($$;@) {
	shift;
	my $glob=shift;
	my %params=@_;
	
	$glob=derel($glob, $params{location});

	return IkiWiki::FailReason->new("cannot match sourcepage") unless exists $params{sourcepage};
	if (match_glob($params{sourcepage}, $glob, @_)) {
		return IkiWiki::SuccessReason->new("sourcepage matches $glob");
	}
	else {
		return IkiWiki::FailReason->new("sourcepage does not match $glob");
	}
}

sub match_destpage ($$;@) {
	shift;
	my $glob=shift;
	my %params=@_;
	
	$glob=derel($glob, $params{location});

	return IkiWiki::FailReason->new("cannot match destpage") unless exists $params{destpage};
	if (match_glob($params{destpage}, $glob, @_)) {
		return IkiWiki::SuccessReason->new("destpage matches $glob");
	}
	else {
		return IkiWiki::FailReason->new("destpage does not match $glob");
	}
}

sub match_included ($$;@) {
	shift;
	shift;
	my %params=@_;

	return IkiWiki::FailReason->new("cannot match included") unless exists $params{sourcepage} && exists $params{destpage};
	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");
	}
}

1