summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/autoindex.pm
blob: 8179ee1e72a28ba5dac9dc6911cf35b2477ac7bf (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 => "refresh", id => "autoindex", call => \&refresh);
  9. } # }}}
  10. sub genindex ($) { #{{{
  11. my $page=shift;
  12. my $file=$page.".".$config{default_pageext};
  13. my $template=template("autoindex.tmpl");
  14. $template->param(page => $page);
  15. writefile($file, $config{srcdir}, $template->output);
  16. if ($config{rcs}) {
  17. IkiWiki::rcs_add($file);
  18. }
  19. } #}}}
  20. sub refresh () { #{{{
  21. eval q{use File::Find};
  22. error($@) if $@;
  23. my (%pages, %dirs);
  24. find({
  25. no_chdir => 1,
  26. wanted => sub {
  27. $_=decode_utf8($_);
  28. if (IkiWiki::file_pruned($_, $config{srcdir})) {
  29. $File::Find::prune=1;
  30. }
  31. elsif (! -l $_) {
  32. my ($f)=/$config{wiki_file_regexp}/; # untaint
  33. return unless defined $f;
  34. $f=~s/^\Q$config{srcdir}\E\/?//;
  35. return unless length $f;
  36. if (! -d _) {
  37. $pages{pagename($f)}=1;
  38. }
  39. else {
  40. $dirs{$f}=1;
  41. }
  42. }
  43. }
  44. }, $config{srcdir});
  45. my @needed;
  46. foreach my $dir (keys %dirs) {
  47. if (! exists $pages{$dir}) {
  48. push @needed, $dir;
  49. }
  50. }
  51. if (@needed) {
  52. if ($config{rcs}) {
  53. IkiWiki::disable_commit_hook();
  54. }
  55. genindex($_) foreach @needed;
  56. if ($config{rcs}) {
  57. IkiWiki::rcs_commit_staged(
  58. gettext("automatic index generation"),
  59. undef, undef);
  60. IkiWiki::enable_commit_hook();
  61. }
  62. }
  63. } #}}}
  64. 1