summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/linkmap.pm
blob: 28acbda32abdbb4a9abd022d96a9906b81e987b7 (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. my $connected=IkiWiki::yesno($params{connected});
  42. # Get all the items to map.
  43. my %mapitems = map { $_ => urlto($_, $params{destpage}) }
  44. pagespec_match_list($params{page}, $params{pages},
  45. # update when a page is added or removed, or its
  46. # links change
  47. deptype => deptype("presence", "links"));
  48. my $dest=$params{page}."/linkmap.png";
  49. # Use ikiwiki's function to create the file, this makes sure needed
  50. # subdirs are there and does some sanity checking.
  51. will_render($params{page}, $dest);
  52. writefile($dest, $config{destdir}, "");
  53. # Run dot to create the graphic and get the map data.
  54. my $pid;
  55. my $sigpipe=0;
  56. $SIG{PIPE}=sub { $sigpipe=1 };
  57. $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
  58. # open2 doesn't respect "use open ':utf8'"
  59. binmode (IN, ':utf8');
  60. binmode (OUT, ':utf8');
  61. print OUT "digraph linkmap$mapnum {\n";
  62. print OUT "concentrate=true;\n";
  63. print OUT "charset=\"utf-8\";\n";
  64. print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
  65. if defined $params{width} and defined $params{height};
  66. my %shown;
  67. my $show=sub {
  68. my $item=shift;
  69. if (! $shown{$item}) {
  70. print OUT "\"$item\" [shape=box,href=\"$mapitems{$item}\"];\n";
  71. $shown{$item}=1;
  72. }
  73. };
  74. foreach my $item (keys %mapitems) {
  75. $show->($item) unless $connected;
  76. foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
  77. next unless length $link and $mapitems{$link};
  78. foreach my $endpoint ($item, $link) {
  79. $show->($endpoint);
  80. }
  81. print OUT "\"$item\" -> \"$link\";\n";
  82. }
  83. }
  84. print OUT "}\n";
  85. close OUT || error gettext("failed to run dot");
  86. local $/=undef;
  87. my $ret="<object data=\"".urlto($dest, $params{destpage}).
  88. "\" type=\"image/png\" usemap=\"#linkmap$mapnum\">\n".
  89. <IN>.
  90. "</object>";
  91. close IN || error gettext("failed to run dot");
  92. waitpid $pid, 0;
  93. if ($?) {
  94. error gettext("failed to run dot");
  95. }
  96. $SIG{PIPE}="DEFAULT";
  97. error gettext("failed to run dot") if $sigpipe;
  98. return $ret;
  99. }
  100. 1