2 package IkiWiki::Plugin::linkmap;
10 hook(type => "getsetup", id => "linkmap", call => \&getsetup);
11 hook(type => "preprocess", id => "linkmap", call => \&preprocess);
12 hook(type => "format", id => "linkmap", call => \&format);
29 $params{pages}="*" unless defined $params{pages};
31 # Needs to update whenever a relevant page's links change.
32 add_depends($params{page}, $params{pages}, links => 1);
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
39 $maps{$mapnum}=\%params;
40 return "<div class=\"linkmap$mapnum\"></div>";
46 $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
48 return $params{content};
53 return "" unless exists $maps{$mapnum};
54 my %params=%{$maps{$mapnum}};
56 # Get all the items to map.
58 foreach my $item (keys %links) {
59 if (pagespec_match($item, $params{pages}, location => $params{page})) {
60 $mapitems{$item}=urlto($item, $params{destpage});
64 my $dest=$params{page}."/linkmap.png";
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}, "");
71 # Run dot to create the graphic and get the map data.
74 $SIG{PIPE}=sub { $sigpipe=1 };
75 $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
77 # open2 doesn't respect "use open ':utf8'"
78 binmode (IN, ':utf8');
79 binmode (OUT, ':utf8');
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"
97 my $ret="<object data=\"".urlto($dest, $params{destpage}).
98 "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
104 $SIG{PIPE}="DEFAULT";
105 error gettext("failed to run dot") if $sigpipe;