aboutsummaryrefslogtreecommitdiff
path: root/doc/todo/flexible_relationships_between_pages/blocks.pm.mdwn
blob: 0ef97b2c195b26ed532fd2508e273f0e75398f40 (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
129
#!/usr/bin/perl
# Ikiwiki "blocks" relationship plugin.
package IkiWiki::Plugin::blocks;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type => "getsetup", id => "blocks", call => \&getsetup);
	hook(type => "checkconfig", id => "skeleton", call => \&checkconfig);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => 1,
		},
		blocks_names => {
			type => "string",
			example => "blocks/blockedby",
			description => "comma separated list of defined relationship pairs, the forward and backward name separated by a slash",
			safe => 1,
			rebuild => 1,
		},
}

sub checkconfig () {
	my $blocksnames;
	if (defined $config{blocks_names}) {
		$blocksnames = $config{blocks_names};
	} else {
		$blocksnames = "blocks/blockedby";
	}

	while ( $blocksnames =~ /([^ ]+)/g )
	{
		if ( $1 =~ m@([a-zA-Z0-9]+)(/([a-zA-Z0-9]+))?@ )
		{
			my $from = $1;
			my $to = $3;
			hook(
				type => "preprocess",
				shortcut => 1, # gets interpreted by listdirectives; see doc/bugs/cannot_preview_shortcuts.mdwn / ikiwiki commit 354d22e2
				no_override => 1,
				id => $from,
				scan => 1,
				call => sub { preprocess_blocks($from, 1, @_); }
			);
			if ($to)
			{
				hook(
					type => "preprocess",
					shortcut => 1,
					no_override => 1,
					id => $to,
					scan => 1,
					call => sub { preprocess_blocks($from, 0, @_); }
				);
			}

			my $backward_match; my $backward_name;
			my $forward_match; my $forward_name;

			$backward_match = sub ($$;@) {
				my $page=shift;
				my $glob=shift;
				return IkiWiki::PageSpec::match_backlink($page, $glob, linktype => $from, @_);
			};

			$backward_name = "IkiWiki::PageSpec::match_$from";

			if ($to)
			{
				$forward_match = sub ($$;@) {
					my $page=shift;
					my $glob=shift;
					return IkiWiki::PageSpec::match_link($page, $glob, linktype => $from, @_);
				};

				$forward_name = "IkiWiki::PageSpec::match_$to";
			}

			{
				no strict 'refs';

				if ($to)
				{
					*$forward_name = $forward_match;
				}
				*$backward_name = $backward_match;
			}
		} else {
			error gettext("Malformed option in blocks_names");
		}
	}
}

sub preprocess_blocks ($$@) {
	# with flip=0, the directive occurring on page A pointing at page B
	# means that A $relation B, with flip=1, it means B $relation A
	my $relation = shift;
	my $flip = shift;

	if (! @_) {
		return "";
	}
	my %params=@_;
	my $page = $params{page};
	delete $params{page};
	delete $params{destpage};
	delete $params{preview};

	foreach my $blocks (keys %params) {
		$blocks=linkpage($blocks);
		
		# hidden WikiLink
		if ( $flip == 0 ) {
			add_link($page, $blocks, $relation);
		} else {
			add_link($blocks, $page, $relation);
		}
	}
		
	return "";
}

1