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") {
39 # Avoid nested inlines, to avoid loops etc.
40 if ($processing_inline) {
46 foreach my $page (keys %pagesources) {
47 next if $page eq $params{page};
48 if (globlist_match($page, $params{pages})) {
52 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
53 if ($params{show} && @list > $params{show}) {
54 @list=@list[0..$params{show} - 1];
57 add_depends($params{page}, $params{pages});
61 if (exists $params{rootpage} && $config{cgiurl}) {
62 # Add a blog post form, with a rss link button.
63 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
64 $formtemplate->param(cgiurl => $config{cgiurl});
65 $formtemplate->param(rootpage => $params{rootpage});
67 $formtemplate->param(rssurl => rsspage(basename($params{page})));
69 $ret.=$formtemplate->output;
71 elsif ($config{rss}) {
72 # Add a rss link button.
73 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
74 $linktemplate->param(rssurl => rsspage(basename($params{page})));
75 $ret.=$linktemplate->output;
78 my $template=template(
79 (($params{archive} eq "no")
81 : "inlinepagetitle.tmpl"),
85 foreach my $page (@list) {
86 # Don't use htmllink because this way the title is separate
87 # and can be overridden by other plugins.
88 my $link=htmlpage(bestlink($params{page}, $page));
89 $link=abs2rel($link, dirname($params{page}));
90 $template->param(pageurl => $link);
91 $template->param(title => pagetitle(basename($page)));
92 $template->param(content => get_inline_content($page, $params{page}))
93 if $params{archive} eq "no";
94 $template->param(ctime => displaytime($pagectime{$page}));
96 run_hooks(pagetemplate => sub {
97 shift->(page => $page, destpage => $params{page},
98 template => $template,);
101 $ret.=$template->output;
102 $template->clear_params;
105 # TODO: should really add this to renderedfiles and call
106 # check_overwrite, but currently renderedfiles
107 # only supports listing one file per page.
109 writefile(rsspage($params{page}), $config{destdir},
110 genrss($params{page}, @list));
111 $toping{$params{page}}=1 unless $config{rebuild};
114 $processing_inline=0;
119 sub get_inline_content ($$) { #{{{
123 my $file=$pagesources{$page};
124 my $type=pagetype($file);
126 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
133 sub date_822 ($) { #{{{
137 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
140 sub absolute_urls ($$) { #{{{
141 # sucky sub because rss sucks
147 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
148 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
152 sub rsspage ($) { #{{{
158 sub genrss ($@) { #{{{
162 my $url="$config{url}/".htmlpage($page);
164 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1,
165 die_on_bad_params => 0);
167 foreach my $p (@pages) {
168 next unless exists $renderedfiles{$p};
170 $itemtemplate->param(
171 title => pagetitle(basename($p)),
172 url => "$config{url}/$renderedfiles{$p}",
173 pubdate => date_822($pagectime{$p}),
174 content => absolute_urls(get_inline_content($p, $page), $url),
176 run_hooks(pagetemplate => sub {
177 shift->(page => $p, destpage => $page,
178 template => $itemtemplate);
180 $content.=$itemtemplate->output;
181 $itemtemplate->clear_params;
184 my $template=template("rsspage.tmpl", blind_cache => 1);
186 title => $config{wikiname},
187 wikiname => $config{wikiname},
192 run_hooks(pagetemplate => sub {
193 shift->(page => $page, destpage => $page,
194 template => $template);
197 return $template->output;
200 sub pingurl (@) { #{{{
201 return unless $config{pingurl} && %toping;
203 eval q{require RPC::XML::Client};
205 debug("RPC::XML::Client not found, not pinging");
209 foreach my $page (keys %toping) {
210 my $title=pagetitle(basename($page));
211 my $url="$config{url}/".htmlpage($page);
212 foreach my $pingurl (@{$config{pingurl}}) {
213 my $client = RPC::XML::Client->new($pingurl);
214 my $req = RPC::XML::request->new('weblogUpdates.ping',
216 debug("Pinging $pingurl for $page");
217 my $res = $client->send_request($req);
219 debug("Did not receive response to ping");
222 if (! exists $r->{flerror} || $r->{flerror}) {
223 debug("Ping rejected: ".$r->{message});