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