2 package IkiWiki::Plugin::linkmap;
11 hook(type => "getsetup", id => "linkmap", call => \&getsetup);
12 hook(type => "preprocess", id => "linkmap", call => \&preprocess);
28 # encoding explicitly in case ikiwiki is configured to accept <> or &
30 my $title = pagetitle($item, 1);
31 # it would not be necessary to encode *all* the html entities (<> would
32 # be sufficient, &" probably a good idea), as dot accepts utf8, but it
34 $title = encode_entities($title);
41 $params{pages}="*" unless defined $params{pages};
44 my $connected=IkiWiki::yesno($params{connected});
46 # Get all the items to map.
47 my %mapitems = map { $_ => urlto($_, $params{destpage}) }
48 pagespec_match_list($params{page}, $params{pages},
49 # update when a page is added or removed, or its
51 deptype => deptype("presence", "links"));
53 my $dest=$params{page}."/linkmap.png";
55 # Use ikiwiki's function to create the file, this makes sure needed
56 # subdirs are there and does some sanity checking.
57 will_render($params{page}, $dest);
58 writefile($dest, $config{destdir}, "");
60 # Run dot to create the graphic and get the map data.
63 $SIG{PIPE}=sub { $sigpipe=1 };
64 $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
66 # open2 doesn't respect "use open ':utf8'"
67 binmode (IN, ':utf8');
68 binmode (OUT, ':utf8');
70 print OUT "digraph linkmap$mapnum {\n";
71 print OUT "concentrate=true;\n";
72 print OUT "charset=\"utf-8\";\n";
73 print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
74 if defined $params{width} and defined $params{height};
78 if (! $shown{$item}) {
79 print OUT pageescape($item)." [shape=box,href=\"$mapitems{$item}\"];\n";
83 foreach my $item (keys %mapitems) {
84 $show->($item) unless $connected;
85 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
86 next unless length $link and $mapitems{$link};
87 foreach my $endpoint ($item, $link) {
90 print OUT pageescape($item)." -> ".pageescape($link).";\n";
94 close OUT || error gettext("failed to run dot");
97 my $ret="<img src=\"".urlto($dest, $params{destpage}).
98 "\" alt=\"".gettext("linkmap").
99 "\" usemap=\"#linkmap$mapnum\" />\n".
101 close IN || error gettext("failed to run dot");
105 error gettext("failed to run dot");
107 $SIG{PIPE}="DEFAULT";
108 error gettext("failed to run dot") if $sigpipe;