2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
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);
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.
25 my @processing_inline;
29 return (defined $val && lc($val) eq "yes");
32 sub preprocess_inline (@) { #{{{
35 # Avoid nested inlines, to avoid loops etc.
36 return "" if grep { $_ eq $params{page} } @processing_inline;
37 push @processing_inline, $params{page};
39 if (! exists $params{pages}) {
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) {
50 foreach my $page (keys %pagesources) {
51 next if $page eq $params{page};
52 if (pagespec_match($page, $params{pages})) {
56 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
57 if ($params{show} && @list > $params{show}) {
58 @list=@list[0..$params{show} - 1];
61 add_depends($params{page}, $params{pages});
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});
71 $formtemplate->param(rssurl => rsspage(basename($params{page})));
73 $ret.=$formtemplate->output;
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;
82 my $template=template(
83 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
87 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 # 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}));
102 run_hooks(pagetemplate => sub {
103 shift->(page => $page, destpage => $params{page},
104 template => $template,);
107 $ret.=$template->output;
108 $template->clear_params;
111 my $file=$pagesources{$page};
112 my $type=pagetype($file);
115 preprocess($page, $params{page},
116 linkify($page, $params{page},
118 readfile(srcfile($file)))));
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};
132 pop @processing_inline;
136 sub get_inline_content ($$) { #{{{
140 my $file=$pagesources{$page};
141 my $type=pagetype($file);
143 return htmlize($type,
144 preprocess($page, $destpage,
145 linkify($page, $destpage,
147 readfile(srcfile($file))))));
154 sub date_822 ($) { #{{{
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);
165 sub absolute_urls ($$) { #{{{
166 # sucky sub because rss sucks
172 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
173 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
177 sub rsspage ($) { #{{{
183 sub genrss ($@) { #{{{
187 my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
189 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
191 foreach my $p (@pages) {
192 next unless exists $renderedfiles{$p};
194 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
196 $itemtemplate->param(
197 title => pagetitle(basename($p)),
200 pubdate => date_822($pagectime{$p}),
201 content => absolute_urls(get_inline_content($p, $page), $url),
203 run_hooks(pagetemplate => sub {
204 shift->(page => $p, destpage => $page,
205 template => $itemtemplate);
208 $content.=$itemtemplate->output;
209 $itemtemplate->clear_params;
212 my $template=template("rsspage.tmpl", blind_cache => 1);
214 title => $config{wikiname},
215 wikiname => $config{wikiname},
219 run_hooks(pagetemplate => sub {
220 shift->(page => $page, destpage => $page,
221 template => $template);
224 return $template->output;
227 sub pingurl (@) { #{{{
228 return unless $config{pingurl} && %toping;
230 eval q{require RPC::XML::Client};
232 debug("RPC::XML::Client not found, not pinging");
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',
243 debug("Pinging $pingurl for $page");
244 my $res = $client->send_request($req);
246 debug("Did not receive response to ping");
249 if (! exists $r->{flerror} || $r->{flerror}) {
250 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));