]> sipb.mit.edu Git - ikiwiki.git/blob - ikiwiki-transition
0e2bd4e22020b909de8bf3098f563e3d7772f672
[ikiwiki.git] / ikiwiki-transition
1 #!/usr/bin/perl -i
2 use warnings;
3 use strict;
4 use IkiWiki;
5 use HTML::Entities;
6
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;
28
29 sub handle_directive {
30         my $escape = shift;
31         my $prefix = shift;
32         my $directive = shift;
33         my $args = shift;
34
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 }
43
44 sub prefix_directives {
45         $/=undef; # process whole files at once
46         
47         while (<>) {
48                 s{$regex}{handle_directive($1, $2, $3, $4)}eg;
49                 print;
50         }
51 }
52
53 sub indexdb {
54         $config{wikistatedir}=shift()."/.ikiwiki";
55
56         if (! defined $config{wikistatedir}) {
57                 usage();                
58         }
59
60         if (! IkiWiki::oldloadindex()) {
61                 die "failed to load index\n";
62         }
63         if (! IkiWiki::saveindex()) {
64                 die "failed to save indexdb\n"
65         }
66         if (! IkiWiki::loadindex()) {
67                 die "transition failed, cannot load new indexdb\n";
68         }
69         if (! unlink("$config{wikistatedir}/index")) {
70                 die "unlink failed: $!\n";
71         }
72 }
73
74 sub usage {
75         print STDERR "Usage: ikiwiki-transition type ...\n";
76         print STDERR "Currently supported transition subcommands:\n";
77         print STDERR "  prefix_directives file\n";
78         print STDERR "  indexdb srcdir\n";
79         exit 1;
80 }
81
82 usage() unless @ARGV;
83
84 my $mode=shift;
85 if ($mode eq 'prefix_directives') {
86         prefix_directives(@ARGV);
87 }
88 elsif ($mode eq 'indexdb') {
89         indexdb(@ARGV);
90 }
91 else {
92         usage();
93 }
94
95 package IkiWiki;
96
97 sub oldloadindex {
98         %oldrenderedfiles=%pagectime=();
99         if (! $config{rebuild}) {
100                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
101                         %destsources=%renderedfiles=%pagecase=%pagestate=();
102         }
103         open (my $in, "<", "$config{wikistatedir}/index") || return;
104         while (<$in>) {
105                 chomp;
106                 my %items;
107                 $items{link}=[];
108                 $items{dest}=[];
109                 foreach my $i (split(/ /, $_)) {
110                         my ($item, $val)=split(/=/, $i, 2);
111                         push @{$items{$item}}, decode_entities($val);
112                 }
113
114                 next unless exists $items{src}; # skip bad lines for now
115
116                 my $page=pagename($items{src}[0]);
117                 if (! $config{rebuild}) {
118                         $pagesources{$page}=$items{src}[0];
119                         $pagemtime{$page}=$items{mtime}[0];
120                         $oldlinks{$page}=[@{$items{link}}];
121                         $links{$page}=[@{$items{link}}];
122                         $depends{$page}=$items{depends}[0] if exists $items{depends};
123                         $destsources{$_}=$page foreach @{$items{dest}};
124                         $renderedfiles{$page}=[@{$items{dest}}];
125                         $pagecase{lc $page}=$page;
126                         foreach my $k (grep /_/, keys %items) {
127                                 my ($id, $key)=split(/_/, $k, 2);
128                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
129                         }
130                 }
131                 $oldrenderedfiles{$page}=[@{$items{dest}}];
132                 $pagectime{$page}=$items{ctime}[0];
133         }
134         return close($in);
135 }