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