]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
* Don't fail syntax check if Text::Typography isn't installed.
[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 IkiWiki::Render; # for displaytime
9 use URI;
10
11 sub import { #{{{
12         hook(type => "preprocess", id => "inline", 
13                 call => \&IkiWiki::preprocess_inline);
14         hook(type => "pagetemplate", id => "inline",
15                 call => \&IkiWiki::pagetemplate_inline);
16         # Hook to change to do pinging since it's called late.
17         # This ensures each page only pings once and prevents slow
18         # pings interrupting page builds.
19         hook(type => "change", id => "inline", 
20                 call => \&IkiWiki::pingurl);
21 } # }}}
22
23 # Back to ikiwiki namespace for the rest, this code is very much
24 # internal to ikiwiki even though it's separated into a plugin.
25 package IkiWiki;
26
27 my %toping;
28 my %rsslinks;
29
30 sub yesno ($) { #{{{
31         my $val=shift;
32         return (defined $val && lc($val) eq "yes");
33 } #}}}
34
35 sub preprocess_inline (@) { #{{{
36         my %params=@_;
37         
38         if (! exists $params{pages}) {
39                 return "";
40         }
41         my $raw=yesno($params{raw});
42         my $archive=yesno($params{archive});
43         my $rss=exists $params{rss} ? yesno($params{rss}) : 1;
44         if (! exists $params{show} && ! $archive) {
45                 $params{show}=10;
46         }
47         my $desc;
48         if (exists $params{description}) {
49                 $desc = $params{description} 
50         } else {
51                 $desc = $config{wikiname};
52         }
53         my $actions=yesno($params{actions});
54
55         my @list;
56         foreach my $page (keys %pagesources) {
57                 next if $page eq $params{page};
58                 if (pagespec_match($page, $params{pages})) {
59                         push @list, $page;
60                 }
61         }
62
63         if (exists $params{sort} && $params{sort} eq 'title') {
64                 @list=sort @list;
65         }
66         elsif (! exists $params{sort} || $params{sort} eq 'age') {
67                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
68         }
69         else {
70                 return "unknown sort type $params{sort}";
71         }
72
73         if ($params{show} && @list > $params{show}) {
74                 @list=@list[0..$params{show} - 1];
75         }
76
77         add_depends($params{page}, $params{pages});
78
79         my $rssurl=rsspage(basename($params{page}));
80         my $ret="";
81
82         if (exists $params{rootpage} && $config{cgiurl}) {
83                 # Add a blog post form, with a rss link button.
84                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
85                 $formtemplate->param(cgiurl => $config{cgiurl});
86                 $formtemplate->param(rootpage => $params{rootpage});
87                 if ($config{rss}) {
88                         $formtemplate->param(rssurl => $rssurl);
89                 }
90                 $ret.=$formtemplate->output;
91         }
92         elsif ($config{rss} && $rss) {
93                 # Add a rss link button.
94                 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
95                 $linktemplate->param(rssurl => $rssurl);
96                 $ret.=$linktemplate->output;
97         }
98         
99         my $template=template(
100                 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
101                 blind_cache => 1,
102         ) unless $raw;
103         
104         foreach my $page (@list) {
105                 if (! $raw) {
106                         # Get the content before populating the template,
107                         # since getting the content uses the same template
108                         # if inlines are nested.
109                         # TODO: if $archive=1, the only reason to do this
110                         # is to let the meta plugin get page title info; so stop
111                         # calling this next line then once the meta plugin can
112                         # store that accross runs (also tags plugin).
113                         my $content=get_inline_content($page, $params{destpage});
114                         # Don't use htmllink because this way the title is separate
115                         # and can be overridden by other plugins.
116                         my $link=htmlpage(bestlink($params{page}, $page));
117                         $link=abs2rel($link, dirname($params{destpage}));
118                         $template->param(pageurl => $link);
119                         $template->param(title => pagetitle(basename($page)));
120                         $template->param(content => $content);
121                         $template->param(ctime => displaytime($pagectime{$page}));
122
123                         if ($actions) {
124                                 my $file = $pagesources{$page};
125                                 my $type = pagetype($file);
126                                 if ($config{discussion}) {
127                                         $template->param(have_actions => 1);
128                                         $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
129                                 }
130                                 if (length $config{cgiurl} && defined $type) {
131                                         $template->param(have_actions => 1);
132                                         $template->param(editurl => cgiurl(do => "edit", page => $page));
133                                 }
134                         }
135
136                         run_hooks(pagetemplate => sub {
137                                 shift->(page => $page, destpage => $params{page},
138                                         template => $template,);
139                         });
140
141                         $ret.=$template->output;
142                         $template->clear_params;
143                 }
144                 else {
145                         my $file=$pagesources{$page};
146                         my $type=pagetype($file);
147                         if (defined $type) {
148                                 $ret.="\n".
149                                       linkify($page, $params{page},
150                                       preprocess($page, $params{page},
151                                       filter($page,
152                                       readfile(srcfile($file)))));
153                         }
154                 }
155         }
156         
157         # TODO: should really add this to renderedfiles and call
158         # check_overwrite, but currently renderedfiles
159         # only supports listing one file per page.
160         if ($config{rss} && $rss) {
161                 writefile(rsspage($params{page}), $config{destdir},
162                         genrss($desc, $params{page}, @list));
163                 $toping{$params{page}}=1 unless $config{rebuild};
164                 $rsslinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
165         }
166         
167         return $ret;
168 } #}}}
169
170 sub pagetemplate_inline (@) { #{{{
171         my %params=@_;
172         my $page=$params{page};
173         my $template=$params{template};
174
175         $template->param(rsslink => $rsslinks{$page})
176                 if exists $rsslinks{$page} && $template->query(name => "rsslink");
177 } #}}}
178
179 sub get_inline_content ($$) { #{{{
180         my $page=shift;
181         my $destpage=shift;
182         
183         my $file=$pagesources{$page};
184         my $type=pagetype($file);
185         if (defined $type) {
186                 return htmlize($page, $type,
187                        linkify($page, $destpage,
188                        preprocess($page, $destpage,
189                        filter($page,
190                        readfile(srcfile($file))))));
191         }
192         else {
193                 return "";
194         }
195 } #}}}
196
197 sub date_822 ($) { #{{{
198         my $time=shift;
199
200         eval q{use POSIX};
201         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
202         POSIX::setlocale(&POSIX::LC_TIME, "C");
203         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
204         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
205         return $ret;
206 } #}}}
207
208 sub absolute_urls ($$) { #{{{
209         # sucky sub because rss sucks
210         my $content=shift;
211         my $url=shift;
212
213         $url=~s/[^\/]+$//;
214         
215         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
216         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
217         return $content;
218 } #}}}
219
220 sub rsspage ($) { #{{{
221         my $page=shift;
222
223         return $page.".rss";
224 } #}}}
225
226 sub genrss ($$@) { #{{{
227         my $desc=shift;
228         my $page=shift;
229         my @pages=@_;
230         
231         my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
232         
233         my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
234         my $content="";
235         foreach my $p (@pages) {
236                 next unless exists $renderedfiles{$p};
237
238                 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
239
240                 $itemtemplate->param(
241                         title => pagetitle(basename($p)),
242                         url => $u,
243                         permalink => $u,
244                         pubdate => date_822($pagectime{$p}),
245                         content => absolute_urls(get_inline_content($p, $page), $url),
246                 );
247                 run_hooks(pagetemplate => sub {
248                         shift->(page => $p, destpage => $page,
249                                 template => $itemtemplate);
250                 });
251
252                 $content.=$itemtemplate->output;
253                 $itemtemplate->clear_params;
254         }
255
256         my $template=template("rsspage.tmpl", blind_cache => 1);
257         $template->param(
258                 title => $config{wikiname},
259                 wikiname => $config{wikiname},
260                 pageurl => $url,
261                 content => $content,
262                 rssdesc => $desc,
263         );
264         run_hooks(pagetemplate => sub {
265                 shift->(page => $page, destpage => $page,
266                         template => $template);
267         });
268         
269         return $template->output;
270 } #}}}
271
272 sub pingurl (@) { #{{{
273         return unless $config{pingurl} && %toping;
274
275         eval q{require RPC::XML::Client};
276         if ($@) {
277                 debug("RPC::XML::Client not found, not pinging");
278                 return;
279         }
280
281         foreach my $page (keys %toping) {
282                 my $title=pagetitle(basename($page));
283                 my $url="$config{url}/".htmlpage($page);
284                 foreach my $pingurl (@{$config{pingurl}}) {
285                         my $client = RPC::XML::Client->new($pingurl);
286                         my $req = RPC::XML::request->new('weblogUpdates.ping',
287                                 $title, $url);
288                         debug("Pinging $pingurl for $page");
289                         my $res = $client->send_request($req);
290                         if (! ref $res) {
291                                 debug("Did not receive response to ping");
292                         }
293                         my $r=$res->value;
294                         if (! exists $r->{flerror} || $r->{flerror}) {
295                                 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
296                         }
297                 }
298         }
299 } #}}}
300
301 1