summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/brokenlinks.pm
blob: a4da7161e22d8707664b41484839087933893b93 (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 2.00;
  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}, location => $params{page})) {
  19. my $discussion=gettext("discussion");
  20. foreach my $link (@{$links{$page}}) {
  21. next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
  22. my $bestlink=bestlink($page, $link);
  23. next if length $bestlink;
  24. push @broken, sprintf(gettext("%s from %s"),
  25. htmllink($page, $params{destpage}, $link, noimageinline => 1),
  26. htmllink($params{page}, $params{destpage}, $page, noimageinline => 1));
  27. }
  28. }
  29. }
  30. return gettext("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