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