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") {
37 add_depends($params{page}, $params{pages});
41 if (exists $params{rootpage}) {
42 # Add a blog post form, with a rss link button.
43 my $formtemplate=HTML::Template->new(blind_cache => 1,
44 filename => "$config{templatedir}/blogpost.tmpl");
45 $formtemplate->param(cgiurl => $config{cgiurl});
46 $formtemplate->param(rootpage => $params{rootpage});
48 $formtemplate->param(rssurl => rsspage(basename($params{page})));
50 $ret.=$formtemplate->output;
52 elsif ($config{rss}) {
53 # Add a rss link button.
54 my $linktemplate=HTML::Template->new(blind_cache => 1,
55 filename => "$config{templatedir}/rsslink.tmpl");
56 $linktemplate->param(rssurl => rsspage(basename($params{page})));
57 $ret.=$linktemplate->output;
60 my $template=HTML::Template->new(blind_cache => 1,
61 filename => (($params{archive} eq "no")
62 ? "$config{templatedir}/inlinepage.tmpl"
63 : "$config{templatedir}/inlinepagetitle.tmpl"));
66 foreach my $page (blog_list($params{pages}, $params{show})) {
67 next if $page eq $params{page};
69 $template->param(pagelink => htmllink($params{page}, $params{page}, $page));
70 $template->param(content => get_inline_content($params{page}, $page))
71 if $params{archive} eq "no";
72 $template->param(ctime => displaytime($pagectime{$page}));
73 $ret.=$template->output;
76 # TODO: should really add this to renderedfiles and call
77 # check_overwrite, but currently renderedfiles
78 # only supports listing one file per page.
80 writefile(rsspage($params{page}), $config{destdir},
81 genrss($params{page}, @pages));
82 $toping{$params{page}}=1;
88 sub blog_list ($$) { #{{{
93 foreach my $page (keys %pagesources) {
94 if (globlist_match($page, $globlist)) {
99 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
100 return @list if ! $maxitems || @list <= $maxitems;
101 return @list[0..$maxitems - 1];
104 sub get_inline_content ($$) { #{{{
105 my $parentpage=shift;
108 my $file=$pagesources{$page};
109 my $type=pagetype($file);
110 if ($type ne 'unknown') {
111 return htmlize($type, preprocess($page, linkify($page, $parentpage, readfile(srcfile($file))), 1));
118 sub date_822 ($) { #{{{
122 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
125 sub absolute_urls ($$) { #{{{
126 # sucky sub because rss sucks
132 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
133 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
137 sub rsspage ($) { #{{{
143 sub genrss ($@) { #{{{
147 my $url="$config{url}/".htmlpage($page);
149 my $template=HTML::Template->new(blind_cache => 1,
150 filename => "$config{templatedir}/rsspage.tmpl");
153 foreach my $p (@pages) {
155 itemtitle => pagetitle(basename($p)),
156 itemurl => "$config{url}/$renderedfiles{$p}",
157 itempubdate => date_822($pagectime{$p}),
158 itemcontent => absolute_urls(get_inline_content($page, $p), $url),
159 } if exists $renderedfiles{$p};
163 title => $config{wikiname},
168 return $template->output;
171 sub pingurl (@) { #{{{
172 return unless $config{pingurl} && %toping;
174 eval q{require RPC::XML::Client};
176 debug("RPC::XML::Client not found, not pinging");
180 foreach my $page (keys %toping) {
181 my $title=pagetitle(basename($page));
182 my $url="$config{url}/".htmlpage($page);
183 foreach my $pingurl (@{$config{pingurl}}) {
184 my $client = RPC::XML::Client->new($pingurl);
185 my $req = RPC::XML::request->new('weblogUpdates.ping',
187 debug("Pinging $pingurl for $page");
188 my $res = $client->send_request($req);
190 debug("Did not receive response to ping");
193 if (! exists $r->{flerror} || $r->{flerror}) {
194 debug("Ping rejected: ".$r->{message});