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 $template->param(pagelink => htmllink($params{page}, $params{page}, $page));
87 $template->param(content => get_inline_content($page, $params{page}))
88 if $params{archive} eq "no";
89 $template->param(ctime => displaytime($pagectime{$page}));
91 run_hooks(pagetemplate => sub {
92 shift->(page => $page, destpage => $params{page},
93 template => $template,);
96 $ret.=$template->output;
97 $template->clear_params;
100 # TODO: should really add this to renderedfiles and call
101 # check_overwrite, but currently renderedfiles
102 # only supports listing one file per page.
104 writefile(rsspage($params{page}), $config{destdir},
105 genrss($params{page}, @list));
106 $toping{$params{page}}=1 unless $config{rebuild};
109 $processing_inline=0;
114 sub get_inline_content ($$) { #{{{
118 my $file=$pagesources{$page};
119 my $type=pagetype($file);
121 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
128 sub date_822 ($) { #{{{
132 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
135 sub absolute_urls ($$) { #{{{
136 # sucky sub because rss sucks
142 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
143 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
147 sub rsspage ($) { #{{{
153 sub genrss ($@) { #{{{
157 my $url="$config{url}/".htmlpage($page);
159 my $template=template("rsspage.tmpl", blind_cache => 1,
160 die_on_bad_params => 0);
163 foreach my $p (@pages) {
165 itemtitle => pagetitle(basename($p)),
166 itemurl => "$config{url}/$renderedfiles{$p}",
167 itempubdate => date_822($pagectime{$p}),
168 itemcontent => absolute_urls(get_inline_content($p, $page), $url),
169 page => $p, # used by category adding code in tag plugin
170 } if exists $renderedfiles{$p};
174 title => $config{wikiname},
179 run_hooks(pagetemplate => sub {
180 shift->(page => $page, destpage => $page,
181 template => $template);
184 return $template->output;
187 sub pingurl (@) { #{{{
188 return unless $config{pingurl} && %toping;
190 eval q{require RPC::XML::Client};
192 debug("RPC::XML::Client not found, not pinging");
196 foreach my $page (keys %toping) {
197 my $title=pagetitle(basename($page));
198 my $url="$config{url}/".htmlpage($page);
199 foreach my $pingurl (@{$config{pingurl}}) {
200 my $client = RPC::XML::Client->new($pingurl);
201 my $req = RPC::XML::request->new('weblogUpdates.ping',
203 debug("Pinging $pingurl for $page");
204 my $res = $client->send_request($req);
206 debug("Did not receive response to ping");
209 if (! exists $r->{flerror} || $r->{flerror}) {
210 debug("Ping rejected: ".$r->{message});