summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/autoindex.pm
blob: c71d73349c091021ab1d8083292fbd8888ce7d82 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::autoindex;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.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=newpagefile($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 %deleted;
  57. if (ref $wikistate{autoindex}{deleted}) {
  58. %deleted=%{$wikistate{autoindex}{deleted}};
  59. }
  60. elsif (ref $pagestate{index}{autoindex}{deleted}) {
  61. # compatability code
  62. %deleted=%{$pagestate{index}{autoindex}{deleted}};
  63. delete $pagestate{index}{autoindex};
  64. }
  65. if (keys %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. $wikistate{autoindex}{deleted}=\%deleted;
  75. }
  76. my @needed;
  77. foreach my $dir (keys %dirs) {
  78. if (! exists $pages{$dir} && ! $deleted{$dir} &&
  79. grep /^$dir\/.*/, keys %pages) {
  80. if (exists $IkiWiki::pagemtime{$dir}) {
  81. # This page must have just been deleted, so
  82. # don't re-add it. And remember it was
  83. # deleted.
  84. if (! ref $wikistate{autoindex}{deleted}) {
  85. $wikistate{autoindex}{deleted}={};
  86. }
  87. ${$wikistate{autoindex}{deleted}}{$dir}=1;
  88. }
  89. else {
  90. push @needed, $dir;
  91. }
  92. }
  93. }
  94. if (@needed) {
  95. if ($config{rcs}) {
  96. IkiWiki::disable_commit_hook();
  97. }
  98. foreach my $page (@needed) {
  99. genindex($page);
  100. }
  101. if ($config{rcs}) {
  102. IkiWiki::rcs_commit_staged(
  103. gettext("automatic index generation"),
  104. undef, undef);
  105. IkiWiki::enable_commit_hook();
  106. }
  107. }
  108. }
  109. 1