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