summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 528d51697fa74e37f2fcefaa15113d94d40e6027 (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. my $link=htmlpage($item);
  41. $link=IkiWiki::abs2rel($link, IkiWiki::dirname($params{page}));
  42. $mapitems{$item}=$link;
  43. }
  44. }
  45. # Use ikiwiki's function to create the file, this makes sure needed
  46. # subdirs are there and does some sanity checking.
  47. will_render($params{page}, $params{page}.".png");
  48. writefile($params{page}.".png", $config{destdir}, "");
  49. # Run dot to create the graphic and get the map data.
  50. my $pid;
  51. my $sigpipe=0;;
  52. $SIG{PIPE}=sub { $sigpipe=1 };
  53. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$params{page}.png' -Tcmapx");
  54. # open2 doesn't respect "use open ':utf8'"
  55. binmode (IN, ':utf8');
  56. binmode (OUT, ':utf8');
  57. print OUT "digraph linkmap$mapnum {\n";
  58. print OUT "concentrate=true;\n";
  59. print OUT "charset=\"utf-8\";\n";
  60. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  61. if defined $params{width} and defined $params{height};
  62. foreach my $item (keys %mapitems) {
  63. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  64. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  65. print OUT "\"$item\" -> \"$link\";\n"
  66. if $mapitems{$link};
  67. }
  68. }
  69. print OUT "}\n";
  70. close OUT;
  71. local $/=undef;
  72. my $ret="<object data=\"".
  73. IkiWiki::abs2rel("$params{page}.png", IkiWiki::dirname($params{page})).
  74. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  75. <IN>.
  76. "</object>";
  77. close IN;
  78. waitpid $pid, 0;
  79. $SIG{PIPE}="DEFAULT";
  80. if ($sigpipe) {
  81. return "[[".gettext("linkmap failed to run dot")."]]";
  82. }
  83. return $ret;
  84. } #}}}
  85. 1