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}) {
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 if (exists $hooks{pagetemplate}) {
92 foreach my $id (keys %{$hooks{pagetemplate}}) {
93 $hooks{pagetemplate}{$id}{call}->(
95 destpage => $params{page},
96 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 $template=template("rsspage.tmpl", blind_cache => 1,
165 die_on_bad_params => 0);
168 foreach my $p (@pages) {
170 itemtitle => pagetitle(basename($p)),
171 itemurl => "$config{url}/$renderedfiles{$p}",
172 itempubdate => date_822($pagectime{$p}),
173 itemcontent => absolute_urls(get_inline_content($p, $page), $url),
174 page => $p, # used by category adding code in tag plugin
175 } if exists $renderedfiles{$p};
179 title => $config{wikiname},
184 foreach my $id (keys %{$hooks{pagetemplate}}) {
185 $hooks{pagetemplate}{$id}{call}->(
188 template => $template,
192 return $template->output;
195 sub pingurl (@) { #{{{
196 return unless $config{pingurl} && %toping;
198 eval q{require RPC::XML::Client};
200 debug("RPC::XML::Client not found, not pinging");
204 foreach my $page (keys %toping) {
205 my $title=pagetitle(basename($page));
206 my $url="$config{url}/".htmlpage($page);
207 foreach my $pingurl (@{$config{pingurl}}) {
208 my $client = RPC::XML::Client->new($pingurl);
209 my $req = RPC::XML::request->new('weblogUpdates.ping',
211 debug("Pinging $pingurl for $page");
212 my $res = $client->send_request($req);
214 debug("Did not receive response to ping");
217 if (! exists $r->{flerror} || $r->{flerror}) {
218 debug("Ping rejected: ".$r->{message});