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);
|
2008-08-14 20:44:30 +02:00
|
|
|
hook(type => "preprocess", id => "graph", call => \&graph);
|
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
|
|
|
|
);
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub render_graph (\%) {
|
2007-04-09 11:09:02 +02:00
|
|
|
my %params = %{(shift)};
|
|
|
|
|
|
|
|
my $src = "$params{type} g {\n";
|
|
|
|
$src .= "charset=\"utf-8\";\n";
|
|
|
|
$src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
|
|
|
|
if defined $params{width} and defined $params{height};
|
|
|
|
$src .= $params{src};
|
|
|
|
$src .= "}\n";
|
|
|
|
|
|
|
|
# Use the sha1 of the graphviz code as part of its filename.
|
|
|
|
eval q{use Digest::SHA1};
|
|
|
|
error($@) if $@;
|
2008-03-24 01:01:26 +01:00
|
|
|
my $dest=$params{page}."/graph-".
|
2007-04-09 11:09:02 +02:00
|
|
|
IkiWiki::possibly_foolish_untaint(Digest::SHA1::sha1_hex($src)).
|
|
|
|
".png";
|
2008-03-24 01:01:26 +01:00
|
|
|
will_render($params{page}, $dest);
|
2007-04-09 11:09:02 +02:00
|
|
|
|
|
|
|
if (! -e "$config{destdir}/$dest") {
|
|
|
|
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 };
|
|
|
|
$pid=open2(*IN, *OUT, "$params{prog} -Tpng");
|
|
|
|
|
|
|
|
# open2 doesn't respect "use open ':utf8'"
|
|
|
|
binmode (OUT, ':utf8');
|
|
|
|
|
|
|
|
print OUT $src;
|
|
|
|
close OUT;
|
|
|
|
|
|
|
|
my $png;
|
|
|
|
{
|
|
|
|
local $/ = undef;
|
|
|
|
$png = <IN>;
|
|
|
|
}
|
|
|
|
close IN;
|
|
|
|
|
|
|
|
waitpid $pid, 0;
|
|
|
|
$SIG{PIPE}="DEFAULT";
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("failed to run graphviz") if $sigpipe;
|
2007-04-09 11:09:02 +02:00
|
|
|
|
|
|
|
if (! $params{preview}) {
|
|
|
|
writefile($dest, $config{destdir}, $png, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# can't write the file, so embed it in a data uri
|
|
|
|
eval q{use MIME::Base64};
|
|
|
|
error($@) if $@;
|
|
|
|
return "<img src=\"data:image/png;base64,".
|
|
|
|
encode_base64($png)."\" />";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-04 22:54:58 +02:00
|
|
|
return "<img src=\"".urlto($dest, $params{destpage})."\" />\n";
|
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=@_;
|
|
|
|
$params{src} = "" unless defined $params{src};
|
|
|
|
$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
|