2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
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);
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.
24 my $processing_inline=0;
26 sub preprocess_inline (@) { #{{{
29 if (! exists $params{pages}) {
32 if (! exists $params{archive}) {
33 $params{archive}="no";
35 if (! exists $params{show} && $params{archive} eq "no") {
38 if (! exists $params{rss}) {
42 # Avoid nested inlines, to avoid loops etc.
43 if ($processing_inline) {
49 foreach my $page (keys %pagesources) {
50 next if $page eq $params{page};
51 if (pagespec_match($page, $params{pages})) {
55 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
56 if ($params{show} && @list > $params{show}) {
57 @list=@list[0..$params{show} - 1];
60 add_depends($params{page}, $params{pages});
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});
70 $formtemplate->param(rssurl => rsspage(basename($params{page})));
72 $ret.=$formtemplate->output;
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;
81 my $template=template(
82 (($params{archive} eq "no")
84 : "inlinepagetitle.tmpl"),
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}));
99 run_hooks(pagetemplate => sub {
100 shift->(page => $page, destpage => $params{page},
101 template => $template,);
104 $ret.=$template->output;
105 $template->clear_params;
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};
117 $processing_inline=0;
122 sub get_inline_content ($$) { #{{{
126 my $file=$pagesources{$page};
127 my $type=pagetype($file);
129 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
136 sub date_822 ($) { #{{{
140 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
143 sub absolute_urls ($$) { #{{{
144 # sucky sub because rss sucks
150 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
151 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
155 sub rsspage ($) { #{{{
161 sub genrss ($@) { #{{{
165 my $url="$config{url}/".htmlpage($page);
167 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1,
168 die_on_bad_params => 0);
170 foreach my $p (@pages) {
171 next unless exists $renderedfiles{$p};
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),
179 run_hooks(pagetemplate => sub {
180 shift->(page => $p, destpage => $page,
181 template => $itemtemplate);
183 $content.=$itemtemplate->output;
184 $itemtemplate->clear_params;
187 my $template=template("rsspage.tmpl", blind_cache => 1);
189 title => $config{wikiname},
190 wikiname => $config{wikiname},
195 run_hooks(pagetemplate => sub {
196 shift->(page => $page, destpage => $page,
197 template => $template);
200 return $template->output;
203 sub pingurl (@) { #{{{
204 return unless $config{pingurl} && %toping;
206 eval q{require RPC::XML::Client};
208 debug("RPC::XML::Client not found, not pinging");
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',
219 debug("Pinging $pingurl for $page");
220 my $res = $client->send_request($req);
222 debug("Did not receive response to ping");
225 if (! exists $r->{flerror} || $r->{flerror}) {
226 debug("Ping rejected: ".$r->{message});