summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: cb9f3ef413cc0e0c4fa669595f376619fcb881e0 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::linkmap;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. use IPC::Open2;
  7. sub import {
  8. hook(type => "getsetup", id => "linkmap", call => \&getsetup);
  9. hook(type => "preprocess", id => "linkmap", call => \&preprocess);
  10. hook(type => "format", id => "linkmap", call => \&format);
  11. }
  12. sub getsetup () {
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => undef,
  17. },
  18. }
  19. my $mapnum=0;
  20. my %maps;
  21. sub preprocess (@) {
  22. my %params=@_;
  23. $params{pages}="*" unless defined $params{pages};
  24. # Needs to update whenever a page is added or removed, so
  25. # register a dependency.
  26. add_depends($params{page}, $params{pages});
  27. # Can't just return the linkmap here, since the htmlscrubber
  28. # scrubs out all <object> tags (with good reason!)
  29. # Instead, insert a placeholder tag, which will be expanded during
  30. # formatting.
  31. $mapnum++;
  32. $maps{$mapnum}=\%params;
  33. return "<div class=\"linkmap$mapnum\"></div>";
  34. }
  35. sub format (@) {
  36. my %params=@_;
  37. $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
  38. return $params{content};
  39. }
  40. sub genmap ($) {
  41. my $mapnum=shift;
  42. return "" unless exists $maps{$mapnum};
  43. my %params=%{$maps{$mapnum}};
  44. # Get all the items to map.
  45. my %mapitems = ();
  46. foreach my $item (keys %links) {
  47. if (pagespec_match($item, $params{pages}, location => $params{page})) {
  48. $mapitems{$item}=urlto($item, $params{destpage});
  49. }
  50. }
  51. my $dest=$params{page}."/linkmap.png";
  52. # Use ikiwiki's function to create the file, this makes sure needed
  53. # subdirs are there and does some sanity checking.
  54. will_render($params{page}, $dest);
  55. writefile($dest, $config{destdir}, "");
  56. # Run dot to create the graphic and get the map data.
  57. my $pid;
  58. my $sigpipe=0;
  59. $SIG{PIPE}=sub { $sigpipe=1 };
  60. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
  61. # open2 doesn't respect "use open ':utf8'"
  62. binmode (IN, ':utf8');
  63. binmode (OUT, ':utf8');
  64. print OUT "digraph linkmap$mapnum {\n";
  65. print OUT "concentrate=true;\n";
  66. print OUT "charset=\"utf-8\";\n";
  67. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  68. if defined $params{width} and defined $params{height};
  69. foreach my $item (keys %mapitems) {
  70. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  71. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  72. print OUT "\"$item\" -> \"$link\";\n"
  73. if $mapitems{$link};
  74. }
  75. }
  76. print OUT "}\n";
  77. close OUT;
  78. local $/=undef;
  79. my $ret="<object data=\"".urlto($dest, $params{destpage}).
  80. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  81. <IN>.
  82. "</object>";
  83. close IN;
  84. waitpid $pid, 0;
  85. $SIG{PIPE}="DEFAULT";
  86. error gettext("failed to run dot") if $sigpipe;
  87. return $ret;
  88. }
  89. 1