summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/autoindex.pm
blob: 0e30b9900e1ea3011789ddc6561961d610208773 (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. foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
  33. find({
  34. no_chdir => 1,
  35. wanted => sub {
  36. $_=decode_utf8($_);
  37. if (IkiWiki::file_pruned($_, $dir)) {
  38. $File::Find::prune=1;
  39. }
  40. elsif (! -l $_) {
  41. my ($f)=/$config{wiki_file_regexp}/; # untaint
  42. return unless defined $f;
  43. $f=~s/^\Q$dir\E\/?//;
  44. return unless length $f;
  45. return if $f =~ /\._([^.]+)$/; # skip internal page
  46. if (! -d _) {
  47. $pages{pagename($f)}=1;
  48. }
  49. elsif ($dir eq $config{srcdir}) {
  50. $dirs{$f}=1;
  51. }
  52. }
  53. }
  54. }, $dir);
  55. }
  56. my @needed;
  57. foreach my $dir (keys %dirs) {
  58. if (! exists $pages{$dir} && grep /^$dir\/.*/, keys %pages) {
  59. push @needed, $dir;
  60. }
  61. }
  62. if (@needed) {
  63. if ($config{rcs}) {
  64. IkiWiki::disable_commit_hook();
  65. }
  66. genindex($_) foreach @needed;
  67. if ($config{rcs}) {
  68. IkiWiki::rcs_commit_staged(
  69. gettext("automatic index generation"),
  70. undef, undef);
  71. IkiWiki::enable_commit_hook();
  72. }
  73. }
  74. } #}}}
  75. 1