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