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($params{page}, $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}->($page, $template);
90 $ret.=$template->output;
91 $template->clear_params;
94 # TODO: should really add this to renderedfiles and call
95 # check_overwrite, but currently renderedfiles
96 # only supports listing one file per page.
98 writefile(rsspage($params{page}), $config{destdir},
99 genrss($params{page}, @list));
100 $toping{$params{page}}=1 unless $config{rebuild};
106 sub get_inline_content ($$) { #{{{
107 my $parentpage=shift;
110 my $file=$pagesources{$page};
111 my $type=pagetype($file);
113 return htmlize($type, preprocess($page, linkify($page, $parentpage, readfile(srcfile($file))), 1));
120 sub date_822 ($) { #{{{
124 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
127 sub absolute_urls ($$) { #{{{
128 # sucky sub because rss sucks
134 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
135 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
139 sub rsspage ($) { #{{{
145 sub genrss ($@) { #{{{
149 my $url="$config{url}/".htmlpage($page);
151 my $template=template("rsspage.tmpl", blind_cache => 1);
154 foreach my $p (@pages) {
156 itemtitle => pagetitle(basename($p)),
157 itemurl => "$config{url}/$renderedfiles{$p}",
158 itempubdate => date_822($pagectime{$p}),
159 itemcontent => absolute_urls(get_inline_content($page, $p), $url),
160 } if exists $renderedfiles{$p};
164 title => $config{wikiname},
169 return $template->output;
172 sub pingurl (@) { #{{{
173 return unless $config{pingurl} && %toping;
175 eval q{require RPC::XML::Client};
177 debug("RPC::XML::Client not found, not pinging");
181 foreach my $page (keys %toping) {
182 my $title=pagetitle(basename($page));
183 my $url="$config{url}/".htmlpage($page);
184 foreach my $pingurl (@{$config{pingurl}}) {
185 my $client = RPC::XML::Client->new($pingurl);
186 my $req = RPC::XML::request->new('weblogUpdates.ping',
188 debug("Pinging $pingurl for $page");
189 my $res = $client->send_request($req);
191 debug("Did not receive response to ping");
194 if (! exists $r->{flerror} || $r->{flerror}) {
195 debug("Ping rejected: ".$r->{message});