2006-09-07 07:31:01 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::linkmap;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-09-07 07:31:01 +02:00
|
|
|
use IPC::Open2;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "linkmap", call => \&getsetup);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "preprocess", id => "linkmap", call => \&preprocess);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-09-07 07:31:01 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2006-09-07 07:31:01 +02:00
|
|
|
my $mapnum=0;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess (@) {
|
2006-09-07 07:31:01 +02:00
|
|
|
my %params=@_;
|
2006-09-07 08:58:27 +02:00
|
|
|
|
2006-09-07 07:31:01 +02:00
|
|
|
$params{pages}="*" unless defined $params{pages};
|
|
|
|
|
|
|
|
$mapnum++;
|
2010-01-01 09:37:59 +01:00
|
|
|
my $connected=IkiWiki::yesno($params{connected});
|
2006-09-07 07:31:01 +02:00
|
|
|
|
|
|
|
# Get all the items to map.
|
2009-10-09 18:35:41 +02:00
|
|
|
my %mapitems = map { $_ => urlto($_, $params{destpage}) }
|
|
|
|
pagespec_match_list($params{page}, $params{pages},
|
|
|
|
# update when a page is added or removed, or its
|
|
|
|
# links change
|
|
|
|
deptype => deptype("presence", "links"));
|
2006-09-07 07:31:01 +02:00
|
|
|
|
2008-03-24 01:01:26 +01:00
|
|
|
my $dest=$params{page}."/linkmap.png";
|
2007-04-01 23:17:31 +02:00
|
|
|
|
2006-09-07 07:31:01 +02:00
|
|
|
# Use ikiwiki's function to create the file, this makes sure needed
|
|
|
|
# subdirs are there and does some sanity checking.
|
2008-03-24 01:01:26 +01:00
|
|
|
will_render($params{page}, $dest);
|
2007-04-01 23:17:31 +02:00
|
|
|
writefile($dest, $config{destdir}, "");
|
2006-09-07 07:31:01 +02:00
|
|
|
|
|
|
|
# Run dot to create the graphic and get the map data.
|
|
|
|
my $pid;
|
2007-04-07 02:30:09 +02:00
|
|
|
my $sigpipe=0;
|
2006-09-25 23:38:25 +02:00
|
|
|
$SIG{PIPE}=sub { $sigpipe=1 };
|
2007-04-01 23:17:31 +02:00
|
|
|
$pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
|
2006-09-25 23:38:25 +02:00
|
|
|
|
2006-09-07 07:31:01 +02:00
|
|
|
# open2 doesn't respect "use open ':utf8'"
|
|
|
|
binmode (IN, ':utf8');
|
|
|
|
binmode (OUT, ':utf8');
|
|
|
|
|
|
|
|
print OUT "digraph linkmap$mapnum {\n";
|
|
|
|
print OUT "concentrate=true;\n";
|
2006-09-07 07:54:01 +02:00
|
|
|
print OUT "charset=\"utf-8\";\n";
|
2006-09-07 08:58:27 +02:00
|
|
|
print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
|
|
|
|
if defined $params{width} and defined $params{height};
|
2010-01-01 09:37:59 +01:00
|
|
|
my %shown;
|
|
|
|
my $show=sub {
|
|
|
|
my $item=shift;
|
|
|
|
if (! $shown{$item}) {
|
|
|
|
print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
|
|
|
|
$shown{$item}=1;
|
|
|
|
}
|
|
|
|
};
|
2006-09-07 07:31:01 +02:00
|
|
|
foreach my $item (keys %mapitems) {
|
2010-01-01 09:37:59 +01:00
|
|
|
$show->($item) unless $connected;
|
2006-09-10 00:50:27 +02:00
|
|
|
foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
|
2010-01-01 09:37:59 +01:00
|
|
|
next unless length $link and $mapitems{$link};
|
|
|
|
foreach my $endpoint ($item, $link) {
|
|
|
|
$show->($endpoint);
|
|
|
|
}
|
|
|
|
print OUT "\"$item\" -> \"$link\";\n";
|
2006-09-07 07:31:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
print OUT "}\n";
|
2010-01-01 09:37:59 +01:00
|
|
|
close OUT || error gettext("failed to run dot");
|
2006-09-07 07:31:01 +02:00
|
|
|
|
|
|
|
local $/=undef;
|
2010-01-10 05:10:26 +01:00
|
|
|
my $ret="<img src=\"".urlto($dest, $params{destpage}).
|
|
|
|
"\" alt=\"".gettext("linkmap").
|
|
|
|
"\" usemap=\"#linkmap$mapnum\" />\n".
|
|
|
|
<IN>;
|
2010-01-01 09:37:59 +01:00
|
|
|
close IN || error gettext("failed to run dot");
|
2006-09-25 23:38:25 +02:00
|
|
|
|
2006-09-07 07:31:01 +02:00
|
|
|
waitpid $pid, 0;
|
2010-01-01 09:37:59 +01:00
|
|
|
if ($?) {
|
|
|
|
error gettext("failed to run dot");
|
|
|
|
}
|
2006-09-25 23:38:25 +02:00
|
|
|
$SIG{PIPE}="DEFAULT";
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("failed to run dot") if $sigpipe;
|
2006-09-25 23:38:25 +02:00
|
|
|
|
2006-09-07 07:31:01 +02:00
|
|
|
return $ret;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-09-07 07:31:01 +02:00
|
|
|
|
|
|
|
1
|