summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 497b1ef43485dbdd4cb77043072f42c4a3b2898e (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 $pid;
  53. my $sigpipe=0;;
  54. $SIG{PIPE}=sub { $sigpipe=1 };
  55. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$params{page}.png' -Tcmapx");
  56. # open2 doesn't respect "use open ':utf8'"
  57. binmode (IN, ':utf8');
  58. binmode (OUT, ':utf8');
  59. print OUT "digraph linkmap$mapnum {\n";
  60. print OUT "concentrate=true;\n";
  61. print OUT "charset=\"utf-8\";\n";
  62. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  63. if defined $params{width} and defined $params{height};
  64. foreach my $item (keys %mapitems) {
  65. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  66. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  67. print OUT "\"$item\" -> \"$link\";\n"
  68. if $mapitems{$link};
  69. }
  70. }
  71. print OUT "}\n";
  72. close OUT;
  73. local $/=undef;
  74. my $ret="<object data=\"".
  75. IkiWiki::abs2rel("$params{page}.png", IkiWiki::dirname($params{page})).
  76. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  77. <IN>.
  78. "</object>";
  79. close IN;
  80. waitpid $pid, 0;
  81. $SIG{PIPE}="DEFAULT";
  82. if ($sigpipe) {
  83. return "[[linkmap failed to run dot]]";
  84. }
  85. return $ret;
  86. } #}}}
  87. 1