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