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 (@) { #{{{
21 $params{pages}="*" unless defined $params{pages};
23 # Needs to update whenever a page is added or removed, so
24 # register a dependency.
25 IkiWiki::add_depends($params{page}, $params{pages});
27 # Can't just return the linkmap here, since the htmlscrubber
28 # scrubs out all <object> tags (with good reason!)
29 # Instead, insert a placeholder tag, which will be expanded during
32 $maps{$mapnum}=\%params;
33 return "<div class=\"linkmap$mapnum\"></div>";
39 $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
41 return $params{content};
46 return "" unless exists $maps{$mapnum};
47 my %params=%{$maps{$mapnum}};
49 # Get all the items to map.
51 foreach my $item (keys %IkiWiki::links) {
52 if (IkiWiki::pagespec_match($item, $params{pages})) {
53 my $link=IkiWiki::htmlpage($item);
54 $link=IkiWiki::abs2rel($link, IkiWiki::dirname($params{page}));
55 $mapitems{$item}=$link;
59 # Use ikiwiki's function to create the file, this makes sure needed
60 # subdirs are there and does some sanity checking.
61 IkiWiki::writefile("$params{page}.png", $IkiWiki::config{destdir}, "");
63 # Run dot to create the graphic and get the map data.
64 # TODO: should really add the png to renderedfiles and call
65 # check_overwrite, but currently renderedfiles
66 # only supports listing one file per page.
71 $pid=open2(*IN, *OUT, "dot /dev/stdin -Tpng -o '$IkiWiki::config{destdir}/$params{page}.png' -Tcmapx");
76 return "failed to run dot: $@";
79 # open2 doesn't respect "use open ':utf8'"
80 binmode (IN, ':utf8');
81 binmode (OUT, ':utf8');
83 print OUT "digraph linkmap$mapnum {\n";
84 print OUT "concentrate=true;\n";
85 foreach my $item (keys %mapitems) {
86 print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
87 foreach my $link (map { IkiWiki::bestlink($item, $_) } @{$IkiWiki::links{$item}}) {
88 print OUT "\"$item\" -> \"$link\";\n"
96 my $ret="<object data=\"".
97 IkiWiki::abs2rel("$params{page}.png", IkiWiki::dirname($params{page})).
98 "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".