2 # graphviz plugin for ikiwiki: render graphviz source as an image.
4 package IkiWiki::Plugin::graphviz;
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);
26 my %graphviz_programs = (
27 "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
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};
44 sub render_graph (\%) {
45 my %params = %{(shift)};
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};
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};
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}, "");
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');
82 $map=$pagestate{$params{destpage}}{graph}{$sha}=<IN>;
87 error gettext("failed to run graphviz") if ($sigpipe || $?);
90 return "<img src=\"".urlto($dest, $params{destpage}).
91 "\" usemap=\"#graph$sha\" />\n".
98 # Support wikilinks in the graph source.
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.
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*"$/) {
118 elsif ($s=~/href\s*=\s*$/) {
119 $s.="\"$attrs{href}\"";
122 $s.="href=\"$attrs{href}\"";
127 $p->handler(end => sub {
130 $p->handler(default => sub {
131 $s.=join("", @_) unless $nested;
135 $s=~s/\[ href= \]//g; # handle self-links
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);