summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/graphviz.pm
blob: dfd66a03e2250723132caa75c2f9d5da2e2c4fc2 (plain)
  1. #!/usr/bin/perl
  2. # graphviz plugin for ikiwiki: render graphviz source as an image.
  3. # Josh Triplett
  4. package IkiWiki::Plugin::graphviz;
  5. use warnings;
  6. use strict;
  7. use IkiWiki 3.00;
  8. use IPC::Open2;
  9. sub import {
  10. hook(type => "getsetup", id => "graphviz", call => \&getsetup);
  11. hook(type => "preprocess", id => "graph", call => \&graph);
  12. }
  13. sub getsetup () {
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => undef,
  18. section => "widget",
  19. },
  20. }
  21. my %graphviz_programs = (
  22. "dot" => 1, "neato" => 1, "fdp" => 1, "twopi" => 1, "circo" => 1
  23. );
  24. sub render_graph (\%) {
  25. my %params = %{(shift)};
  26. my $src = "$params{type} g {\n";
  27. $src .= "charset=\"utf-8\";\n";
  28. $src .= "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  29. if defined $params{width} and defined $params{height};
  30. $src .= $params{src};
  31. $src .= "}\n";
  32. # Use the sha1 of the graphviz code as part of its filename.
  33. eval q{use Digest::SHA1};
  34. error($@) if $@;
  35. my $dest=$params{page}."/graph-".
  36. IkiWiki::possibly_foolish_untaint(Digest::SHA1::sha1_hex($src)).
  37. ".png";
  38. will_render($params{page}, $dest);
  39. if (! -e "$config{destdir}/$dest") {
  40. my $pid;
  41. my $sigpipe=0;
  42. $SIG{PIPE}=sub { $sigpipe=1 };
  43. $pid=open2(*IN, *OUT, "$params{prog} -Tpng");
  44. # open2 doesn't respect "use open ':utf8'"
  45. binmode (OUT, ':utf8');
  46. print OUT $src;
  47. close OUT;
  48. my $png;
  49. {
  50. local $/ = undef;
  51. $png = <IN>;
  52. }
  53. close IN;
  54. waitpid $pid, 0;
  55. $SIG{PIPE}="DEFAULT";
  56. error gettext("failed to run graphviz") if $sigpipe;
  57. if (! $params{preview}) {
  58. writefile($dest, $config{destdir}, $png, 1);
  59. }
  60. else {
  61. # in preview mode, embed the image in a data uri
  62. # to avoid temp file clutter
  63. eval q{use MIME::Base64};
  64. error($@) if $@;
  65. return "<img src=\"data:image/png;base64,".
  66. encode_base64($png)."\" />";
  67. }
  68. }
  69. return "<img src=\"".urlto($dest, $params{destpage})."\" />\n";
  70. }
  71. sub graph (@) {
  72. my %params=@_;
  73. $params{src} = "" unless defined $params{src};
  74. $params{type} = "digraph" unless defined $params{type};
  75. $params{prog} = "dot" unless defined $params{prog};
  76. error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
  77. return render_graph(%params);
  78. }
  79. 1