5a58c820aaefa1244fd2b08947f82968ce945c37
[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 3.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=newpagefile($page, $config{default_pageext});
25
26         add_autofile($file, "autoindex", sub {
27                         my $message = sprintf(gettext("creating index page %s"),
28                                 $page);
29                         debug($message);
30
31                         my $template = template("autoindex.tmpl");
32                         $template->param(page => $page);
33                         writefile($file, $config{srcdir}, $template->output);
34
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 }
43
44 sub refresh () {
45         eval q{use File::Find};
46         error($@) if $@;
47         eval q{use Cwd};
48         error($@) if $@;
49         my $origdir=getcwd();
50
51         my (%pages, %dirs);
52         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
53                 chdir($dir) || next;
54
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                 }, '.');
77
78                 chdir($origdir) || die "chdir $origdir: $!";
79         }
80
81         # Compatibility code.
82         #
83         # {deleted} contains pages that have been deleted at some point.
84         # This plugin used to delete from the hash sometimes, but no longer
85         # does; in [[todo/autoindex_should_use_add__95__autofile]] Joey
86         # thought the old behaviour was probably a bug.
87         #
88         # The effect of listing a page in {deleted} was to avoid re-creating
89         # it; we migrate these pages to {autofile} which has the same effect.
90         # However, {autofile} contains source filenames whereas {deleted}
91         # contains page names.
92         my %deleted;
93         if (ref $wikistate{autoindex}{deleted}) {
94                 %deleted=%{$wikistate{autoindex}{deleted}};
95                 delete $wikistate{autoindex}{deleted};
96         }
97         elsif (ref $pagestate{index}{autoindex}{deleted}) {
98                 # an even older version
99                 %deleted=%{$pagestate{index}{autoindex}{deleted}};
100                 delete $pagestate{index}{autoindex};
101         }
102
103         if (keys %deleted) {
104                 foreach my $dir (keys %deleted) {
105                         my $file=newpagefile($dir, $config{default_pageext});
106                         $wikistate{autoindex}{autofile}{$file} = 1;
107                 }
108         }
109
110         foreach my $dir (keys %dirs) {
111                 if (! exists $pages{$dir} && grep /^$dir\/.*/, keys %pages) {
112                         genindex($dir);
113                 }
114         }
115 }
116
117 1