f01fdca8573135026210468ac2b84ae63806f7b5
[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 3.00;
9 use IPC::Open2;
10
11 sub import {
12         hook(type => "getsetup", id => "graphviz", call => \&getsetup);
13         hook(type => "preprocess", id => "graph", call => \&graph);
14 }
15
16 sub getsetup () {
17         return
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                         section => "widget",
22                 },
23 }
24
25 my %graphviz_programs = (
26         "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
27 );
28
29 my $graphnum=0;
30
31 sub render_graph (\%) {
32         my %params = %{(shift)};
33
34         $graphnum++;
35         my $src = "$params{type} graph$graphnum {\n";
36         $src .= "charset=\"utf-8\";\n";
37         $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
38                 if defined $params{width} and defined $params{height};
39         $src .= $params{src};
40         $src .= "}\n";
41
42         # Use the sha1 of the graphviz code as part of its filename.
43         eval q{use Digest::SHA};
44         error($@) if $@;
45         my $base=$params{page}."/graph-".
46                 IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($src));
47         my $dest=$base.".png";
48         will_render($params{page}, $dest);
49
50         # The imagemap data is stored as a separate file.
51         my $imagemap=$base.".imagemap";
52         will_render($params{page}, $imagemap);
53         
54         my $map;
55         if (! -e "$config{destdir}/$dest" || ! -e "$config{destdir}/$imagemap") {
56                 # Use ikiwiki's function to create the image file, this makes
57                 # sure needed subdirs are there and does some sanity checking.
58                 writefile($dest, $config{destdir}, "");
59                 
60                 my $pid;
61                 my $sigpipe=0;
62                 $SIG{PIPE}=sub { $sigpipe=1 };
63                 $pid=open2(*IN, *OUT, "$params{prog} -Tpng -o '$config{destdir}/$dest' -Tcmapx");
64
65                 # open2 doesn't respect "use open ':utf8'"
66                 binmode (IN, ':utf8');
67                 binmode (OUT, ':utf8');
68
69                 print OUT $src;
70                 close OUT;
71
72                 local $/ = undef;
73                 $map=<IN>;
74                 close IN;
75                 writefile($imagemap, $config{destdir}, $map);
76
77                 waitpid $pid, 0;
78                 $SIG{PIPE}="DEFAULT";
79                 error gettext("failed to run graphviz") if ($sigpipe || $?);
80
81         }
82         else {
83                 $map=readfile("$config{destdir}/$imagemap");
84         }
85
86         return "<img src=\"".urlto($dest, $params{destpage}).
87                 "\" usemap=\"#graph$graphnum\" />\n".
88                 $map;
89 }
90
91 sub graph (@) {
92         my %params=@_;
93         $params{src} = "" unless defined $params{src};
94         $params{type} = "digraph" unless defined $params{type};
95         $params{prog} = "dot" unless defined $params{prog};
96         error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
97
98         return render_graph(%params);
99 }
100
101 1