]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/brokenlinks.pm
* More compact output for the brokenlinks plugin.
[git.ikiwiki.info.git] / IkiWiki / Plugin / brokenlinks.pm
1 #!/usr/bin/perl
2 # Provides a list of broken links.
3 package IkiWiki::Plugin::brokenlinks;
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
9 sub import { #{{{
10         hook(type => "preprocess", id => "brokenlinks", call => \&preprocess);
11 } # }}}
13 sub preprocess (@) { #{{{
14         my %params=@_;
15         $params{pages}="*" unless defined $params{pages};
16         
17         # Needs to update whenever a page is added or removed, so
18         # register a dependency.
19         add_depends($params{page}, $params{pages});
20         
21         my %broken;
22         foreach my $page (keys %links) {
23                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
24                         my $discussion=gettext("discussion");
25                         foreach my $link (@{$links{$page}}) {
26                                 next if $link =~ /.*\/\Q$discussion\E/i && $config{discussion};
27                                 my $bestlink=bestlink($page, $link);
28                                 next if length $bestlink;
29                                 push @{$broken{$link}}, $page;
30                         }
31                 }
32         }
34         my @broken;
35         foreach my $link (keys %broken) {
36                 my $page=$broken{$link}->[0];
37                 push @broken, sprintf(gettext("%s from %s"),
38                         htmllink($page, $params{destpage}, $link, noimageinline => 1),
39                         join(", ", map {
40                                 htmllink($params{page}, $params{destpage}, $_,  noimageinline => 1)
41                         } @{$broken{$link}}));
42         }
43         
44         return gettext("There are no broken links!") unless %broken;
45         return "<ul>\n"
46                 .join("\n",
47                         map {
48                                 "<li>$_</li>"
49                         }
50                         sort @broken)
51                 ."</ul>\n";
52 } # }}}
54 1