linkmap: Add option to omit disconnected pages from the map.
[ikiwiki] / IkiWiki / Plugin / linkmap.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::linkmap;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use IPC::Open2;
8
9 sub import {
10         hook(type => "getsetup", id => "linkmap", call => \&getsetup);
11         hook(type => "preprocess", id => "linkmap", call => \&preprocess);
12         hook(type => "format", id => "linkmap", call => \&format);
13 }
14
15 sub getsetup () {
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => undef,
20                 },
21 }
22
23 my $mapnum=0;
24 my %maps;
25
26 sub preprocess (@) {
27         my %params=@_;
28
29         $params{pages}="*" unless defined $params{pages};
30         
31         # Can't just return the linkmap here, since the htmlscrubber
32         # scrubs out all <object> tags (with good reason!)
33         # Instead, insert a placeholder tag, which will be expanded during
34         # formatting.
35         $mapnum++;
36         $maps{$mapnum}=\%params;
37         return "<div class=\"linkmap$mapnum\"></div>";
38 }
39
40 sub format (@) {
41         my %params=@_;
42
43         $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
44
45         return $params{content};
46 }
47
48 sub genmap ($) {
49         my $mapnum=shift;
50         return "" unless exists $maps{$mapnum};
51         my %params=%{$maps{$mapnum}};
52         my $connected=IkiWiki::yesno($params{connected});
53
54         # Get all the items to map.
55         my %mapitems = map { $_ => urlto($_, $params{destpage}) }
56                 pagespec_match_list($params{page}, $params{pages},
57                         # update when a page is added or removed, or its
58                         # links change
59                         deptype => deptype("presence", "links"));
60
61         my $dest=$params{page}."/linkmap.png";
62
63         # Use ikiwiki's function to create the file, this makes sure needed
64         # subdirs are there and does some sanity checking.
65         will_render($params{page}, $dest);
66         writefile($dest, $config{destdir}, "");
67
68         # Run dot to create the graphic and get the map data.
69         my $pid;
70         my $sigpipe=0;
71         $SIG{PIPE}=sub { $sigpipe=1 };
72         $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
73         
74         # open2 doesn't respect "use open ':utf8'"
75         binmode (IN, ':utf8'); 
76         binmode (OUT, ':utf8'); 
77
78         print OUT "digraph linkmap$mapnum {\n";
79         print OUT "concentrate=true;\n";
80         print OUT "charset=\"utf-8\";\n";
81         print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
82                 if defined $params{width} and defined $params{height};
83         my %shown;
84         my $show=sub {
85                 my $item=shift;
86                 if (! $shown{$item}) {
87                         print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
88                         $shown{$item}=1;
89                 }
90         };
91         foreach my $item (keys %mapitems) {
92                 $show->($item) unless $connected;
93                 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
94                         next unless length $link and $mapitems{$link};
95                         foreach my $endpoint ($item, $link) {
96                                 $show->($endpoint);
97                         }
98                         print OUT "\"$item\" -> \"$link\";\n";
99                 }
100         }
101         print OUT "}\n";
102         close OUT || error gettext("failed to run dot");
103
104         local $/=undef;
105         my $ret="<object data=\"".urlto($dest, $params{destpage}).
106                "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
107                 <IN>.
108                 "</object>";
109         close IN || error gettext("failed to run dot");
110         
111         waitpid $pid, 0;
112         if ($?) {
113                 error gettext("failed to run dot");
114         }
115         $SIG{PIPE}="DEFAULT";
116         error gettext("failed to run dot") if $sigpipe;
117
118         return $ret;
119 }
120
121 1