summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 6b16097442cc468abcdb4f88754cf498eafab2c8 (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})) {
  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. writefile("$params{page}.png", $config{destdir}, "");
  48. # Run dot to create the graphic and get the map data.
  49. # TODO: should really add the png to renderedfiles and call
  50. # check_overwrite, but currently renderedfiles
  51. # only supports listing one file per page.
  52. my $tries=10;
  53. my $pid;
  54. while (1) {
  55. eval {
  56. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$params{page}.png' -Tcmapx");
  57. };
  58. last unless $@;
  59. $tries--;
  60. if ($tries < 1) {
  61. return "failed to run dot: $@";
  62. }
  63. }
  64. # open2 doesn't respect "use open ':utf8'"
  65. binmode (IN, ':utf8');
  66. binmode (OUT, ':utf8');
  67. print OUT "digraph linkmap$mapnum {\n";
  68. print OUT "concentrate=true;\n";
  69. print OUT "charset=\"utf-8\";\n";
  70. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  71. if defined $params{width} and defined $params{height};
  72. foreach my $item (keys %mapitems) {
  73. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  74. foreach my $link (map { bestlink($item, $_) } @{$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