]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/autoindex.pm
Merge branch 'master' of git://git.ikiwiki.info
[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 => "checkconfig", id => "autoindex", call => \&checkconfig);
11         hook(type => "getsetup", id => "autoindex", call => \&getsetup);
12         hook(type => "refresh", id => "autoindex", call => \&refresh);
13         IkiWiki::loadplugin("transient");
14 }
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => 0,
21                 },
22                 autoindex_commit => {
23                         type => "boolean",
24                         example => 1,
25                         default => 1,
26                         description => "commit autocreated index pages",
27                         safe => 1,
28                         rebuild => 0,
29                 },
30 }
32 sub checkconfig () {
33         if (! defined $config{autoindex_commit}) {
34                 $config{autoindex_commit} = 1;
35         }
36 }
38 sub genindex ($) {
39         my $page=shift;
40         my $file=newpagefile($page, $config{default_pageext});
42         add_autofile($file, "autoindex", sub {
43                         my $message = sprintf(gettext("creating index page %s"),
44                                 $page);
45                         debug($message);
47                         my $dir = $config{srcdir};
48                         if (! $config{autoindex_commit}) {
49                                 $dir = $IkiWiki::Plugin::transient::transientdir;
50                         }
52                         my $template = template("autoindex.tmpl");
53                         $template->param(page => $page);
54                         writefile($file, $dir, $template->output);
56                         if ($config{rcs} && $config{autoindex_commit}) {
57                                 IkiWiki::disable_commit_hook();
58                                 IkiWiki::rcs_add($file);
59                                 IkiWiki::rcs_commit_staged(message => $message);
60                                 IkiWiki::enable_commit_hook();
61                         }
62                 });
63 }
65 sub refresh () {
66         eval q{use File::Find};
67         error($@) if $@;
68         eval q{use Cwd};
69         error($@) if $@;
70         my $origdir=getcwd();
72         my (%pages, %dirs);
73         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
74                 next if $dir eq $IkiWiki::Plugin::transient::transientdir;
75                 chdir($dir) || next;
77                 find({
78                         no_chdir => 1,
79                         wanted => sub {
80                                 my $file=decode_utf8($_);
81                                 $file=~s/^\.\/?//;
82                                 return unless length $file;
83                                 if (IkiWiki::file_pruned($file)) {
84                                         $File::Find::prune=1;
85                                 }
86                                 elsif (! -l $_) {
87                                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
88                                         return unless defined $f;
89                                         return if $f =~ /\._([^.]+)$/; # skip internal page
90                                         if (! -d _) {
91                                                 $pages{pagename($f)}=1;
92                                         }
93                                         elsif ($dir eq $config{srcdir}) {
94                                                 $dirs{$f}=1;
95                                         }
96                                 }
97                         }
98                 }, '.');
100                 chdir($origdir) || die "chdir $origdir: $!";
101         }
103         # Compatibility code.
104         #
105         # {deleted} contains pages that have been deleted at some point.
106         # This plugin used to delete from the hash sometimes, but no longer
107         # does; in [[todo/autoindex_should_use_add__95__autofile]] Joey
108         # thought the old behaviour was probably a bug.
109         #
110         # The effect of listing a page in {deleted} was to avoid re-creating
111         # it; we migrate these pages to {autofile} which has the same effect.
112         # However, {autofile} contains source filenames whereas {deleted}
113         # contains page names.
114         my %deleted;
115         if (ref $wikistate{autoindex}{deleted}) {
116                 %deleted=%{$wikistate{autoindex}{deleted}};
117                 delete $wikistate{autoindex}{deleted};
118         }
119         elsif (ref $pagestate{index}{autoindex}{deleted}) {
120                 # an even older version
121                 %deleted=%{$pagestate{index}{autoindex}{deleted}};
122                 delete $pagestate{index}{autoindex};
123         }
125         if (keys %deleted) {
126                 foreach my $dir (keys %deleted) {
127                         my $file=newpagefile($dir, $config{default_pageext});
128                         $wikistate{autoindex}{autofile}{$file} = 1;
129                 }
130         }
132         foreach my $dir (keys %dirs) {
133                 if (! exists $pages{$dir} && grep /^$dir\/.*/, keys %pages) {
134                         genindex($dir);
135                 }
136         }