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