2 package IkiWiki::Plugin::linkmap;
10 hook(type => "preprocess", id => "linkmap", call => \&preprocess);
11 hook(type => "format", id => "linkmap", call => \&format);
17 sub preprocess (@) { #{{{
20 $params{pages}="*" unless defined $params{pages};
22 # Needs to update whenever a page is added or removed, so
23 # register a dependency.
24 add_depends($params{page}, $params{pages});
26 # Can't just return the linkmap here, since the htmlscrubber
27 # scrubs out all <object> tags (with good reason!)
28 # Instead, insert a placeholder tag, which will be expanded during
31 $maps{$mapnum}=\%params;
32 return "<div class=\"linkmap$mapnum\"></div>";
38 $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
40 return $params{content};
45 return "" unless exists $maps{$mapnum};
46 my %params=%{$maps{$mapnum}};
48 # Get all the items to map.
50 foreach my $item (keys %links) {
51 if (pagespec_match($item, $params{pages}, location => $params{page})) {
52 $mapitems{$item}=urlto($item, $params{destpage});
56 my $dest=$params{page}."/linkmap.png";
58 # Use ikiwiki's function to create the file, this makes sure needed
59 # subdirs are there and does some sanity checking.
60 will_render($params{page}, $dest);
61 writefile($dest, $config{destdir}, "");
63 # Run dot to create the graphic and get the map data.
66 $SIG{PIPE}=sub { $sigpipe=1 };
67 $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
69 # open2 doesn't respect "use open ':utf8'"
70 binmode (IN, ':utf8');
71 binmode (OUT, ':utf8');
73 print OUT "digraph linkmap$mapnum {\n";
74 print OUT "concentrate=true;\n";
75 print OUT "charset=\"utf-8\";\n";
76 print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
77 if defined $params{width} and defined $params{height};
78 foreach my $item (keys %mapitems) {
79 print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
80 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
81 print OUT "\"$item\" -> \"$link\";\n"
89 my $ret="<object data=\"".urlto($dest, $params{destpage}).
90 "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
97 error gettext("failed to run dot") if $sigpipe;