2007-04-09 11:09:02 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# graphviz plugin for ikiwiki: render graphviz source as an image.
|
|
|
|
# Josh Triplett
|
|
|
|
package IkiWiki::Plugin::graphviz;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2007-04-09 11:09:02 +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 => "graphviz", call => \&getsetup);
|
2011-11-30 20:03:24 +01:00
|
|
|
hook(type => "needsbuild", id => "version", call => \&needsbuild);
|
2011-11-30 20:42:42 +01:00
|
|
|
hook(type => "preprocess", id => "graph", call => \&graph, scan => 1);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-04-09 11:09:02 +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,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2007-04-09 11:09:02 +02:00
|
|
|
my %graphviz_programs = (
|
|
|
|
"dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
|
|
|
|
);
|
|
|
|
|
2011-11-30 20:03:24 +01:00
|
|
|
sub needsbuild {
|
|
|
|
my $needsbuild=shift;
|
|
|
|
foreach my $page (keys %pagestate) {
|
|
|
|
if (exists $pagestate{$page}{graph} &&
|
|
|
|
exists $pagesources{$page} &&
|
|
|
|
grep { $_ eq $pagesources{$page} } @$needsbuild) {
|
|
|
|
# remove state, will be re-added if
|
|
|
|
# the graph is still there during the rebuild
|
|
|
|
delete $pagestate{$page}{graph};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $needsbuild;
|
|
|
|
}
|
2011-11-30 19:40:27 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub render_graph (\%) {
|
2007-04-09 11:09:02 +02:00
|
|
|
my %params = %{(shift)};
|
2011-11-30 20:03:24 +01:00
|
|
|
|
|
|
|
my $src = "charset=\"utf-8\";\n";
|
2007-04-09 11:09:02 +02:00
|
|
|
$src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
|
|
|
|
if defined $params{width} and defined $params{height};
|
|
|
|
$src .= $params{src};
|
|
|
|
$src .= "}\n";
|
2011-11-30 20:03:24 +01:00
|
|
|
|
|
|
|
# Use the sha1 of the graphviz code as part of its filename,
|
|
|
|
# and as a unique identifier for its imagemap.
|
2010-07-31 21:54:25 +02:00
|
|
|
eval q{use Digest::SHA};
|
2007-04-09 11:09:02 +02:00
|
|
|
error($@) if $@;
|
2011-11-30 20:03:24 +01:00
|
|
|
my $sha=IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($params{type}.$src));
|
|
|
|
$src = "$params{type} graph$sha {\n".$src;
|
|
|
|
|
|
|
|
my $dest=$params{page}."/graph-".$sha.".png";
|
2008-03-24 01:01:26 +01:00
|
|
|
will_render($params{page}, $dest);
|
2007-04-09 11:09:02 +02:00
|
|
|
|
2011-11-30 20:03:24 +01:00
|
|
|
my $map=$pagestate{$params{destpage}}{graph}{$sha};
|
|
|
|
if (! -e "$config{destdir}/$dest" || ! defined $map) {
|
2011-11-30 19:40:27 +01:00
|
|
|
# Use ikiwiki's function to create the image file, this makes
|
|
|
|
# sure needed subdirs are there and does some sanity checking.
|
|
|
|
writefile($dest, $config{destdir}, "");
|
|
|
|
|
2007-04-09 11:09:02 +02:00
|
|
|
my $pid;
|
2009-02-11 18:29:39 +01:00
|
|
|
my $sigpipe=0;
|
2007-04-09 11:09:02 +02:00
|
|
|
$SIG{PIPE}=sub { $sigpipe=1 };
|
2011-11-30 19:40:27 +01:00
|
|
|
$pid=open2(*IN, *OUT, "$params{prog} -Tpng -o '$config{destdir}/$dest' -Tcmapx");
|
2007-04-09 11:09:02 +02:00
|
|
|
|
|
|
|
# open2 doesn't respect "use open ':utf8'"
|
2011-11-30 19:40:27 +01:00
|
|
|
binmode (IN, ':utf8');
|
2007-04-09 11:09:02 +02:00
|
|
|
binmode (OUT, ':utf8');
|
|
|
|
|
|
|
|
print OUT $src;
|
|
|
|
close OUT;
|
|
|
|
|
2011-11-30 19:40:27 +01:00
|
|
|
local $/ = undef;
|
2011-11-30 20:03:24 +01:00
|
|
|
$map=$pagestate{$params{destpage}}{graph}{$sha}=<IN>;
|
2007-04-09 11:09:02 +02:00
|
|
|
close IN;
|
|
|
|
|
|
|
|
waitpid $pid, 0;
|
|
|
|
$SIG{PIPE}="DEFAULT";
|
2011-11-30 19:40:27 +01:00
|
|
|
error gettext("failed to run graphviz") if ($sigpipe || $?);
|
2007-04-09 11:09:02 +02:00
|
|
|
}
|
|
|
|
|
2011-11-30 19:40:27 +01:00
|
|
|
return "<img src=\"".urlto($dest, $params{destpage}).
|
2011-11-30 20:03:24 +01:00
|
|
|
"\" usemap=\"#graph$sha\" />\n".
|
2011-11-30 19:40:27 +01:00
|
|
|
$map;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-04-09 11:09:02 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub graph (@) {
|
2007-04-09 11:09:02 +02:00
|
|
|
my %params=@_;
|
2011-11-30 20:42:42 +01:00
|
|
|
|
2018-10-22 06:24:59 +02:00
|
|
|
if (exists $params{file}) {
|
|
|
|
if (! exists $pagesources{$params{file}}) {
|
|
|
|
error gettext("cannot find file");
|
|
|
|
}
|
|
|
|
$params{src} = readfile(srcfile($params{file}));
|
|
|
|
add_depends($params{page}, $params{file});
|
|
|
|
}
|
|
|
|
|
2011-11-30 20:42:42 +01:00
|
|
|
# Support wikilinks in the graph source.
|
|
|
|
my $src=$params{src};
|
|
|
|
$src="" unless defined $src;
|
|
|
|
$src=IkiWiki::linkify($params{page}, $params{destpage}, $params{src});
|
|
|
|
return unless defined wantarray; # scan mode short-circuit
|
|
|
|
if ($src ne $params{src}) {
|
|
|
|
# linkify makes html links, but graphviz wants plain
|
|
|
|
# urls. This is, frankly a hack: Process source as html,
|
|
|
|
# throw out everything inside tags that is not a href.
|
|
|
|
my $s;
|
|
|
|
my $nested=0;
|
|
|
|
use HTML::Parser;
|
|
|
|
error $@ if $@;
|
|
|
|
my $p=HTML::Parser->new(api_version => 3);
|
|
|
|
$p->handler(start => sub {
|
|
|
|
my %attrs=%{shift()};
|
|
|
|
if (exists $attrs{href}) {
|
2011-11-30 21:25:17 +01:00
|
|
|
if ($s=~/href\s*=\s*"$/) {
|
|
|
|
$s.=$attrs{href};
|
|
|
|
}
|
|
|
|
elsif ($s=~/href\s*=\s*$/) {
|
|
|
|
$s.="\"$attrs{href}\"";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$s.="href=\"$attrs{href}\"";
|
|
|
|
}
|
2011-11-30 20:42:42 +01:00
|
|
|
}
|
|
|
|
$nested++;
|
|
|
|
}, "attr");
|
|
|
|
$p->handler(end => sub {
|
|
|
|
$nested--;
|
|
|
|
});
|
|
|
|
$p->handler(default => sub {
|
|
|
|
$s.=join("", @_) unless $nested;
|
|
|
|
}, "text");
|
|
|
|
$p->parse($src);
|
|
|
|
$p->eof;
|
2012-04-18 21:36:27 +02:00
|
|
|
$s=~s/\[ href= \]//g; # handle self-links
|
2011-11-30 20:42:42 +01:00
|
|
|
$params{src}=$s;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$params{src}=$src;
|
|
|
|
}
|
|
|
|
|
2007-04-09 11:09:02 +02:00
|
|
|
$params{type} = "digraph" unless defined $params{type};
|
|
|
|
$params{prog} = "dot" unless defined $params{prog};
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
|
2007-04-09 11:09:02 +02:00
|
|
|
|
|
|
|
return render_graph(%params);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-04-09 11:09:02 +02:00
|
|
|
|
|
|
|
1
|