graphviz: Support urls embedded in the graph, by having graphviz generate an imagemap.

Also, I let preview mode write real files, rather than using data: uri.
Which is ok these days, since ikiwiki tracks files created during
previewing, and cleans them up later.
master
Joey Hess 2011-11-30 14:40:27 -04:00
parent 9936c3ec32
commit 44863ea234
3 changed files with 33 additions and 30 deletions

View File

@ -26,10 +26,13 @@ my %graphviz_programs = (
"dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1 "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
); );
my $graphnum=0;
sub render_graph (\%) { sub render_graph (\%) {
my %params = %{(shift)}; my %params = %{(shift)};
my $src = "$params{type} g {\n"; $graphnum++;
my $src = "$params{type} graph$graphnum {\n";
$src .= "charset=\"utf-8\";\n"; $src .= "charset=\"utf-8\";\n";
$src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n" $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
if defined $params{width} and defined $params{height}; if defined $params{width} and defined $params{height};
@ -39,48 +42,50 @@ sub render_graph (\%) {
# Use the sha1 of the graphviz code as part of its filename. # Use the sha1 of the graphviz code as part of its filename.
eval q{use Digest::SHA}; eval q{use Digest::SHA};
error($@) if $@; error($@) if $@;
my $dest=$params{page}."/graph-". my $base=$params{page}."/graph-".
IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($src)). IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($src));
".png"; my $dest=$base.".png";
will_render($params{page}, $dest); will_render($params{page}, $dest);
if (! -e "$config{destdir}/$dest") { # The imagemap data is stored as a separate file.
my $imagemap=$base.".imagemap";
will_render($params{page}, $imagemap);
my $map;
if (! -e "$config{destdir}/$dest" || ! -e "$config{destdir}/$imagemap") {
# Use ikiwiki's function to create the image file, this makes
# sure needed subdirs are there and does some sanity checking.
writefile($dest, $config{destdir}, "");
my $pid; my $pid;
my $sigpipe=0; my $sigpipe=0;
$SIG{PIPE}=sub { $sigpipe=1 }; $SIG{PIPE}=sub { $sigpipe=1 };
$pid=open2(*IN, *OUT, "$params{prog} -Tpng"); $pid=open2(*IN, *OUT, "$params{prog} -Tpng -o '$config{destdir}/$dest' -Tcmapx");
# open2 doesn't respect "use open ':utf8'" # open2 doesn't respect "use open ':utf8'"
binmode (IN, ':utf8');
binmode (OUT, ':utf8'); binmode (OUT, ':utf8');
print OUT $src; print OUT $src;
close OUT; close OUT;
my $png; local $/ = undef;
{ $map=<IN>;
local $/ = undef;
$png = <IN>;
}
close IN; close IN;
writefile($imagemap, $config{destdir}, $map);
waitpid $pid, 0; waitpid $pid, 0;
$SIG{PIPE}="DEFAULT"; $SIG{PIPE}="DEFAULT";
error gettext("failed to run graphviz") if $sigpipe; error gettext("failed to run graphviz") if ($sigpipe || $?);
if (! $params{preview}) { }
writefile($dest, $config{destdir}, $png, 1); else {
} $map=readfile("$config{destdir}/$imagemap");
else {
# in preview mode, embed the image in a data uri
# to avoid temp file clutter
eval q{use MIME::Base64};
error($@) if $@;
return "<img src=\"data:image/png;base64,".
encode_base64($png)."\" />";
}
} }
return "<img src=\"".urlto($dest, $params{destpage})."\" />\n"; return "<img src=\"".urlto($dest, $params{destpage}).
"\" usemap=\"#graph$graphnum\" />\n".
$map;
} }
sub graph (@) { sub graph (@) {

2
debian/changelog vendored
View File

@ -6,6 +6,8 @@ ikiwiki (3.20111107) UNRELEASED; urgency=low
* The umask setting can now be set to private, group, or public, * The umask setting can now be set to private, group, or public,
avoiding the need to enter octal correctly which is particularly avoiding the need to enter octal correctly which is particularly
difficult in yaml setup files. (smcv) difficult in yaml setup files. (smcv)
* graphviz: Support urls embedded in the graph, by having graphviz
generate an imagemap.
-- Joey Hess <joeyh@debian.org> Thu, 17 Nov 2011 18:52:23 -0400 -- Joey Hess <joeyh@debian.org> Thu, 17 Nov 2011 18:52:23 -0400

View File

@ -1,14 +1,10 @@
The `graph` directive is supplied by the [[!iki plugins/graphviz desc=graphviz]] plugin. The `graph` directive is supplied by the [[!iki plugins/graphviz desc=graphviz]] plugin.
This directive allows embedding [graphviz](http://www.graphviz.org/) graphs in a This directive allows embedding [graphviz](http://www.graphviz.org/)
page. Example usage: graphs in a page. Example usage:
\[[!graph src="a -> b -> c; a -> c;"]] \[[!graph src="a -> b -> c; a -> c;"]]
Note that graphs will only show up in previews if your browser has
[[!wikipedia data: URI]] support, or if the same graph already exists on that
page.
The `graph` directive supports the following parameters: The `graph` directive supports the following parameters:
- `src` - The graphviz source to render. - `src` - The graphviz source to render.