aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/conditional.pm
blob: 7716fce1b4be7a612a84d71386c11f92adff51b3 (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
128
#!/usr/bin/perl
package IkiWiki::Plugin::conditional;

use warnings;
use strict;
use IkiWiki 2.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,
		},
} #}}}

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 ((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} =~ /^\s*\!?\s*(enabled|sourcepage|destpage|included)\((.*)\)\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 {
		add_depends($params{page}, $params{test});

		foreach my $page (keys %pagesources) {
			if (pagespec_match($page, $params{test}, 
					location => $params{page},
					sourcepage => $params{page},
					destpage => $params{destpage})) {
				$result=1;
				last;
			}
		}
	}

	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=@_;

	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=@_;
	
	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