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 if (exists $params{file}) {
99 if (! exists $pagesources{$params{file}}) {
100 error gettext("cannot find file");
102 $params{src} = readfile(srcfile($params{file}));
103 add_depends($params{page}, $params{file});
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.
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*"$/) {
126 elsif ($s=~/href\s*=\s*$/) {
127 $s.="\"$attrs{href}\"";
130 $s.="href=\"$attrs{href}\"";
135 $p->handler(end => sub {
138 $p->handler(default => sub {
139 $s.=join("", @_) unless $nested;
143 $s=~s/\[ href= \]//g; # handle self-links
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);