]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
* Patch from James Westby to add an actions option to inline; this
[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{page});
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{page}));
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                                 $template->param(have_actions => 1);
112                                 if ($config{discussion}) {
113                                         $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
114                                 }
115                                 if (length $config{cgiurl} && defined $type) {
116                                         $template->param(editurl => cgiurl(do => "edit", page => $page));
117                                 }
118                         }
119
120                         run_hooks(pagetemplate => sub {
121                                 shift->(page => $page, destpage => $params{page},
122                                         template => $template,);
123                         });
124
125                         $ret.=$template->output;
126                         $template->clear_params;
127                 }
128                 else {
129                         my $file=$pagesources{$page};
130                         my $type=pagetype($file);
131                         if (defined $type) {
132                                 $ret.="\n".
133                                       linkify($page, $params{page},
134                                       preprocess($page, $params{page},
135                                       filter($page,
136                                       readfile(srcfile($file)))));
137                         }
138                 }
139         }
140         
141         # TODO: should really add this to renderedfiles and call
142         # check_overwrite, but currently renderedfiles
143         # only supports listing one file per page.
144         if ($config{rss} && $rss) {
145                 writefile(rsspage($params{page}), $config{destdir},
146                         genrss($desc, $params{page}, @list));
147                 $toping{$params{page}}=1 unless $config{rebuild};
148         }
149         
150         return $ret;
151 } #}}}
152
153 sub get_inline_content ($$) { #{{{
154         my $page=shift;
155         my $destpage=shift;
156         
157         my $file=$pagesources{$page};
158         my $type=pagetype($file);
159         if (defined $type) {
160                 return htmlize($page, $type,
161                        linkify($page, $destpage,
162                        preprocess($page, $destpage,
163                        filter($page,
164                        readfile(srcfile($file))))));
165         }
166         else {
167                 return "";
168         }
169 } #}}}
170
171 sub date_822 ($) { #{{{
172         my $time=shift;
173
174         eval q{use POSIX};
175         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
176         POSIX::setlocale(&POSIX::LC_TIME, "C");
177         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
178         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
179         return $ret;
180 } #}}}
181
182 sub absolute_urls ($$) { #{{{
183         # sucky sub because rss sucks
184         my $content=shift;
185         my $url=shift;
186
187         $url=~s/[^\/]+$//;
188         
189         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
190         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
191         return $content;
192 } #}}}
193
194 sub rsspage ($) { #{{{
195         my $page=shift;
196
197         return $page.".rss";
198 } #}}}
199
200 sub genrss ($$@) { #{{{
201         my $desc = shift;
202         my $page=shift;
203         my @pages=@_;
204         
205         my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
206         
207         my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
208         my $content="";
209         foreach my $p (@pages) {
210                 next unless exists $renderedfiles{$p};
211
212                 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
213
214                 $itemtemplate->param(
215                         title => pagetitle(basename($p)),
216                         url => $u,
217                         permalink => $u,
218                         pubdate => date_822($pagectime{$p}),
219                         content => absolute_urls(get_inline_content($p, $page), $url),
220                 );
221                 run_hooks(pagetemplate => sub {
222                         shift->(page => $p, destpage => $page,
223                                 template => $itemtemplate);
224                 });
225
226                 $content.=$itemtemplate->output;
227                 $itemtemplate->clear_params;
228         }
229
230         my $template=template("rsspage.tmpl", blind_cache => 1);
231         $template->param(
232                 title => $config{wikiname},
233                 wikiname => $config{wikiname},
234                 pageurl => $url,
235                 content => $content,
236                 rssdesc => $desc,
237         );
238         run_hooks(pagetemplate => sub {
239                 shift->(page => $page, destpage => $page,
240                         template => $template);
241         });
242         
243         return $template->output;
244 } #}}}
245
246 sub pingurl (@) { #{{{
247         return unless $config{pingurl} && %toping;
248
249         eval q{require RPC::XML::Client};
250         if ($@) {
251                 debug("RPC::XML::Client not found, not pinging");
252                 return;
253         }
254
255         foreach my $page (keys %toping) {
256                 my $title=pagetitle(basename($page));
257                 my $url="$config{url}/".htmlpage($page);
258                 foreach my $pingurl (@{$config{pingurl}}) {
259                         my $client = RPC::XML::Client->new($pingurl);
260                         my $req = RPC::XML::request->new('weblogUpdates.ping',
261                                 $title, $url);
262                         debug("Pinging $pingurl for $page");
263                         my $res = $client->send_request($req);
264                         if (! ref $res) {
265                                 debug("Did not receive response to ping");
266                         }
267                         my $r=$res->value;
268                         if (! exists $r->{flerror} || $r->{flerror}) {
269                                 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
270                         }
271                 }
272         }
273 } #}}}
274
275 1