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