]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/graphviz.pm
ikiwiki (3.20140916) unstable; urgency=low
[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 => "needsbuild", id => "version", call => \&needsbuild);
14         hook(type => "preprocess", id => "graph", call => \&graph, scan => 1);
15 }
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => undef,
22                         section => "widget",
23                 },
24 }
26 my %graphviz_programs = (
27         "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
28 );
30 sub needsbuild {
31         my $needsbuild=shift;
32         foreach my $page (keys %pagestate) {
33                 if (exists $pagestate{$page}{graph} &&
34                     exists $pagesources{$page} &&
35                     grep { $_ eq $pagesources{$page} } @$needsbuild) {
36                         # remove state, will be re-added if
37                         # the graph is still there during the rebuild
38                         delete $pagestate{$page}{graph};
39                 }
40         }       
41         return $needsbuild;
42 }
44 sub render_graph (\%) {
45         my %params = %{(shift)};
46         
47         my $src = "charset=\"utf-8\";\n";
48         $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
49                 if defined $params{width} and defined $params{height};
50         $src .= $params{src};
51         $src .= "}\n";
52         
53         # Use the sha1 of the graphviz code as part of its filename,
54         # and as a unique identifier for its imagemap.
55         eval q{use Digest::SHA};
56         error($@) if $@;
57         my $sha=IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($params{type}.$src));
58         $src = "$params{type} graph$sha {\n".$src;
60         my $dest=$params{page}."/graph-".$sha.".png";
61         will_render($params{page}, $dest);
63         my $map=$pagestate{$params{destpage}}{graph}{$sha};
64         if (! -e "$config{destdir}/$dest" || ! defined $map) {
65                 # Use ikiwiki's function to create the image file, this makes
66                 # sure needed subdirs are there and does some sanity checking.
67                 writefile($dest, $config{destdir}, "");
68                 
69                 my $pid;
70                 my $sigpipe=0;
71                 $SIG{PIPE}=sub { $sigpipe=1 };
72                 $pid=open2(*IN, *OUT, "$params{prog} -Tpng -o '$config{destdir}/$dest' -Tcmapx");
74                 # open2 doesn't respect "use open ':utf8'"
75                 binmode (IN, ':utf8');
76                 binmode (OUT, ':utf8');
78                 print OUT $src;
79                 close OUT;
81                 local $/ = undef;
82                 $map=$pagestate{$params{destpage}}{graph}{$sha}=<IN>;
83                 close IN;
85                 waitpid $pid, 0;
86                 $SIG{PIPE}="DEFAULT";
87                 error gettext("failed to run graphviz") if ($sigpipe || $?);
88         }
90         return "<img src=\"".urlto($dest, $params{destpage}).
91                 "\" usemap=\"#graph$sha\" />\n".
92                 $map;
93 }
95 sub graph (@) {
96         my %params=@_;
98         # Support wikilinks in the graph source.
99         my $src=$params{src};
100         $src="" unless defined $src;
101         $src=IkiWiki::linkify($params{page}, $params{destpage}, $params{src});
102         return unless defined wantarray; # scan mode short-circuit
103         if ($src ne $params{src}) {
104                 # linkify makes html links, but graphviz wants plain
105                 # urls. This is, frankly a hack: Process source as html,
106                 # throw out everything inside tags that is not a href.
107                 my $s;
108                 my $nested=0;
109                 use HTML::Parser;
110                 error $@ if $@;
111                 my $p=HTML::Parser->new(api_version => 3);
112                 $p->handler(start => sub {
113                         my %attrs=%{shift()};
114                         if (exists $attrs{href}) {
115                                 if ($s=~/href\s*=\s*"$/) {
116                                         $s.=$attrs{href};
117                                 }
118                                 elsif ($s=~/href\s*=\s*$/) {
119                                         $s.="\"$attrs{href}\"";
120                                 }
121                                 else {
122                                         $s.="href=\"$attrs{href}\"";
123                                 }
124                         }
125                         $nested++;
126                 }, "attr");
127                 $p->handler(end => sub {
128                         $nested--;
129                 });
130                 $p->handler(default => sub {
131                         $s.=join("", @_) unless $nested;
132                 }, "text");
133                 $p->parse($src);
134                 $p->eof;
135                 $s=~s/\[ href= \]//g; # handle self-links
136                 $params{src}=$s;
137         }
138         else {
139                 $params{src}=$src;
140         }
142         $params{type} = "digraph" unless defined $params{type};
143         $params{prog} = "dot" unless defined $params{prog};
144         error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
146         return render_graph(%params);