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);
16 sub getsetup () { #{{{
24 my %graphviz_programs = (
25 "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
28 sub render_graph (\%) { #{{{
29 my %params = %{(shift)};
31 my $src = "$params{type} g {\n";
32 $src .= "charset=\"utf-8\";\n";
33 $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
34 if defined $params{width} and defined $params{height};
38 # Use the sha1 of the graphviz code as part of its filename.
39 eval q{use Digest::SHA1};
41 my $dest=$params{page}."/graph-".
42 IkiWiki::possibly_foolish_untaint(Digest::SHA1::sha1_hex($src)).
44 will_render($params{page}, $dest);
46 if (! -e "$config{destdir}/$dest") {
49 $SIG{PIPE}=sub { $sigpipe=1 };
50 $pid=open2(*IN, *OUT, "$params{prog} -Tpng");
52 # open2 doesn't respect "use open ':utf8'"
53 binmode (OUT, ':utf8');
67 error gettext("failed to run graphviz") if $sigpipe;
69 if (! $params{preview}) {
70 writefile($dest, $config{destdir}, $png, 1);
73 # can't write the file, so embed it in a data uri
74 eval q{use MIME::Base64};
76 return "<img src=\"data:image/png;base64,".
77 encode_base64($png)."\" />";
81 if ($params{preview}) {
82 return "<img src=\"".urlto($dest, "")."\" />\n";
85 return "<img src=\"".urlto($dest, $params{destpage})."\" />\n";
91 $params{src} = "" unless defined $params{src};
92 $params{type} = "digraph" unless defined $params{type};
93 $params{prog} = "dot" unless defined $params{prog};
94 error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
96 return render_graph(%params);