]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/graphviz.pm
does not seem possible
[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         if (exists $params{file}) {
99                 if (! exists $pagesources{$params{file}}) {
100                         error gettext("cannot find file");
101                 }
102                 $params{src} = readfile(srcfile($params{file}));
103                 add_depends($params{page}, $params{file});
104         }
106         # Support wikilinks in the graph source.
107         my $src=$params{src};
108         $src="" unless defined $src;
109         $src=IkiWiki::linkify($params{page}, $params{destpage}, $params{src});
110         return unless defined wantarray; # scan mode short-circuit
111         if ($src ne $params{src}) {
112                 # linkify makes html links, but graphviz wants plain
113                 # urls. This is, frankly a hack: Process source as html,
114                 # throw out everything inside tags that is not a href.
115                 my $s;
116                 my $nested=0;
117                 use HTML::Parser;
118                 error $@ if $@;
119                 my $p=HTML::Parser->new(api_version => 3);
120                 $p->handler(start => sub {
121                         my %attrs=%{shift()};
122                         if (exists $attrs{href}) {
123                                 if ($s=~/href\s*=\s*"$/) {
124                                         $s.=$attrs{href};
125                                 }
126                                 elsif ($s=~/href\s*=\s*$/) {
127                                         $s.="\"$attrs{href}\"";
128                                 }
129                                 else {
130                                         $s.="href=\"$attrs{href}\"";
131                                 }
132                         }
133                         $nested++;
134                 }, "attr");
135                 $p->handler(end => sub {
136                         $nested--;
137                 });
138                 $p->handler(default => sub {
139                         $s.=join("", @_) unless $nested;
140                 }, "text");
141                 $p->parse($src);
142                 $p->eof;
143                 $s=~s/\[ href= \]//g; # handle self-links
144                 $params{src}=$s;
145         }
146         else {
147                 $params{src}=$src;
148         }
150         $params{type} = "digraph" unless defined $params{type};
151         $params{prog} = "dot" unless defined $params{prog};
152         error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
154         return render_graph(%params);