summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: 6741f90817dca6895ab1eae90f898c4fd4c40176 (plain)
  1. #!/usr/bin/perl
  2. # Provides a list of broken links.
  3. package IkiWiki::Plugin::brokenlinks;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. sub import { #{{{
  8. hook(type => "preprocess", id => "brokenlinks", call => \&preprocess);
  9. } # }}}
  10. sub preprocess (@) { #{{{
  11. my %params=@_;
  12. $params{pages}="*" unless defined $params{pages};
  13. # Needs to update whenever a page is added or removed, so
  14. # register a dependency.
  15. add_depends($params{page}, $params{pages});
  16. my @broken;
  17. foreach my $page (keys %links) {
  18. if (pagespec_match($page, $params{pages})) {
  19. foreach my $link (@{$links{$page}}) {
  20. next if $link =~ /.*\/discussion/i && $config{discussion};
  21. my $bestlink=bestlink($page, $link);
  22. next if length $bestlink;
  23. push @broken,
  24. htmllink($page, $params{destpage}, $link, 1).
  25. " from ".
  26. htmllink($params{page}, $params{destpage}, $page, 1);
  27. }
  28. }
  29. }
  30. return "There are no broken links!" unless @broken;
  31. my %seen;
  32. return "<ul>\n".join("\n", map { "<li>$_</li>" } grep { ! $seen{$_}++ } sort @broken)."</ul>\n";
  33. } # }}}
  34. 1