2 # graphviz plugin for ikiwiki: render graphviz source as an image.
4 package IkiWiki::Plugin::graphviz;
12 hook(type => "getsetup", id => "graphviz", call => \&getsetup);
13 hook(type => "preprocess", id => "graph", call => \&graph);
25 my %graphviz_programs = (
26 "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
31 sub render_graph (\%) {
32 my %params = %{(shift)};
35 my $src = "$params{type} graph$graphnum {\n";
36 $src .= "charset=\"utf-8\";\n";
37 $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
38 if defined $params{width} and defined $params{height};
42 # Use the sha1 of the graphviz code as part of its filename.
43 eval q{use Digest::SHA};
45 my $base=$params{page}."/graph-".
46 IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($src));
47 my $dest=$base.".png";
48 will_render($params{page}, $dest);
50 # The imagemap data is stored as a separate file.
51 my $imagemap=$base.".imagemap";
52 will_render($params{page}, $imagemap);
55 if (! -e "$config{destdir}/$dest" || ! -e "$config{destdir}/$imagemap") {
56 # Use ikiwiki's function to create the image file, this makes
57 # sure needed subdirs are there and does some sanity checking.
58 writefile($dest, $config{destdir}, "");
62 $SIG{PIPE}=sub { $sigpipe=1 };
63 $pid=open2(*IN, *OUT, "$params{prog} -Tpng -o '$config{destdir}/$dest' -Tcmapx");
65 # open2 doesn't respect "use open ':utf8'"
66 binmode (IN, ':utf8');
67 binmode (OUT, ':utf8');
75 writefile($imagemap, $config{destdir}, $map);
79 error gettext("failed to run graphviz") if ($sigpipe || $?);
83 $map=readfile("$config{destdir}/$imagemap");
86 return "<img src=\"".urlto($dest, $params{destpage}).
87 "\" usemap=\"#graph$graphnum\" />\n".
93 $params{src} = "" unless defined $params{src};
94 $params{type} = "digraph" unless defined $params{type};
95 $params{prog} = "dot" unless defined $params{prog};
96 error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
98 return render_graph(%params);