]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
* Add a linkmap plugin (requires graphviz).
[ikiwiki.git] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use URI;
9
10 sub import { #{{{
11         IkiWiki::hook(type => "preprocess", id => "inline", 
12                 call => \&IkiWiki::preprocess_inline);
13         IkiWiki::hook(type => "pagetemplate", id => "inline",
14                 call => \&IkiWiki::pagetemplate_inline);
15         # Hook to change to do pinging since it's called late.
16         # This ensures each page only pings once and prevents slow
17         # pings interrupting page builds.
18         IkiWiki::hook(type => "change", id => "inline", 
19                 call => \&IkiWiki::pingurl);
20 } # }}}
21
22 # Back to ikiwiki namespace for the rest, this code is very much
23 # internal to ikiwiki even though it's separated into a plugin.
24 package IkiWiki;
25
26 my %toping;
27 my %rsslinks;
28
29 sub yesno ($) { #{{{
30         my $val=shift;
31         return (defined $val && lc($val) eq "yes");
32 } #}}}
33
34 sub preprocess_inline (@) { #{{{
35         my %params=@_;
36         
37         if (! exists $params{pages}) {
38                 return "";
39         }
40         my $raw=yesno($params{raw});
41         my $archive=yesno($params{archive});
42         my $rss=exists $params{rss} ? yesno($params{rss}) : 1;
43         if (! exists $params{show} && ! $archive) {
44                 $params{show}=10;
45         }
46         my $desc;
47         if (exists $params{description}) {
48                 $desc = $params{description} 
49         } else {
50                 $desc = $config{wikiname};
51         }
52         my $actions=yesno($params{actions});
53
54         my @list;
55         foreach my $page (keys %pagesources) {
56                 next if $page eq $params{page};
57                 if (pagespec_match($page, $params{pages})) {
58                         push @list, $page;
59                 }
60         }
61         @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
62         if ($params{show} && @list > $params{show}) {
63                 @list=@list[0..$params{show} - 1];
64         }
65
66         add_depends($params{page}, $params{pages});
67
68         my $rssurl=rsspage(basename($params{page}));
69         my $ret="";
70
71         if (exists $params{rootpage} && $config{cgiurl}) {
72                 # Add a blog post form, with a rss link button.
73                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
74                 $formtemplate->param(cgiurl => $config{cgiurl});
75                 $formtemplate->param(rootpage => $params{rootpage});
76                 if ($config{rss}) {
77                         $formtemplate->param(rssurl => $rssurl);
78                 }
79                 $ret.=$formtemplate->output;
80         }
81         elsif ($config{rss} && $rss) {
82                 # Add a rss link button.
83                 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
84                 $linktemplate->param(rssurl => $rssurl);
85                 $ret.=$linktemplate->output;
86         }
87         
88         my $template=template(
89                 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
90                 blind_cache => 1,
91         ) unless $raw;
92         
93         foreach my $page (@list) {
94                 if (! $raw) {
95                         # Get the content before populating the template,
96                         # since getting the content uses the same template
97                         # if inlines are nested.
98                         # TODO: if $archive=1, the only reason to do this
99                         # is to let the meta plugin get page title info; so stop
100                         # calling this next line then once the meta plugin can
101                         # store that accross runs (also tags plugin).
102                         my $content=get_inline_content($page, $params{destpage});
103                         # Don't use htmllink because this way the title is separate
104                         # and can be overridden by other plugins.
105                         my $link=htmlpage(bestlink($params{page}, $page));
106                         $link=abs2rel($link, dirname($params{destpage}));
107                         $template->param(pageurl => $link);
108                         $template->param(title => pagetitle(basename($page)));
109                         $template->param(content => $content);
110                         $template->param(ctime => displaytime($pagectime{$page}));
111
112                         if ($actions) {
113                                 my $file = $pagesources{$page};
114                                 my $type = pagetype($file);
115                                 if ($config{discussion}) {
116                                         $template->param(have_actions => 1);
117                                         $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
118                                 }
119                                 if (length $config{cgiurl} && defined $type) {
120                                         $template->param(have_actions => 1);
121                                         $template->param(editurl => cgiurl(do => "edit", page => $page));
122                                 }
123                         }
124
125                         run_hooks(pagetemplate => sub {
126                                 shift->(page => $page, destpage => $params{page},
127                                         template => $template,);
128                         });
129
130                         $ret.=$template->output;
131                         $template->clear_params;
132                 }
133                 else {
134                         my $file=$pagesources{$page};
135                         my $type=pagetype($file);
136                         if (defined $type) {
137                                 $ret.="\n".
138                                       linkify($page, $params{page},
139                                       preprocess($page, $params{page},
140                                       filter($page,
141                                       readfile(srcfile($file)))));
142                         }
143                 }
144         }
145         
146         # TODO: should really add this to renderedfiles and call
147         # check_overwrite, but currently renderedfiles
148         # only supports listing one file per page.
149         if ($config{rss} && $rss) {
150                 writefile(rsspage($params{page}), $config{destdir},
151                         genrss($desc, $params{page}, @list));
152                 $toping{$params{page}}=1 unless $config{rebuild};
153                 $rsslinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
154         }
155         
156         return $ret;
157 } #}}}
158
159 sub pagetemplate_inline (@) { #{{{
160         my %params=@_;
161         my $page=$params{page};
162         my $template=$params{template};
163
164         $template->param(rsslink => $rsslinks{$page})
165                 if exists $rsslinks{$page} && $template->query(name => "rsslink");
166 } #}}}
167
168 sub get_inline_content ($$) { #{{{
169         my $page=shift;
170         my $destpage=shift;
171         
172         my $file=$pagesources{$page};
173         my $type=pagetype($file);
174         if (defined $type) {
175                 return htmlize($page, $type,
176                        linkify($page, $destpage,
177                        preprocess($page, $destpage,
178                        filter($page,
179                        readfile(srcfile($file))))));
180         }
181         else {
182                 return "";
183         }
184 } #}}}
185
186 sub date_822 ($) { #{{{
187         my $time=shift;
188
189         eval q{use POSIX};
190         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
191         POSIX::setlocale(&POSIX::LC_TIME, "C");
192         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
193         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
194         return $ret;
195 } #}}}
196
197 sub absolute_urls ($$) { #{{{
198         # sucky sub because rss sucks
199         my $content=shift;
200         my $url=shift;
201
202         $url=~s/[^\/]+$//;
203         
204         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
205         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
206         return $content;
207 } #}}}
208
209 sub rsspage ($) { #{{{
210         my $page=shift;
211
212         return $page.".rss";
213 } #}}}
214
215 sub genrss ($$@) { #{{{
216         my $desc=shift;
217         my $page=shift;
218         my @pages=@_;
219         
220         my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
221         
222         my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
223         my $content="";
224         foreach my $p (@pages) {
225                 next unless exists $renderedfiles{$p};
226
227                 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
228
229                 $itemtemplate->param(
230                         title => pagetitle(basename($p)),
231                         url => $u,
232                         permalink => $u,
233                         pubdate => date_822($pagectime{$p}),
234                         content => absolute_urls(get_inline_content($p, $page), $url),
235                 );
236                 run_hooks(pagetemplate => sub {
237                         shift->(page => $p, destpage => $page,
238                                 template => $itemtemplate);
239                 });
240
241                 $content.=$itemtemplate->output;
242                 $itemtemplate->clear_params;
243         }
244
245         my $template=template("rsspage.tmpl", blind_cache => 1);
246         $template->param(
247                 title => $config{wikiname},
248                 wikiname => $config{wikiname},
249                 pageurl => $url,
250                 content => $content,
251                 rssdesc => $desc,
252         );
253         run_hooks(pagetemplate => sub {
254                 shift->(page => $page, destpage => $page,
255                         template => $template);
256         });
257         
258         return $template->output;
259 } #}}}
260
261 sub pingurl (@) { #{{{
262         return unless $config{pingurl} && %toping;
263
264         eval q{require RPC::XML::Client};
265         if ($@) {
266                 debug("RPC::XML::Client not found, not pinging");
267                 return;
268         }
269
270         foreach my $page (keys %toping) {
271                 my $title=pagetitle(basename($page));
272                 my $url="$config{url}/".htmlpage($page);
273                 foreach my $pingurl (@{$config{pingurl}}) {
274                         my $client = RPC::XML::Client->new($pingurl);
275                         my $req = RPC::XML::request->new('weblogUpdates.ping',
276                                 $title, $url);
277                         debug("Pinging $pingurl for $page");
278                         my $res = $client->send_request($req);
279                         if (! ref $res) {
280                                 debug("Did not receive response to ping");
281                         }
282                         my $r=$res->value;
283                         if (! exists $r->{flerror} || $r->{flerror}) {
284                                 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
285                         }
286                 }
287         }
288 } #}}}
289
290 1