summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 9540bd112f397cd5cd98e89eb54572737aefa839 (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. # Can't just return the linkmap here, since the htmlscrubber
  25. # scrubs out all <object> tags (with good reason!)
  26. # Instead, insert a placeholder tag, which will be expanded during
  27. # formatting.
  28. $mapnum++;
  29. $maps{$mapnum}=\%params;
  30. return "<div class=\"linkmap$mapnum\"></div>";
  31. }
  32. sub format (@) {
  33. my %params=@_;
  34. $params{content}=~s/<div class=\"linkmap(\d+)"><\/div>/genmap($1)/eg;
  35. return $params{content};
  36. }
  37. sub genmap ($) {
  38. my $mapnum=shift;
  39. return "" unless exists $maps{$mapnum};
  40. my %params=%{$maps{$mapnum}};
  41. # Get all the items to map.
  42. my %mapitems = map { $_ => urlto($_, $params{destpage}) }
  43. pagespec_match_list($params{page}, $params{pages},
  44. # update when a page is added or removed, or its
  45. # links change
  46. deptype => deptype("presence", "links"));
  47. my $dest=$params{page}."/linkmap.png";
  48. # Use ikiwiki's function to create the file, this makes sure needed
  49. # subdirs are there and does some sanity checking.
  50. will_render($params{page}, $dest);
  51. writefile($dest, $config{destdir}, "");
  52. # Run dot to create the graphic and get the map data.
  53. my $pid;
  54. my $sigpipe=0;
  55. $SIG{PIPE}=sub { $sigpipe=1 };
  56. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
  57. # open2 doesn't respect "use open ':utf8'"
  58. binmode (IN, ':utf8');
  59. binmode (OUT, ':utf8');
  60. print OUT "digraph linkmap$mapnum {\n";
  61. print OUT "concentrate=true;\n";
  62. print OUT "charset=\"utf-8\";\n";
  63. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  64. if defined $params{width} and defined $params{height};
  65. foreach my $item (keys %mapitems) {
  66. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  67. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  68. print OUT "\"$item\" -> \"$link\";\n"
  69. if $mapitems{$link};
  70. }
  71. }
  72. print OUT "}\n";
  73. close OUT;
  74. local $/=undef;
  75. my $ret="<object data=\"".urlto($dest, $params{destpage}).
  76. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  77. <IN>.
  78. "</object>";
  79. close IN;
  80. waitpid $pid, 0;
  81. $SIG{PIPE}="DEFAULT";
  82. error gettext("failed to run dot") if $sigpipe;
  83. return $ret;
  84. }
  85. 1