]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/autoindex.pm
autoindex: use add_autofile
[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 3.00;
7 use Encode;
9 sub import {
10         hook(type => "getsetup", id => "autoindex", call => \&getsetup);
11         hook(type => "refresh", id => "autoindex", call => \&refresh);
12 }
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => 0,
19                 },
20 }
22 sub genindex ($) {
23         my $page=shift;
24         my $file=newpagefile($page, $config{default_pageext});
26         add_autofile($file, "autoindex", sub {
27                         my $message = sprintf(gettext("creating index page %s"),
28                                 $page);
29                         debug($message);
31                         my $template = template("autoindex.tmpl");
32                         $template->param(page => $page);
33                         writefile($file, $config{srcdir}, $template->output);
35                         if ($config{rcs}) {
36                                 IkiWiki::disable_commit_hook();
37                                 IkiWiki::rcs_add($file);
38                                 IkiWiki::rcs_commit_staged(message => $message);
39                                 IkiWiki::enable_commit_hook();
40                         }
41                 });
42 }
44 sub refresh () {
45         eval q{use File::Find};
46         error($@) if $@;
47         eval q{use Cwd};
48         error($@) if $@;
49         my $origdir=getcwd();
51         my (%pages, %dirs);
52         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
53                 chdir($dir) || next;
55                 find({
56                         no_chdir => 1,
57                         wanted => sub {
58                                 my $file=decode_utf8($_);
59                                 $file=~s/^\.\/?//;
60                                 return unless length $file;
61                                 if (IkiWiki::file_pruned($file)) {
62                                         $File::Find::prune=1;
63                                 }
64                                 elsif (! -l $_) {
65                                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
66                                         return unless defined $f;
67                                         return if $f =~ /\._([^.]+)$/; # skip internal page
68                                         if (! -d _) {
69                                                 $pages{pagename($f)}=1;
70                                         }
71                                         elsif ($dir eq $config{srcdir}) {
72                                                 $dirs{$f}=1;
73                                         }
74                                 }
75                         }
76                 }, '.');
78                 chdir($origdir) || die "chdir $origdir: $!";
79         }
81         # FIXME: some of this is probably redundant with add_autofile now, and
82         # the rest should perhaps be added to the autofile machinery
84         my %deleted;
85         if (ref $wikistate{autoindex}{deleted}) {
86                 %deleted=%{$wikistate{autoindex}{deleted}};
87         }
88         elsif (ref $pagestate{index}{autoindex}{deleted}) {
89                 # compatability code
90                 %deleted=%{$pagestate{index}{autoindex}{deleted}};
91                 delete $pagestate{index}{autoindex};
92         }
94         if (keys %deleted) {
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};
101                         }
102                 }
103                 $wikistate{autoindex}{deleted}=\%deleted;
104         }
106         my @needed;
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
113                                 # deleted.
114                                 if (! ref $wikistate{autoindex}{deleted}) {
115                                         $wikistate{autoindex}{deleted}={};
116                                 }
117                                 ${$wikistate{autoindex}{deleted}}{$dir}=1;
118                         }
119                         else {
120                                 push @needed, $dir;
121                         }
122                 }
123         }
124         
125         if (@needed) {
126                 foreach my $page (@needed) {
127                         genindex($page);
128                 }
129         }