aboutsummaryrefslogtreecommitdiff
path: root/ikiwiki-prefix-directives
blob: d35c41f30e922628d3e15268b18a19c7bfb53338 (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
#!/usr/bin/perl -i
undef $/; # process whole files at once

my $regex = qr{
	(\\?)		# 1: escape?
	\[\[(!?)	# directive open; 2: optional prefix
	([-\w]+)	# 3: command
	(		# 4: the parameters (including initial whitespace)
	\s+
		(?:
			(?:[-\w]+=)?		# named parameter key?
			(?:
				""".*?"""	# triple-quoted value
				|
				"[^"]+"		# single-quoted value
				|
				[^\s\]]+	# unquoted value
			)
			\s*			# whitespace or end
						# of directive
		)
	*)		# 0 or more parameters
	\]\]		# directive closed
}sx;

sub handle_directive($$$$) {
	my $escape = shift;
	my $prefix = shift;
	my $directive = shift;
	my $args = shift;

	if (length $escape) {
		return "${escape}[[${prefix}${directive}${args}]]"
	}
	if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
		$args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
	}
	return "[[!${directive}${args}]]"
}

while (<>) {
	s{$regex}{handle_directive($1, $2, $3, $4)}eg;
	print;
}