summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 0137476acfd60b5aa07693960c59743bab4214df (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::linkmap;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.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 (pagespec_match_list([keys %links],
  47. $params{pages}, location => $params{page})) {
  48. $mapitems{$item}=urlto($item, $params{destpage});
  49. }
  50. my $dest=$params{page}."/linkmap.png";
  51. # Use ikiwiki's function to create the file, this makes sure needed
  52. # subdirs are there and does some sanity checking.
  53. will_render($params{page}, $dest);
  54. writefile($dest, $config{destdir}, "");
  55. # Run dot to create the graphic and get the map data.
  56. my $pid;
  57. my $sigpipe=0;
  58. $SIG{PIPE}=sub { $sigpipe=1 };
  59. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
  60. # open2 doesn't respect "use open ':utf8'"
  61. binmode (IN, ':utf8');
  62. binmode (OUT, ':utf8');
  63. print OUT "digraph linkmap$mapnum {\n";
  64. print OUT "concentrate=true;\n";
  65. print OUT "charset=\"utf-8\";\n";
  66. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  67. if defined $params{width} and defined $params{height};
  68. foreach my $item (keys %mapitems) {
  69. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  70. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  71. print OUT "\"$item\" -> \"$link\";\n"
  72. if $mapitems{$link};
  73. }
  74. }
  75. print OUT "}\n";
  76. close OUT;
  77. local $/=undef;
  78. my $ret="<object data=\"".urlto($dest, $params{destpage}).
  79. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  80. <IN>.
  81. "</object>";
  82. close IN;
  83. waitpid $pid, 0;
  84. $SIG{PIPE}="DEFAULT";
  85. error gettext("failed to run dot") if $sigpipe;
  86. return $ret;
  87. }
  88. 1