web commit by http://hands.com/~phil/
[ikiwiki] / 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 # A slightly modified version of the old loadindex function.
98 sub oldloadindex {
99         %oldrenderedfiles=%pagectime=();
100         if (! $config{rebuild}) {
101                 %pagesources=%pagemtime=%oldlinks=%links=%depends=
102                         %destsources=%renderedfiles=%pagecase=%pagestate=();
103         }
104         open (my $in, "<", "$config{wikistatedir}/index") || return;
105         while (<$in>) {
106                 chomp;
107                 my %items;
108                 $items{link}=[];
109                 $items{dest}=[];
110                 foreach my $i (split(/ /, $_)) {
111                         my ($item, $val)=split(/=/, $i, 2);
112                         push @{$items{$item}}, decode_entities($val);
113                 }
114
115                 next unless exists $items{src}; # skip bad lines for now
116
117                 my $page=pagename($items{src}[0]);
118                 if (! $config{rebuild}) {
119                         $pagesources{$page}=$items{src}[0];
120                         $pagemtime{$page}=$items{mtime}[0];
121                         $oldlinks{$page}=[@{$items{link}}];
122                         $links{$page}=[@{$items{link}}];
123                         $depends{$page}=$items{depends}[0] if exists $items{depends};
124                         $destsources{$_}=$page foreach @{$items{dest}};
125                         $renderedfiles{$page}=[@{$items{dest}}];
126                         $pagecase{lc $page}=$page;
127                         foreach my $k (grep /_/, keys %items) {
128                                 my ($id, $key)=split(/_/, $k, 2);
129                                 $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
130                         }
131                 }
132                 $oldrenderedfiles{$page}=[@{$items{dest}}];
133                 $pagectime{$page}=$items{ctime}[0];
134         }
135
136         # saveindex relies on %hooks being populated, else it won't save
137         # the page state owned by a given hook. But no plugins are loaded
138         # by this program, so populate %hooks with all hook ids that
139         # currently have page state.
140         foreach my $page (keys %pagemtime) {
141                 foreach my $id (keys %{$pagestate{$page}}) {
142                         $hooks{_dummy}{$id}=1;
143                 }
144         }
145         
146         return close($in);
147 }