ikiwiki/ikiwiki-transition

67 lines
1.3 KiB
Perl
Executable File

#!/usr/bin/perl -i
use warnings;
use strict;
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}]]"
}
sub prefix_directives {
$/=undef; # process whole files at once
while (<>) {
s{$regex}{handle_directive($1, $2, $3, $4)}eg;
print;
}
}
sub usage {
print STDERR "Usage: ikiwiki-transition type file ...\n";
print STDERR "Currently supported transition types:\n";
print STDERR " prefix_directives\n";
exit 1;
}
usage() unless @ARGV;
my $mode=shift;
if ($mode eq 'prefix_directives') {
prefix_directives(@ARGV);
}
else {
usage();
}