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 # 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
36 $maps{$mapnum}=\%params;
37 return "<div class=\"linkmap$mapnum\"></div>";
43 $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
45 return $params{content};
50 return "" unless exists $maps{$mapnum};
51 my %params=%{$maps{$mapnum}};
52 my $connected=IkiWiki::yesno($params{connected});
54 # Get all the items to map.
55 my %mapitems = map { $_ => urlto($_, $params{destpage}) }
56 pagespec_match_list($params{page}, $params{pages},
57 # update when a page is added or removed, or its
59 deptype => deptype("presence", "links"));
61 my $dest=$params{page}."/linkmap.png";
63 # Use ikiwiki's function to create the file, this makes sure needed
64 # subdirs are there and does some sanity checking.
65 will_render($params{page}, $dest);
66 writefile($dest, $config{destdir}, "");
68 # Run dot to create the graphic and get the map data.
71 $SIG{PIPE}=sub { $sigpipe=1 };
72 $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
74 # open2 doesn't respect "use open ':utf8'"
75 binmode (IN, ':utf8');
76 binmode (OUT, ':utf8');
78 print OUT "digraph linkmap$mapnum {\n";
79 print OUT "concentrate=true;\n";
80 print OUT "charset=\"utf-8\";\n";
81 print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
82 if defined $params{width} and defined $params{height};
86 if (! $shown{$item}) {
87 print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
91 foreach my $item (keys %mapitems) {
92 $show->($item) unless $connected;
93 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
94 next unless length $link and $mapitems{$link};
95 foreach my $endpoint ($item, $link) {
98 print OUT "\"$item\" -> \"$link\";\n";
102 close OUT || error gettext("failed to run dot");
105 my $ret="<object data=\"".urlto($dest, $params{destpage}).
106 "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
109 close IN || error gettext("failed to run dot");
113 error gettext("failed to run dot");
115 $SIG{PIPE}="DEFAULT";
116 error gettext("failed to run dot") if $sigpipe;