summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 2c92784c2a5e05b1892daf041845a28cf5acc883 (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. IkiWiki::hook(type => "preprocess", id => "linkmap",
  9. call => \&preprocess);
  10. IkiWiki::hook(type => "format", id => "linkmap",
  11. call => \&format);
  12. } # }}}
  13. my $mapnum=0;
  14. my %maps;
  15. sub preprocess (@) { #{{{
  16. my %params=@_;
  17. $params{pages}="*" unless defined $params{pages};
  18. # Needs to update whenever a page is added or removed, so
  19. # register a dependency.
  20. IkiWiki::add_depends($params{page}, $params{pages});
  21. # Can't just return the linkmap here, since the htmlscrubber
  22. # scrubs out all <object> tags (with good reason!)
  23. # Instead, insert a placeholder tag, which will be expanded during
  24. # formatting.
  25. $mapnum++;
  26. $maps{$mapnum}=\%params;
  27. return "<div class=\"linkmap$mapnum\"></div>";
  28. } # }}}
  29. sub format (@) { #{{{
  30. my %params=@_;
  31. $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
  32. return $params{content};
  33. } # }}}
  34. sub genmap ($) { #{{{
  35. my $mapnum=shift;
  36. return "" unless exists $maps{$mapnum};
  37. my %params=%{$maps{$mapnum}};
  38. # Get all the items to map.
  39. my %mapitems = ();
  40. foreach my $item (keys %IkiWiki::links) {
  41. if (IkiWiki::pagespec_match($item, $params{pages})) {
  42. my $link=IkiWiki::htmlpage($item);
  43. $link=IkiWiki::abs2rel($link, IkiWiki::dirname($params{page}));
  44. $mapitems{$item}=$link;
  45. }
  46. }
  47. # Use ikiwiki's function to create the file, this makes sure needed
  48. # subdirs are there and does some sanity checking.
  49. IkiWiki::writefile("$params{page}.png", $IkiWiki::config{destdir}, "");
  50. # Run dot to create the graphic and get the map data.
  51. # TODO: should really add the png to renderedfiles and call
  52. # check_overwrite, but currently renderedfiles
  53. # only supports listing one file per page.
  54. my $tries=10;
  55. my $pid;
  56. while (1) {
  57. eval {
  58. $pid=open2(*IN, *OUT, "dot /dev/stdin -Tpng -o '$IkiWiki::config{destdir}/$params{page}.png' -Tcmapx");
  59. };
  60. last unless $@;
  61. $tries--;
  62. if ($tries < 1) {
  63. return "failed to run dot: $@";
  64. }
  65. }
  66. # open2 doesn't respect "use open ':utf8'"
  67. binmode (IN, ':utf8');
  68. binmode (OUT, ':utf8');
  69. print OUT "digraph linkmap$mapnum {\n";
  70. print OUT "concentrate=true;\n";
  71. print OUT "charset=\"utf-8\";\n";
  72. foreach my $item (keys %mapitems) {
  73. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  74. foreach my $link (map { IkiWiki::bestlink($item, $_) } @{$IkiWiki::links{$item}}) {
  75. print OUT "\"$item\" -> \"$link\";\n"
  76. if $mapitems{$link};
  77. }
  78. }
  79. print OUT "}\n";
  80. close OUT;
  81. local $/=undef;
  82. my $ret="<object data=\"".
  83. IkiWiki::abs2rel("$params{page}.png", IkiWiki::dirname($params{page})).
  84. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  85. <IN>.
  86. "</object>";
  87. close IN;
  88. waitpid $pid, 0;
  89. return $ret;
  90. } #}}}
  91. 1