inline: Fix regression in feed titles. Closes: #610878 (Thanks, Paul Wise)
[ikiwiki.git] / IkiWiki / Plugin / linkmap.pm
blobac26e072e9dc7afa18ad33d5fa2049d347f291bb
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::linkmap;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use IPC::Open2;
9 sub import {
10 hook(type => "getsetup", id => "linkmap", call => \&getsetup);
11 hook(type => "preprocess", id => "linkmap", call => \&preprocess);
14 sub getsetup () {
15 return
16 plugin => {
17 safe => 1,
18 rebuild => undef,
19 section => "widget",
23 my $mapnum=0;
25 sub preprocess (@) {
26 my %params=@_;
28 $params{pages}="*" unless defined $params{pages};
30 $mapnum++;
31 my $connected=IkiWiki::yesno($params{connected});
33 # Get all the items to map.
34 my %mapitems = map { $_ => urlto($_, $params{destpage}) }
35 pagespec_match_list($params{page}, $params{pages},
36 # update when a page is added or removed, or its
37 # links change
38 deptype => deptype("presence", "links"));
40 my $dest=$params{page}."/linkmap.png";
42 # Use ikiwiki's function to create the file, this makes sure needed
43 # subdirs are there and does some sanity checking.
44 will_render($params{page}, $dest);
45 writefile($dest, $config{destdir}, "");
47 # Run dot to create the graphic and get the map data.
48 my $pid;
49 my $sigpipe=0;
50 $SIG{PIPE}=sub { $sigpipe=1 };
51 $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
53 # open2 doesn't respect "use open ':utf8'"
54 binmode (IN, ':utf8');
55 binmode (OUT, ':utf8');
57 print OUT "digraph linkmap$mapnum {\n";
58 print OUT "concentrate=true;\n";
59 print OUT "charset=\"utf-8\";\n";
60 print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
61 if defined $params{width} and defined $params{height};
62 my %shown;
63 my $show=sub {
64 my $item=shift;
65 if (! $shown{$item}) {
66 print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
67 $shown{$item}=1;
70 foreach my $item (keys %mapitems) {
71 $show->($item) unless $connected;
72 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
73 next unless length $link and $mapitems{$link};
74 foreach my $endpoint ($item, $link) {
75 $show->($endpoint);
77 print OUT "\"$item\" -> \"$link\";\n";
80 print OUT "}\n";
81 close OUT || error gettext("failed to run dot");
83 local $/=undef;
84 my $ret="<img src=\"".urlto($dest, $params{destpage}).
85 "\" alt=\"".gettext("linkmap").
86 "\" usemap=\"#linkmap$mapnum\" />\n".
87 <IN>;
88 close IN || error gettext("failed to run dot");
90 waitpid $pid, 0;
91 if ($?) {
92 error gettext("failed to run dot");
94 $SIG{PIPE}="DEFAULT";
95 error gettext("failed to run dot") if $sigpipe;
97 return $ret;