]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/graphviz.pm
img: Support a title attribute, will be passed through to html. Closes: #478718
[ikiwiki.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;
5
6 use warnings;
7 use strict;
8 use IkiWiki 2.00;
9 use IPC::Open2;
10
11 sub import { #{{{
12         hook(type => "preprocess", id => "graph", call => \&graph);
13 } # }}}
14
15 my %graphviz_programs = (
16         "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
17 );
18
19 sub render_graph (\%) { #{{{
20         my %params = %{(shift)};
21
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";
28
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);
36
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");
42
43                 # open2 doesn't respect "use open ':utf8'"
44                 binmode (OUT, ':utf8');
45
46                 print OUT $src;
47                 close OUT;
48
49                 my $png;
50                 {
51                         local $/ = undef;
52                         $png = <IN>;
53                 }
54                 close IN;
55
56                 waitpid $pid, 0;
57                 $SIG{PIPE}="DEFAULT";
58                 return  "[[graph ".gettext("failed to run graphviz")."]]" if ($sigpipe);
59
60                 if (! $params{preview}) {
61                         writefile($dest, $config{destdir}, $png, 1);
62                 }
63                 else {
64                         # can't write the file, so embed it in a data uri
65                         eval q{use MIME::Base64};
66                         error($@) if $@;
67                         return "<img src=\"data:image/png;base64,".
68                                 encode_base64($png)."\" />";
69                 }
70         }
71
72         if ($params{preview}) {
73                 return "<img src=\"".urlto($dest, "")."\" />\n";
74         }
75         else {
76                 return "<img src=\"".urlto($dest, $params{destpage})."\" />\n";
77         }
78 } #}}}
79
80 sub graph (@) { #{{{
81         my %params=@_;
82         $params{src} = "" unless defined $params{src};
83         $params{type} = "digraph" unless defined $params{type};
84         $params{prog} = "dot" unless defined $params{prog};
85         return "[[graph ".gettext("prog not a valid graphviz program")."]]" unless $graphviz_programs{$params{prog}};
86
87         return render_graph(%params);
88 } # }}}
89
90 1