]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/linkmap.pm
Add NetBSD-style 2-clause BSD license.
[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 (keys %links) {
60                 if (pagespec_match($item, $params{pages}, location => $params{page})) {
61                         $mapitems{$item}=urlto($item, $params{destpage});
62                 }
63         }
64
65         my $dest=$params{page}."/linkmap.png";
66
67         # Use ikiwiki's function to create the file, this makes sure needed
68         # subdirs are there and does some sanity checking.
69         will_render($params{page}, $dest);
70         writefile($dest, $config{destdir}, "");
71
72         # Run dot to create the graphic and get the map data.
73         my $pid;
74         my $sigpipe=0;
75         $SIG{PIPE}=sub { $sigpipe=1 };
76         $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
77         
78         # open2 doesn't respect "use open ':utf8'"
79         binmode (IN, ':utf8'); 
80         binmode (OUT, ':utf8'); 
81
82         print OUT "digraph linkmap$mapnum {\n";
83         print OUT "concentrate=true;\n";
84         print OUT "charset=\"utf-8\";\n";
85         print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
86                 if defined $params{width} and defined $params{height};
87         foreach my $item (keys %mapitems) {
88                 print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
89                 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
90                         print OUT "\"$item\" -> \"$link\";\n"
91                                 if $mapitems{$link};
92                 }
93         }
94         print OUT "}\n";
95         close OUT;
96
97         local $/=undef;
98         my $ret="<object data=\"".urlto($dest, $params{destpage}).
99                "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
100                 <IN>.
101                 "</object>";
102         close IN;
103         
104         waitpid $pid, 0;
105         $SIG{PIPE}="DEFAULT";
106         error gettext("failed to run dot") if $sigpipe;
107
108         return $ret;
109 }
110
111 1