* Atom feed support based on a patch by Clint Adams.
[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 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 %feedlinks;
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=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
44         my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
45         my $feeds=exists $params{feeds} ? yesno($params{feeds}) : 1;
46         if (! exists $params{show} && ! $archive) {
47                 $params{show}=10;
48         }
49         my $desc;
50         if (exists $params{description}) {
51                 $desc = $params{description} 
52         } else {
53                 $desc = $config{wikiname};
54         }
55         my $actions=yesno($params{actions});
56
57         my @list;
58         foreach my $page (keys %pagesources) {
59                 next if $page eq $params{page};
60                 if (pagespec_match($page, $params{pages})) {
61                         push @list, $page;
62                 }
63         }
64
65         if (exists $params{sort} && $params{sort} eq 'title') {
66                 @list=sort @list;
67         }
68         elsif (! exists $params{sort} || $params{sort} eq 'age') {
69                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
70         }
71         else {
72                 return "unknown sort type $params{sort}";
73         }
74
75         if ($params{show} && @list > $params{show}) {
76                 @list=@list[0..$params{show} - 1];
77         }
78
79         add_depends($params{page}, $params{pages});
80
81         my $rssurl=rsspage(basename($params{page}));
82         my $atomurl=atompage(basename($params{page}));
83         my $ret="";
84
85         if (exists $params{rootpage} && $config{cgiurl}) {
86                 # Add a blog post form, with feed buttons.
87                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
88                 $formtemplate->param(cgiurl => $config{cgiurl});
89                 $formtemplate->param(rootpage => $params{rootpage});
90                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
91                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
92                 $ret.=$formtemplate->output;
93         }
94         elsif ($feeds) {
95                 # Add feed buttons.
96                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
97                 $linktemplate->param(rssurl => $rssurl) if $rss;
98                 $linktemplate->param(atomurl => $atomurl) if $atom;
99                 $ret.=$linktemplate->output;
100         }
101         
102         my $template=template(
103                 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
104                 blind_cache => 1,
105         ) unless $raw;
106         
107         foreach my $page (@list) {
108                 if (! $raw) {
109                         # Get the content before populating the template,
110                         # since getting the content uses the same template
111                         # if inlines are nested.
112                         # TODO: if $archive=1, the only reason to do this
113                         # is to let the meta plugin get page title info; so stop
114                         # calling this next line then once the meta plugin can
115                         # store that accross runs (also tags plugin).
116                         my $content=get_inline_content($page, $params{destpage});
117                         # Don't use htmllink because this way the title is separate
118                         # and can be overridden by other plugins.
119                         my $link=htmlpage(bestlink($params{page}, $page));
120                         $link=abs2rel($link, dirname($params{destpage}));
121                         $template->param(pageurl => $link);
122                         $template->param(title => pagetitle(basename($page)));
123                         $template->param(content => $content);
124                         $template->param(ctime => displaytime($pagectime{$page}));
125
126                         if ($actions) {
127                                 my $file = $pagesources{$page};
128                                 my $type = pagetype($file);
129                                 if ($config{discussion}) {
130                                         $template->param(have_actions => 1);
131                                         $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
132                                 }
133                                 if (length $config{cgiurl} && defined $type) {
134                                         $template->param(have_actions => 1);
135                                         $template->param(editurl => cgiurl(do => "edit", page => $page));
136                                 }
137                         }
138
139                         run_hooks(pagetemplate => sub {
140                                 shift->(page => $page, destpage => $params{page},
141                                         template => $template,);
142                         });
143
144                         $ret.=$template->output;
145                         $template->clear_params;
146                 }
147                 else {
148                         my $file=$pagesources{$page};
149                         my $type=pagetype($file);
150                         if (defined $type) {
151                                 $ret.="\n".
152                                       linkify($page, $params{page},
153                                       preprocess($page, $params{page},
154                                       filter($page,
155                                       readfile(srcfile($file)))));
156                         }
157                 }
158         }
159         
160         if ($feeds && $rss) {
161                 will_render($params{page}, rsspage($params{page}));
162                 writefile(rsspage($params{page}), $config{destdir},
163                         genfeed("rss", $rssurl, $desc, $params{page}, @list));
164                 $toping{$params{page}}=1 unless $config{rebuild};
165                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
166         }
167         if ($feeds && $atom) {
168                 will_render($params{page}, atompage($params{page}));
169                 writefile(atompage($params{page}), $config{destdir},
170                         genfeed("atom", $atomurl, $desc, $params{page}, @list));
171                 $toping{$params{page}}=1 unless $config{rebuild};
172                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
173         }
174         
175         return $ret;
176 } #}}}
177
178 sub pagetemplate_inline (@) { #{{{
179         my %params=@_;
180         my $page=$params{page};
181         my $template=$params{template};
182
183         $template->param(feedlinks => $feedlinks{$page})
184                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
185 } #}}}
186
187 sub get_inline_content ($$) { #{{{
188         my $page=shift;
189         my $destpage=shift;
190         
191         my $file=$pagesources{$page};
192         my $type=pagetype($file);
193         if (defined $type) {
194                 return htmlize($page, $type,
195                        linkify($page, $destpage,
196                        preprocess($page, $destpage,
197                        filter($page,
198                        readfile(srcfile($file))))));
199         }
200         else {
201                 return "";
202         }
203 } #}}}
204
205 sub date_822 ($) { #{{{
206         my $time=shift;
207
208         eval q{use POSIX};
209         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
210         POSIX::setlocale(&POSIX::LC_TIME, "C");
211         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
212         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
213         return $ret;
214 } #}}}
215
216 sub date_3339 ($) { #{{{
217         my $time=shift;
218
219         eval q{use POSIX};
220         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
221         POSIX::setlocale(&POSIX::LC_TIME, "C");
222         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
223         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
224         return $ret;
225 } #}}}
226
227 sub absolute_urls ($$) { #{{{
228         # sucky sub because rss sucks
229         my $content=shift;
230         my $url=shift;
231
232         $url=~s/[^\/]+$//;
233         
234         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
235         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
236         return $content;
237 } #}}}
238
239 sub rsspage ($) { #{{{
240         my $page=shift;
241
242         return $page.".rss";
243 } #}}}
244
245 sub atompage ($) { #{{{
246         my $page=shift;
247
248         return $page.".atom";
249 } #}}}
250
251 sub genfeed ($$$$@) { #{{{
252         my $feedtype=shift;
253         my $feedurl=shift;
254         my $feeddesc=shift;
255         my $page=shift;
256         my @pages=@_;
257         
258         my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
259         
260         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
261         my $content="";
262         my $lasttime = 0;
263         foreach my $p (@pages) {
264                 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
265
266                 $itemtemplate->param(
267                         title => pagetitle(basename($p)),
268                         url => $u,
269                         permalink => $u,
270                         date_822 => date_822($pagectime{$p}),
271                         date_3339 => date_3339($pagectime{$p}),
272                         content => absolute_urls(get_inline_content($p, $page), $url),
273                 );
274                 run_hooks(pagetemplate => sub {
275                         shift->(page => $p, destpage => $page,
276                                 template => $itemtemplate);
277                 });
278
279                 $content.=$itemtemplate->output;
280                 $itemtemplate->clear_params;
281
282                 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
283         }
284
285         my $template=template($feedtype."page.tmpl", blind_cache => 1);
286         $template->param(
287                 title => $config{wikiname},
288                 wikiname => $config{wikiname},
289                 pageurl => $url,
290                 content => $content,
291                 feeddesc => $feeddesc,
292                 feeddate => date_3339($lasttime),
293                 feedurl => $feedurl,
294                 version => $IkiWiki::version,
295         );
296         run_hooks(pagetemplate => sub {
297                 shift->(page => $page, destpage => $page,
298                         template => $template);
299         });
300         
301         return $template->output;
302 } #}}}
303
304 sub pingurl (@) { #{{{
305         return unless $config{pingurl} && %toping;
306
307         eval q{require RPC::XML::Client};
308         if ($@) {
309                 debug("RPC::XML::Client not found, not pinging");
310                 return;
311         }
312
313         foreach my $page (keys %toping) {
314                 my $title=pagetitle(basename($page));
315                 my $url="$config{url}/".htmlpage($page);
316                 foreach my $pingurl (@{$config{pingurl}}) {
317                         my $client = RPC::XML::Client->new($pingurl);
318                         my $req = RPC::XML::request->new('weblogUpdates.ping',
319                                 $title, $url);
320                         debug("Pinging $pingurl for $page");
321                         my $res = $client->send_request($req);
322                         if (! ref $res) {
323                                 debug("Did not receive response to ping");
324                         }
325                         my $r=$res->value;
326                         if (! exists $r->{flerror} || $r->{flerror}) {
327                                 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
328                         }
329                 }
330         }
331 } #}}}
332
333 1