summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/graphviz.pm
blob: bec12207696ed5639b71dfd773bf261b8b1658b7 (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. # can't write the file, so embed it in a data uri
  62. eval q{use MIME::Base64};
  63. error($@) if $@;
  64. return "<img src=\"data:image/png;base64,".
  65. encode_base64($png)."\" />";
  66. }
  67. }
  68. if ($params{preview}) {
  69. return "<img src=\"".urlto($dest, "")."\" />\n";
  70. }
  71. else {
  72. return "<img src=\"".urlto($dest, $params{destpage})."\" />\n";
  73. }
  74. }
  75. sub graph (@) {
  76. my %params=@_;
  77. $params{src} = "" unless defined $params{src};
  78. $params{type} = "digraph" unless defined $params{type};
  79. $params{prog} = "dot" unless defined $params{prog};
  80. error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};
  81. return render_graph(%params);
  82. }
  83. 1