2008-01-28 01:13:54 +01:00
|
|
|
#!/usr/bin/perl -i
|
2008-01-28 20:24:27 +01:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2008-01-28 01:13:54 +01:00
|
|
|
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;
|
|
|
|
|
2008-01-28 20:24:27 +01:00
|
|
|
sub handle_directive {
|
2008-01-28 01:13:54 +01:00
|
|
|
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;
|
|
|
|
}
|