]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/graphviz.pm
web commit by http://xtermin.us/: sitemaps plugin
[git.ikiwiki.info.git] / IkiWiki / Plugin / graphviz.pm
1 #!/usr/bin/perl
2 # graphviz plugin for ikiwiki: render graphviz source as an image.
3 # Josh Triplett
4 package IkiWiki::Plugin::graphviz;
6 use warnings;
7 use strict;
8 use IkiWiki 2.00;
9 use IPC::Open2;
11 sub import { #{{{
12         hook(type => "preprocess", id => "graph", call => \&graph);
13 } # }}}
15 my %graphviz_programs = (
16         "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
17 );
19 sub render_graph (\%) { #{{{
20         my %params = %{(shift)};
22         my $src = "$params{type} g {\n";
23         $src .= "charset=\"utf-8\";\n";
24         $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
25                 if defined $params{width} and defined $params{height};
26         $src .= $params{src};
27         $src .= "}\n";
29         # Use the sha1 of the graphviz code as part of its filename.
30         eval q{use Digest::SHA1};
31         error($@) if $@;
32         my $dest=$params{page}."/graph-".
33                 IkiWiki::possibly_foolish_untaint(Digest::SHA1::sha1_hex($src)).
34                 ".png";
35         will_render($params{page}, $dest);
37         if (! -e "$config{destdir}/$dest") {
38                 my $pid;
39                 my $sigpipe=0;;
40                 $SIG{PIPE}=sub { $sigpipe=1 };
41                 $pid=open2(*IN, *OUT, "$params{prog} -Tpng");
43                 # open2 doesn't respect "use open ':utf8'"
44                 binmode (IN, ':utf8');
45                 binmode (OUT, ':utf8');
47                 print OUT $src;
48                 close OUT;
50                 my $png;
51                 {
52                         local $/ = undef;
53                         $png = <IN>;
54                 }
55                 close IN;
57                 waitpid $pid, 0;
58                 $SIG{PIPE}="DEFAULT";
59                 return  "[[graph ".gettext("failed to run graphviz")."]]" if ($sigpipe);
61                 if (! $params{preview}) {
62                         writefile($dest, $config{destdir}, $png, 1);
63                 }
64                 else {
65                         # can't write the file, so embed it in a data uri
66                         eval q{use MIME::Base64};
67                         error($@) if $@;
68                         return "<img src=\"data:image/png;base64,".
69                                 encode_base64($png)."\" />";
70                 }
71         }
73         return "<img src=\"".urlto($dest, $params{page})."\" />\n";
74 } #}}}
76 sub graph (@) { #{{{
77         my %params=@_;
78         $params{src} = "" unless defined $params{src};
79         $params{type} = "digraph" unless defined $params{type};
80         $params{prog} = "dot" unless defined $params{prog};
81         return "[[graph ".gettext("prog not a valid graphviz program")."]]" unless $graphviz_programs{$params{prog}};
83         return render_graph(%params);
84 } # }}}
86 1