]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki-prefix-directives
possible implementation
[git.ikiwiki.info.git] / ikiwiki-prefix-directives
1 #!/usr/bin/perl -i
2 use warnings;
3 use strict;
5 undef $/; # process whole files at once
7 my $regex = qr{
8         (\\?)           # 1: escape?
9         \[\[(!?)        # directive open; 2: optional prefix
10         ([-\w]+)        # 3: command
11         (               # 4: the parameters (including initial whitespace)
12         \s+
13                 (?:
14                         (?:[-\w]+=)?            # named parameter key?
15                         (?:
16                                 """.*?"""       # triple-quoted value
17                                 |
18                                 "[^"]+"         # single-quoted value
19                                 |
20                                 [^\s\]]+        # unquoted value
21                         )
22                         \s*                     # whitespace or end
23                                                 # of directive
24                 )
25         *)              # 0 or more parameters
26         \]\]            # directive closed
27 }sx;
29 sub handle_directive {
30         my $escape = shift;
31         my $prefix = shift;
32         my $directive = shift;
33         my $args = shift;
35         if (length $escape) {
36                 return "${escape}[[${prefix}${directive}${args}]]"
37         }
38         if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
39                 $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
40         }
41         return "[[!${directive}${args}]]"
42 }
44 while (<>) {
45         s{$regex}{handle_directive($1, $2, $3, $4)}eg;
46         print;
47 }