]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/autoindex.pm
[tiny] add a link to meta plugin page
[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});
25         my $template=template("autoindex.tmpl");
26         $template->param(page => $page);
27         writefile($file, $config{srcdir}, $template->output);
28         if ($config{rcs}) {
29                 IkiWiki::rcs_add($file);
30         }
31 }
33 sub refresh () {
34         eval q{use File::Find};
35         error($@) if $@;
37         my (%pages, %dirs);
38         foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
39                 find({
40                         no_chdir => 1,
41                         wanted => sub {
42                                 $_=decode_utf8($_);
43                                 if (IkiWiki::file_pruned($_, $dir)) {
44                                         $File::Find::prune=1;
45                                 }
46                                 elsif (! -l $_) {
47                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
48                                         return unless defined $f;
49                                         $f=~s/^\Q$dir\E\/?//;
50                                         return unless length $f;
51                                         return if $f =~ /\._([^.]+)$/; # skip internal page
52                                         if (! -d _) {
53                                                 $pages{pagename($f)}=1;
54                                         }
55                                         elsif ($dir eq $config{srcdir}) {
56                                                 $dirs{$f}=1;
57                                         }
58                                 }
59                         }
60                 }, $dir);
61         }
62         
63         my %deleted;
64         if (ref $pagestate{index}{autoindex}{deleted}) {
65                %deleted=%{$pagestate{index}{autoindex}{deleted}};
66                 foreach my $dir (keys %deleted) {
67                         # remove deleted page state if the deleted page is re-added,
68                         # or if all its subpages are deleted
69                         if ($deleted{$dir} && (exists $pages{$dir} ||
70                                                ! grep /^$dir\/.*/, keys %pages)) {
71                                 delete $deleted{$dir};
72                         }
73                 }
74                 $pagestate{index}{autoindex}{deleted}=\%deleted;
75         }
77         my @needed;
78         foreach my $dir (keys %dirs) {
79                 if (! exists $pages{$dir} && ! $deleted{$dir} &&
80                     grep /^$dir\/.*/, keys %pages) {
81                         if (exists $IkiWiki::pagemtime{$dir}) {
82                                 # This page must have just been deleted, so
83                                 # don't re-add it. And remember it was
84                                 # deleted.
85                                 if (! ref $pagestate{index}{autoindex}{deleted}) {
86                                         $pagestate{index}{autoindex}{deleted}={};
87                                 }
88                                 ${$pagestate{index}{autoindex}{deleted}}{$dir}=1;
89                         }
90                         else {
91                                 push @needed, $dir;
92                         }
93                 }
94         }
95         
96         if (@needed) {
97                 if ($config{rcs}) {
98                         IkiWiki::disable_commit_hook();
99                 }
100                 foreach my $page (@needed) {
101                         genindex($page);
102                 }
103                 if ($config{rcs}) {
104                         IkiWiki::rcs_commit_staged(
105                                 gettext("automatic index generation"),
106                                 undef, undef);
107                         IkiWiki::enable_commit_hook();
108                 }
109         }