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