]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/graphviz.pm
comments: add regression test for sorting by date
[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 3.00;
9 use IPC::Open2;
11 sub import {
12         hook(type => "getsetup", id => "graphviz", call => \&getsetup);
13         hook(type => "preprocess", id => "graph", call => \&graph);
14 }
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                         section => "widget",
22                 },
23 }
25 my %graphviz_programs = (
26         "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
27 );
29 sub render_graph (\%) {
30         my %params = %{(shift)};
32         my $src = "$params{type} g {\n";
33         $src .= "charset=\"utf-8\";\n";
34         $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
35                 if defined $params{width} and defined $params{height};
36         $src .= $params{src};
37         $src .= "}\n";
39         # Use the sha1 of the graphviz code as part of its filename.
40         eval q{use Digest::SHA};
41         error($@) if $@;
42         my $dest=$params{page}."/graph-".
43                 IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($src)).
44                 ".png";
45         will_render($params{page}, $dest);
47         if (! -e "$config{destdir}/$dest") {
48                 my $pid;
49                 my $sigpipe=0;
50                 $SIG{PIPE}=sub { $sigpipe=1 };
51                 $pid=open2(*IN, *OUT, "$params{prog} -Tpng");
53                 # open2 doesn't respect "use open ':utf8'"
54                 binmode (OUT, ':utf8');
56                 print OUT $src;
57                 close OUT;
59                 my $png;
60                 {
61                         local $/ = undef;
62                         $png = <IN>;
63                 }
64                 close IN;
66                 waitpid $pid, 0;
67                 $SIG{PIPE}="DEFAULT";
68                 error gettext("failed to run graphviz") if $sigpipe;
70                 if (! $params{preview}) {
71                         writefile($dest, $config{destdir}, $png, 1);
72                 }
73                 else {
74                         # in preview mode, embed the image in a data uri
75                         # to avoid temp file clutter
76                         eval q{use MIME::Base64};
77                         error($@) if $@;
78                         return "<img src=\"data:image/png;base64,".
79                                 encode_base64($png)."\" />";
80                 }
81         }
83         return "<img src=\"".urlto($dest, $params{destpage})."\" />\n";
84 }
86 sub graph (@) {
87         my %params=@_;
88         $params{src} = "" unless defined $params{src};
89         $params{type} = "digraph" unless defined $params{type};
90         $params{prog} = "dot" unless defined $params{prog};
91         error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
93         return render_graph(%params);
94 }
96 1