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.
25 sub preprocess_inline (@) { #{{{
28 if (! exists $params{pages}) {
31 if (! exists $params{archive}) {
32 $params{archive}="no";
34 if (! exists $params{show} && $params{archive} eq "no") {
39 foreach my $page (keys %pagesources) {
40 next if $page eq $params{page};
41 if (globlist_match($page, $params{pages})) {
45 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
46 if ($params{show} && @list > $params{show}) {
47 @list=@list[0..$params{show} - 1];
50 add_depends($params{page}, $params{pages});
54 if (exists $params{rootpage}) {
55 # Add a blog post form, with a rss link button.
56 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
57 $formtemplate->param(cgiurl => $config{cgiurl});
58 $formtemplate->param(rootpage => $params{rootpage});
60 $formtemplate->param(rssurl => rsspage(basename($params{page})));
62 $ret.=$formtemplate->output;
64 elsif ($config{rss}) {
65 # Add a rss link button.
66 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
67 $linktemplate->param(rssurl => rsspage(basename($params{page})));
68 $ret.=$linktemplate->output;
71 my $template=template(
72 (($params{archive} eq "no")
74 : "inlinepagetitle.tmpl"),
78 foreach my $page (@list) {
79 $template->param(pagelink => htmllink($params{page}, $params{page}, $page));
80 $template->param(content => get_inline_content($page, $params{page}))
81 if $params{archive} eq "no";
82 $template->param(ctime => displaytime($pagectime{$page}));
84 if (exists $hooks{pagetemplate}) {
85 foreach my $id (keys %{$hooks{pagetemplate}}) {
86 $hooks{pagetemplate}{$id}{call}->(
88 destpage => $params{page},
89 template => $template,
94 $ret.=$template->output;
95 $template->clear_params;
98 # TODO: should really add this to renderedfiles and call
99 # check_overwrite, but currently renderedfiles
100 # only supports listing one file per page.
102 writefile(rsspage($params{page}), $config{destdir},
103 genrss($params{page}, @list));
104 $toping{$params{page}}=1 unless $config{rebuild};
110 sub get_inline_content ($$) { #{{{
114 my $file=$pagesources{$page};
115 my $type=pagetype($file);
117 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file))), 1));
124 sub date_822 ($) { #{{{
128 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
131 sub absolute_urls ($$) { #{{{
132 # sucky sub because rss sucks
138 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
139 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
143 sub rsspage ($) { #{{{
149 sub genrss ($@) { #{{{
153 my $url="$config{url}/".htmlpage($page);
155 my $template=template("rsspage.tmpl", blind_cache => 1);
158 foreach my $p (@pages) {
160 itemtitle => pagetitle(basename($p)),
161 itemurl => "$config{url}/$renderedfiles{$p}",
162 itempubdate => date_822($pagectime{$p}),
163 itemcontent => absolute_urls(get_inline_content($p, $page), $url),
164 } if exists $renderedfiles{$p};
168 title => $config{wikiname},
173 return $template->output;
176 sub pingurl (@) { #{{{
177 return unless $config{pingurl} && %toping;
179 eval q{require RPC::XML::Client};
181 debug("RPC::XML::Client not found, not pinging");
185 foreach my $page (keys %toping) {
186 my $title=pagetitle(basename($page));
187 my $url="$config{url}/".htmlpage($page);
188 foreach my $pingurl (@{$config{pingurl}}) {
189 my $client = RPC::XML::Client->new($pingurl);
190 my $req = RPC::XML::request->new('weblogUpdates.ping',
192 debug("Pinging $pingurl for $page");
193 my $res = $client->send_request($req);
195 debug("Did not receive response to ping");
198 if (! exists $r->{flerror} || $r->{flerror}) {
199 debug("Ping rejected: ".$r->{message});