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