summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 5b87277ac1982f0dd73d5f4f06ad881e31d2c7f5 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::linkmap;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. use IPC::Open2;
  7. sub import { #{{{
  8. hook(type => "preprocess", id => "linkmap", call => \&preprocess);
  9. hook(type => "format", id => "linkmap", call => \&format);
  10. } # }}}
  11. my $mapnum=0;
  12. my %maps;
  13. sub preprocess (@) { #{{{
  14. my %params=@_;
  15. $params{pages}="*" unless defined $params{pages};
  16. # Needs to update whenever a page is added or removed, so
  17. # register a dependency.
  18. add_depends($params{page}, $params{pages});
  19. # Can't just return the linkmap here, since the htmlscrubber
  20. # scrubs out all <object> tags (with good reason!)
  21. # Instead, insert a placeholder tag, which will be expanded during
  22. # formatting.
  23. $mapnum++;
  24. $maps{$mapnum}=\%params;
  25. return "<div class=\"linkmap$mapnum\"></div>";
  26. } # }}}
  27. sub format (@) { #{{{
  28. my %params=@_;
  29. $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
  30. return $params{content};
  31. } # }}}
  32. sub genmap ($) { #{{{
  33. my $mapnum=shift;
  34. return "" unless exists $maps{$mapnum};
  35. my %params=%{$maps{$mapnum}};
  36. # Get all the items to map.
  37. my %mapitems = ();
  38. foreach my $item (keys %links) {
  39. if (pagespec_match($item, $params{pages}, location => $params{page})) {
  40. $mapitems{$item}=urlto($item, $params{destpage});
  41. }
  42. }
  43. my $dest=$params{destpage}."/linkmap.png";
  44. # Use ikiwiki's function to create the file, this makes sure needed
  45. # subdirs are there and does some sanity checking.
  46. will_render($params{destpage}, $dest);
  47. writefile($dest, $config{destdir}, "");
  48. # Run dot to create the graphic and get the map data.
  49. my $pid;
  50. my $sigpipe=0;
  51. $SIG{PIPE}=sub { $sigpipe=1 };
  52. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
  53. # open2 doesn't respect "use open ':utf8'"
  54. binmode (IN, ':utf8');
  55. binmode (OUT, ':utf8');
  56. print OUT "digraph linkmap$mapnum {\n";
  57. print OUT "concentrate=true;\n";
  58. print OUT "charset=\"utf-8\";\n";
  59. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  60. if defined $params{width} and defined $params{height};
  61. foreach my $item (keys %mapitems) {
  62. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  63. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  64. print OUT "\"$item\" -> \"$link\";\n"
  65. if $mapitems{$link};
  66. }
  67. }
  68. print OUT "}\n";
  69. close OUT;
  70. local $/=undef;
  71. my $ret="<object data=\"".urlto($dest, $params{page}).
  72. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  73. <IN>.
  74. "</object>";
  75. close IN;
  76. waitpid $pid, 0;
  77. $SIG{PIPE}="DEFAULT";
  78. if ($sigpipe) {
  79. return "[[linkmap ".gettext("failed to run dot")."]]";
  80. }
  81. return $ret;
  82. } #}}}
  83. 1