]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/linkmap.pm
Merge remote-tracking branch 'jcflack/early-env'
[ikiwiki.git] / IkiWiki / Plugin / linkmap.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::linkmap;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use IPC::Open2;
8 use HTML::Entities;
9
10 sub import {
11         hook(type => "getsetup", id => "linkmap", call => \&getsetup);
12         hook(type => "preprocess", id => "linkmap", call => \&preprocess);
13 }
14
15 sub getsetup () {
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => undef,
20                         section => "widget",
21                 },
22 }
23
24 my $mapnum=0;
25
26 sub pageescape {
27         my $item = shift;
28         # encoding explicitly in case ikiwiki is configured to accept <> or &
29         # in file names
30         my $title = pagetitle($item, 1);
31         # it would not be necessary to encode *all* the html entities (<> would
32         # be sufficient, &" probably a good idea), as dot accepts utf8, but it
33         # isn't bad either
34         $title = encode_entities($title);
35         return("<$title>");
36 }
37
38 sub preprocess (@) {
39         my %params=@_;
40
41         $params{pages}="*" unless defined $params{pages};
42         
43         $mapnum++;
44         my $connected=IkiWiki::yesno($params{connected});
45
46         # Get all the items to map.
47         my %mapitems = map { $_ => urlto($_, $params{destpage}) }
48                 pagespec_match_list($params{page}, $params{pages},
49                         # update when a page is added or removed, or its
50                         # links change
51                         deptype => deptype("presence", "links"));
52
53         my $dest=$params{page}."/linkmap.png";
54
55         # Use ikiwiki's function to create the file, this makes sure needed
56         # subdirs are there and does some sanity checking.
57         will_render($params{page}, $dest);
58         writefile($dest, $config{destdir}, "");
59
60         # Run dot to create the graphic and get the map data.
61         my $pid;
62         my $sigpipe=0;
63         $SIG{PIPE}=sub { $sigpipe=1 };
64         $pid=open2(*IN, *OUT, "dot -Tpng -o '$config{destdir}/$dest' -Tcmapx");
65         
66         # open2 doesn't respect "use open ':utf8'"
67         binmode (IN, ':utf8'); 
68         binmode (OUT, ':utf8'); 
69
70         print OUT "digraph linkmap$mapnum {\n";
71         print OUT "concentrate=true;\n";
72         print OUT "charset=\"utf-8\";\n";
73         print OUT "ratio=compress;\nsize=\"".($params{width}+0).", ".($params{height}+0)."\";\n"
74                 if defined $params{width} and defined $params{height};
75         my %shown;
76         my $show=sub {
77                 my $item=shift;
78                 if (! $shown{$item}) {
79                         print OUT pageescape($item)." [shape=box,href=\"$mapitems{$item}\"];\n";
80                         $shown{$item}=1;
81                 }
82         };
83         foreach my $item (keys %mapitems) {
84                 $show->($item) unless $connected;
85                 foreach my $link (map { bestlink($item, $_) } @{$links{$item}}) {
86                         next unless length $link and $mapitems{$link};
87                         foreach my $endpoint ($item, $link) {
88                                 $show->($endpoint);
89                         }
90                         print OUT pageescape($item)." -> ".pageescape($link).";\n";
91                 }
92         }
93         print OUT "}\n";
94         close OUT || error gettext("failed to run dot");
95
96         local $/=undef;
97         my $ret="<img src=\"".urlto($dest, $params{destpage}).
98                "\" alt=\"".gettext("linkmap").
99                "\" usemap=\"#linkmap$mapnum\" />\n".
100                 <IN>;
101         close IN || error gettext("failed to run dot");
102         
103         waitpid $pid, 0;
104         if ($?) {
105                 error gettext("failed to run dot");
106         }
107         $SIG{PIPE}="DEFAULT";
108         error gettext("failed to run dot") if $sigpipe;
109
110         return $ret;
111 }
112
113 1