]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/graphviz.pm
non comprend
[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::SHA1};
41         error($@) if $@;
42         my $dest=$params{page}."/graph-".
43                 IkiWiki::possibly_foolish_untaint(Digest::SHA1::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                         # can't write the file, so embed it in a data uri
75                         eval q{use MIME::Base64};
76                         error($@) if $@;
77                         return "<img src=\"data:image/png;base64,".
78                                 encode_base64($png)."\" />";
79                 }
80         }
82         if ($params{preview}) {
83                 return "<img src=\"".urlto($dest, "")."\" />\n";
84         }
85         else {
86                 return "<img src=\"".urlto($dest, $params{destpage})."\" />\n";
87         }
88 }
90 sub graph (@) {
91         my %params=@_;
92         $params{src} = "" unless defined $params{src};
93         $params{type} = "digraph" unless defined $params{type};
94         $params{prog} = "dot" unless defined $params{prog};
95         error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
97         return render_graph(%params);
98 }