summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: c5055982935c3b9ab6a9357f8dd4a8264067a04e (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::linkmap;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  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}, $params{page})) {
  40. $mapitems{$item}=urlto($item, $params{destpage});
  41. }
  42. }
  43. # Use ikiwiki's function to create the file, this makes sure needed
  44. # subdirs are there and does some sanity checking.
  45. will_render($params{page}, $params{page}.".png");
  46. writefile($params{page}.".png", $config{destdir}, "");
  47. # Run dot to create the graphic and get the map data.
  48. my $pid;
  49. my $sigpipe=0;;
  50. $SIG{PIPE}=sub { $sigpipe=1 };
  51. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$params{page}.png' -Tcmapx");
  52. # open2 doesn't respect "use open ':utf8'"
  53. binmode (IN, ':utf8');
  54. binmode (OUT, ':utf8');
  55. print OUT "digraph linkmap$mapnum {\n";
  56. print OUT "concentrate=true;\n";
  57. print OUT "charset=\"utf-8\";\n";
  58. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  59. if defined $params{width} and defined $params{height};
  60. foreach my $item (keys %mapitems) {
  61. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  62. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  63. print OUT "\"$item\" -> \"$link\";\n"
  64. if $mapitems{$link};
  65. }
  66. }
  67. print OUT "}\n";
  68. close OUT;
  69. local $/=undef;
  70. my $ret="<object data=\"".
  71. IkiWiki::abs2rel("$params{page}.png", IkiWiki::dirname($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