]> sipb.mit.edu Git - ikiwiki.git/blob - ikiwiki-transition
Merge branch 'master' of ssh://git.kitenet.net/srv/git/ikiwiki.info
[ikiwiki.git] / ikiwiki-transition
1 #!/usr/bin/perl -i
2 use warnings;
3 use strict;
4
5 my $regex = qr{
6         (\\?)           # 1: escape?
7         \[\[(!?)        # directive open; 2: optional prefix
8         ([-\w]+)        # 3: command
9         (               # 4: the parameters (including initial whitespace)
10         \s+
11                 (?:
12                         (?:[-\w]+=)?            # named parameter key?
13                         (?:
14                                 """.*?"""       # triple-quoted value
15                                 |
16                                 "[^"]+"         # single-quoted value
17                                 |
18                                 [^\s\]]+        # unquoted value
19                         )
20                         \s*                     # whitespace or end
21                                                 # of directive
22                 )
23         *)              # 0 or more parameters
24         \]\]            # directive closed
25 }sx;
26
27 sub handle_directive {
28         my $escape = shift;
29         my $prefix = shift;
30         my $directive = shift;
31         my $args = shift;
32
33         if (length $escape) {
34                 return "${escape}[[${prefix}${directive}${args}]]"
35         }
36         if ($directive =~ m/^(if|more|table|template|toggleable)$/) {
37                 $args =~ s{$regex}{handle_directive($1, $2, $3, $4)}eg;
38         }
39         return "[[!${directive}${args}]]"
40 }
41
42 sub prefix_directives {
43         $/=undef; # process whole files at once
44         
45         while (<>) {
46                 s{$regex}{handle_directive($1, $2, $3, $4)}eg;
47                 print;
48         }
49 }
50
51 sub usage {
52         print STDERR "Usage: ikiwiki-transition type file ...\n";
53         print STDERR "Currently supported transition types:\n";
54         print STDERR "  prefix_directives\n";
55         exit 1;
56 }
57
58 usage() unless @ARGV;
59
60 my $mode=shift;
61 if ($mode eq 'prefix_directives') {
62         prefix_directives(@ARGV);
63 }
64 else {
65         usage();
66 }