]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/linkmap.pm
further clarification
[ikiwiki.git] / 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 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                         section => "widget",
20                 },
21 }
22
23 my $mapnum=0;
24
25 sub preprocess (@) {
26         my %params=@_;
27
28         $params{pages}="*" unless defined $params{pages};
29         
30         $mapnum++;
31         my $connected=IkiWiki::yesno($params{connected});
32
33         # Get all the items to map.
34         my %mapitems = map { $_ => urlto($_, $params{destpage}) }
35                 pagespec_match_list($params{page}, $params{pages},
36                         # update when a page is added or removed, or its
37                         # links change
38                         deptype => deptype("presence", "links"));
39
40         my $dest=$params{page}."/linkmap.png";
41
42         # Use ikiwiki's function to create the file, this makes sure needed
43         # subdirs are there and does some sanity checking.
44         will_render($params{page}, $dest);
45         writefile($dest, $config{destdir}, "");
46
47         # Run dot to create the graphic and get the map data.
48         my $pid;
49         my $sigpipe=0;
50         $SIG{PIPE}=sub { $sigpipe=1 };
51         $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
52         
53         # open2 doesn't respect "use open ':utf8'"
54         binmode (IN, ':utf8'); 
55         binmode (OUT, ':utf8'); 
56
57         print OUT "digraph linkmap$mapnum {\n";
58         print OUT "concentrate=true;\n";
59         print OUT "charset=\"utf-8\";\n";
60         print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
61                 if defined $params{width} and defined $params{height};
62         my %shown;
63         my $show=sub {
64                 my $item=shift;
65                 if (! $shown{$item}) {
66                         print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
67                         $shown{$item}=1;
68                 }
69         };
70         foreach my $item (keys %mapitems) {
71                 $show->($item) unless $connected;
72                 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
73                         next unless length $link and $mapitems{$link};
74                         foreach my $endpoint ($item, $link) {
75                                 $show->($endpoint);
76                         }
77                         print OUT "\"$item\" -> \"$link\";\n";
78                 }
79         }
80         print OUT "}\n";
81         close OUT || error gettext("failed to run dot");
82
83         local $/=undef;
84         my $ret="<img src=\"".urlto($dest, $params{destpage}).
85                "\" alt=\"".gettext("linkmap").
86                "\" usemap=\"#linkmap$mapnum\" />\n".
87                 <IN>;
88         close IN || error gettext("failed to run dot");
89         
90         waitpid $pid, 0;
91         if ($?) {
92                 error gettext("failed to run dot");
93         }
94         $SIG{PIPE}="DEFAULT";
95         error gettext("failed to run dot") if $sigpipe;
96
97         return $ret;
98 }
99
100 1