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