]> scripts.mit.edu Git - www/ikiwiki.git/blob - IkiWiki/Plugin/linkmap.pm
presence dependencies not needed
[www/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 relevant page's links change.
32         add_depends($params{page}, $params{pages}, links => 1);
33         
34         # Can't just return the linkmap here, since the htmlscrubber
35         # scrubs out all <object> tags (with good reason!)
36         # Instead, insert a placeholder tag, which will be expanded during
37         # formatting.
38         $mapnum++;
39         $maps{$mapnum}=\%params;
40         return "<div class=\"linkmap$mapnum\"></div>";
41 }
42
43 sub format (@) {
44         my %params=@_;
45
46         $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
47
48         return $params{content};
49 }
50
51 sub genmap ($) {
52         my $mapnum=shift;
53         return "" unless exists $maps{$mapnum};
54         my %params=%{$maps{$mapnum}};
55
56         # Get all the items to map.
57         my %mapitems = ();
58         foreach my $item (keys %links) {
59                 if (pagespec_match($item, $params{pages}, location => $params{page})) {
60                         $mapitems{$item}=urlto($item, $params{destpage});
61                 }
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