summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/autoindex.pm
blob: ef22ec8a3e9a244d0a4fbd7cf2471827f6f55599 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::autoindex;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. use Encode;
  7. sub import { #{{{
  8. hook(type => "getsetup", id => "autoindex", call => \&getsetup);
  9. hook(type => "refresh", id => "autoindex", call => \&refresh);
  10. } # }}}
  11. sub getsetup () { #{{{
  12. return
  13. plugin => {
  14. safe => 1,
  15. rebuild => 0,
  16. },
  17. } #}}}
  18. sub genindex ($) { #{{{
  19. my $page=shift;
  20. my $file=$page.".".$config{default_pageext};
  21. my $template=template("autoindex.tmpl");
  22. $template->param(page => $page);
  23. writefile($file, $config{srcdir}, $template->output);
  24. if ($config{rcs}) {
  25. IkiWiki::rcs_add($file);
  26. }
  27. } #}}}
  28. sub refresh () { #{{{
  29. eval q{use File::Find};
  30. error($@) if $@;
  31. my (%pages, %dirs);
  32. find({
  33. no_chdir => 1,
  34. wanted => sub {
  35. $_=decode_utf8($_);
  36. if (IkiWiki::file_pruned($_, $config{srcdir})) {
  37. $File::Find::prune=1;
  38. }
  39. elsif (! -l $_) {
  40. my ($f)=/$config{wiki_file_regexp}/; # untaint
  41. return unless defined $f;
  42. $f=~s/^\Q$config{srcdir}\E\/?//;
  43. return unless length $f;
  44. if (! -d _) {
  45. $pages{pagename($f)}=1;
  46. }
  47. else {
  48. $dirs{$f}=1;
  49. }
  50. }
  51. }
  52. }, $config{srcdir});
  53. my @needed;
  54. foreach my $dir (keys %dirs) {
  55. if (! exists $pages{$dir}) {
  56. push @needed, $dir;
  57. }
  58. }
  59. if (@needed) {
  60. if ($config{rcs}) {
  61. IkiWiki::disable_commit_hook();
  62. }
  63. genindex($_) foreach @needed;
  64. if ($config{rcs}) {
  65. IkiWiki::rcs_commit_staged(
  66. gettext("automatic index generation"),
  67. undef, undef);
  68. IkiWiki::enable_commit_hook();
  69. }
  70. }
  71. } #}}}
  72. 1