2 package IkiWiki::Plugin::autoindex;
10 hook(type => "getsetup", id => "autoindex", call => \&getsetup);
11 hook(type => "refresh", id => "autoindex", call => \&refresh);
24 my $file=newpagefile($page, $config{default_pageext});
26 add_autofile($file, "autoindex", sub {
27 my $message = sprintf(gettext("creating index page %s"),
31 my $template = template("autoindex.tmpl");
32 $template->param(page => $page);
33 writefile($file, $config{srcdir}, $template->output);
36 IkiWiki::disable_commit_hook();
37 IkiWiki::rcs_add($file);
38 IkiWiki::rcs_commit_staged(message => $message);
39 IkiWiki::enable_commit_hook();
45 eval q{use File::Find};
52 foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
58 my $file=decode_utf8($_);
60 return unless length $file;
61 if (IkiWiki::file_pruned($file)) {
65 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
66 return unless defined $f;
67 return if $f =~ /\._([^.]+)$/; # skip internal page
69 $pages{pagename($f)}=1;
71 elsif ($dir eq $config{srcdir}) {
78 chdir($origdir) || die "chdir $origdir: $!";
81 # FIXME: some of this is probably redundant with add_autofile now, and
82 # the rest should perhaps be added to the autofile machinery
85 if (ref $wikistate{autoindex}{deleted}) {
86 %deleted=%{$wikistate{autoindex}{deleted}};
88 elsif (ref $pagestate{index}{autoindex}{deleted}) {
90 %deleted=%{$pagestate{index}{autoindex}{deleted}};
91 delete $pagestate{index}{autoindex};
95 foreach my $dir (keys %deleted) {
96 # remove deleted page state if the deleted page is re-added,
97 # or if all its subpages are deleted
98 if ($deleted{$dir} && (exists $pages{$dir} ||
99 ! grep /^$dir\/.*/, keys %pages)) {
100 delete $deleted{$dir};
103 $wikistate{autoindex}{deleted}=\%deleted;
107 foreach my $dir (keys %dirs) {
108 if (! exists $pages{$dir} && ! $deleted{$dir} &&
109 grep /^$dir\/.*/, keys %pages) {
110 if (exists $IkiWiki::pagemtime{$dir}) {
111 # This page must have just been deleted, so
112 # don't re-add it. And remember it was
114 if (! ref $wikistate{autoindex}{deleted}) {
115 $wikistate{autoindex}{deleted}={};
117 ${$wikistate{autoindex}{deleted}}{$dir}=1;
126 foreach my $page (@needed) {