]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/linkmap.pm
0137476acfd60b5aa07693960c59743bab4214df
[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         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         # Needs to update whenever a page is added or removed, so
32         # register a dependency.
33         add_depends($params{page}, $params{pages});
34         
35         # Can't just return the linkmap here, since the htmlscrubber
36         # scrubs out all <object> tags (with good reason!)
37         # Instead, insert a placeholder tag, which will be expanded during
38         # formatting.
39         $mapnum++;
40         $maps{$mapnum}=\%params;
41         return "<div class=\"linkmap$mapnum\"></div>";
42 }
43
44 sub format (@) {
45         my %params=@_;
46
47         $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
48
49         return $params{content};
50 }
51
52 sub genmap ($) {
53         my $mapnum=shift;
54         return "" unless exists $maps{$mapnum};
55         my %params=%{$maps{$mapnum}};
56
57         # Get all the items to map.
58         my %mapitems = ();
59         foreach my $item (pagespec_match_list([keys %links],
60                                 $params{pages}, location => $params{page})) {
61                 $mapitems{$item}=urlto($item, $params{destpage});
62         }
63
64         my $dest=$params{page}."/linkmap.png";
65
66         # Use ikiwiki's function to create the file, this makes sure needed
67         # subdirs are there and does some sanity checking.
68         will_render($params{page}, $dest);
69         writefile($dest, $config{destdir}, "");
70
71         # Run dot to create the graphic and get the map data.
72         my $pid;
73         my $sigpipe=0;
74         $SIG{PIPE}=sub { $sigpipe=1 };
75         $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
76         
77         # open2 doesn't respect "use open ':utf8'"
78         binmode (IN, ':utf8'); 
79         binmode (OUT, ':utf8'); 
80
81         print OUT "digraph linkmap$mapnum {\n";
82         print OUT "concentrate=true;\n";
83         print OUT "charset=\"utf-8\";\n";
84         print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
85                 if defined $params{width} and defined $params{height};
86         foreach my $item (keys %mapitems) {
87                 print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
88                 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
89                         print OUT "\"$item\" -> \"$link\";\n"
90                                 if $mapitems{$link};
91                 }
92         }
93         print OUT "}\n";
94         close OUT;
95
96         local $/=undef;
97         my $ret="<object data=\"".urlto($dest, $params{destpage}).
98                "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
99                 <IN>.
100                 "</object>";
101         close IN;
102         
103         waitpid $pid, 0;
104         $SIG{PIPE}="DEFAULT";
105         error gettext("failed to run dot") if $sigpipe;
106
107         return $ret;
108 }
109
110 1