2 package IkiWiki::Plugin::linkmap;
10 IkiWiki::hook(type => "preprocess", id => "linkmap",
11 call => \&preprocess);
12 IkiWiki::hook(type => "format", id => "linkmap",
19 sub preprocess (@) { #{{{
22 $params{pages}="*" unless defined $params{pages};
24 # Needs to update whenever a page is added or removed, so
25 # register a dependency.
26 IkiWiki::add_depends($params{page}, $params{pages});
28 # Can't just return the linkmap here, since the htmlscrubber
29 # scrubs out all <object> tags (with good reason!)
30 # Instead, insert a placeholder tag, which will be expanded during
33 $maps{$mapnum}=\%params;
34 return "<div class=\"linkmap$mapnum\"></div>";
40 $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
42 return $params{content};
47 return "" unless exists $maps{$mapnum};
48 my %params=%{$maps{$mapnum}};
50 # Get all the items to map.
52 foreach my $item (keys %IkiWiki::links) {
53 if (IkiWiki::pagespec_match($item, $params{pages})) {
54 my $link=IkiWiki::htmlpage($item);
55 $link=IkiWiki::abs2rel($link, IkiWiki::dirname($params{page}));
56 $mapitems{$item}=$link;
60 # Use ikiwiki's function to create the file, this makes sure needed
61 # subdirs are there and does some sanity checking.
62 IkiWiki::writefile("$params{page}.png", $IkiWiki::config{destdir}, "");
64 # Run dot to create the graphic and get the map data.
65 # TODO: should really add the png to renderedfiles and call
66 # check_overwrite, but currently renderedfiles
67 # only supports listing one file per page.
72 $pid=open2(*IN, *OUT, "dot -Tpng -o '$IkiWiki::config{destdir}/$params{page}.png' -Tcmapx");
77 return "failed to run dot: $@";
80 # open2 doesn't respect "use open ':utf8'"
81 binmode (IN, ':utf8');
82 binmode (OUT, ':utf8');
84 print OUT "digraph linkmap$mapnum {\n";
85 print OUT "concentrate=true;\n";
86 print OUT "charset=\"utf-8\";\n";
87 print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
88 if defined $params{width} and defined $params{height};
89 foreach my $item (keys %mapitems) {
90 print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
91 foreach my $link (map { IkiWiki::bestlink($item, $_) } @{$IkiWiki::links{$item}}) {
92 print OUT "\"$item\" -> \"$link\";\n"
100 my $ret="<object data=\"".
101 IkiWiki::abs2rel("$params{page}.png", IkiWiki::dirname($params{page})).
102 "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".