summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 2405cae7f52c4199042642c9d2103a2fae1a38ae (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 -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. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  73. if defined $params{width} and defined $params{height};
  74. foreach my $item (keys %mapitems) {
  75. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  76. foreach my $link (map { IkiWiki::bestlink($item, $_) } @{$IkiWiki::links{$item}}) {
  77. print OUT "\"$item\" -> \"$link\";\n"
  78. if $mapitems{$link};
  79. }
  80. }
  81. print OUT "}\n";
  82. close OUT;
  83. local $/=undef;
  84. my $ret="<object data=\"".
  85. IkiWiki::abs2rel("$params{page}.png", IkiWiki::dirname($params{page})).
  86. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  87. <IN>.
  88. "</object>";
  89. close IN;
  90. waitpid $pid, 0;
  91. return $ret;
  92. } #}}}
  93. 1