add plugin safe/rebuild info (part 1 of 2)
[ikiwiki] / IkiWiki / Plugin / autoindex.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::autoindex;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7 use Encode;
8
9 sub import { #{{{
10         hook(type => "getsetup", id => "autoindex", call => \&getsetup);
11         hook(type => "refresh", id => "autoindex", call => \&refresh);
12 } # }}}
13
14 sub getsetup () { #{{{
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => 0,
19                 },
20 } #}}}
21
22 sub genindex ($) { #{{{
23         my $page=shift;
24         my $file=$page.".".$config{default_pageext};
25         my $template=template("autoindex.tmpl");
26         $template->param(page => $page);
27         writefile($file, $config{srcdir}, $template->output);
28         if ($config{rcs}) {
29                 IkiWiki::rcs_add($file);
30         }
31 } #}}}
32
33 sub refresh () { #{{{
34         eval q{use File::Find};
35         error($@) if $@;
36
37         my (%pages, %dirs);
38         find({
39                 no_chdir => 1,
40                 wanted => sub {
41                         $_=decode_utf8($_);
42                         if (IkiWiki::file_pruned($_, $config{srcdir})) {
43                                 $File::Find::prune=1;
44                         }
45                         elsif (! -l $_) {
46                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
47                                 return unless defined $f;
48                                 $f=~s/^\Q$config{srcdir}\E\/?//;
49                                 return unless length $f;
50                                 if (! -d _) {
51                                         $pages{pagename($f)}=1;
52                                 }
53                                 else {
54                                         $dirs{$f}=1;
55                                 }
56                         }
57                 }
58         }, $config{srcdir});
59
60         my @needed;
61         foreach my $dir (keys %dirs) {
62                 if (! exists $pages{$dir}) {
63                         push @needed, $dir;
64                 }
65         }
66         
67         if (@needed) {
68                 if ($config{rcs}) {
69                         IkiWiki::disable_commit_hook();
70                 }
71                 genindex($_) foreach @needed;
72                 if ($config{rcs}) {
73                         IkiWiki::rcs_commit_staged(
74                                 gettext("automatic index generation"),
75                                 undef, undef);
76                         IkiWiki::enable_commit_hook();
77                 }
78         }
79 } #}}}
80
81 1