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