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}));
83 $ret.=$template->output;
86 # TODO: should really add this to renderedfiles and call
87 # check_overwrite, but currently renderedfiles
88 # only supports listing one file per page.
90 writefile(rsspage($params{page}), $config{destdir},
91 genrss($params{page}, @list));
92 $toping{$params{page}}=1;
98 sub get_inline_content ($$) { #{{{
102 my $file=$pagesources{$page};
103 my $type=pagetype($file);
104 if ($type ne 'unknown') {
105 return htmlize($type, preprocess($page, linkify($page, $parentpage, readfile(srcfile($file))), 1));
112 sub date_822 ($) { #{{{
116 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
119 sub absolute_urls ($$) { #{{{
120 # sucky sub because rss sucks
126 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
127 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
131 sub rsspage ($) { #{{{
137 sub genrss ($@) { #{{{
141 my $url="$config{url}/".htmlpage($page);
143 my $template=template("rsspage.tmpl", blind_cache => 1);
146 foreach my $p (@pages) {
148 itemtitle => pagetitle(basename($p)),
149 itemurl => "$config{url}/$renderedfiles{$p}",
150 itempubdate => date_822($pagectime{$p}),
151 itemcontent => absolute_urls(get_inline_content($page, $p), $url),
152 } if exists $renderedfiles{$p};
156 title => $config{wikiname},
161 return $template->output;
164 sub pingurl (@) { #{{{
165 return unless $config{pingurl} && %toping;
167 eval q{require RPC::XML::Client};
169 debug("RPC::XML::Client not found, not pinging");
173 foreach my $page (keys %toping) {
174 my $title=pagetitle(basename($page));
175 my $url="$config{url}/".htmlpage($page);
176 foreach my $pingurl (@{$config{pingurl}}) {
177 my $client = RPC::XML::Client->new($pingurl);
178 my $req = RPC::XML::request->new('weblogUpdates.ping',
180 debug("Pinging $pingurl for $page");
181 my $res = $client->send_request($req);
183 debug("Did not receive response to ping");
186 if (! exists $r->{flerror} || $r->{flerror}) {
187 debug("Ping rejected: ".$r->{message});