Merge branch 'master' into tova
[ikiwiki.git] / 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 => "refresh", id => "autoindex", call => \&refresh);
11 } # }}}
12
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 } #}}}
20
21 sub refresh () { #{{{
22         eval q{use File::Find};
23         error($@) if $@;
24
25         my (%pages, %dirs);
26         find({
27                 no_chdir => 1,
28                 wanted => sub {
29                         $_=decode_utf8($_);
30                         if (IkiWiki::file_pruned($_, $config{srcdir})) {
31                                 $File::Find::prune=1;
32                         }
33                         elsif (! -l $_) {
34                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
35                                 return unless defined $f;
36                                 $f=~s/^\Q$config{srcdir}\E\/?//;
37                                 return unless length $f;
38                                 if (! -d _) {
39                                         $pages{pagename($f)}=1;
40                                 }
41                                 else {
42                                         $dirs{$f}=1;
43                                 }
44                         }
45                 }
46         }, $config{srcdir});
47
48         foreach my $dir (keys %dirs) {
49                 if (! exists $pages{$dir}) {
50                         genindex($dir);
51                 }
52         }
53 } #}}}
54
55 1