* Make all templates have a footer div to ease themeing. Required template
[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 (IN, ':utf8');
45                 binmode (OUT, ':utf8');
46
47                 print OUT $src;
48                 close OUT;
49
50                 my $png;
51                 {
52                         local $/ = undef;
53                         $png = <IN>;
54                 }
55                 close IN;
56
57                 waitpid $pid, 0;
58                 $SIG{PIPE}="DEFAULT";
59                 return  "[[graph ".gettext("failed to run graphviz")."]]" if ($sigpipe);
60
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         }
72
73         return "<img src=\"".urlto($dest, $params{page})."\" />\n";
74 } #}}}
75
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}};
82
83         return render_graph(%params);
84 } # }}}
85
86 1