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