2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
11 IkiWiki::hook(type => "preprocess", id => "inline",
12 call => \&IkiWiki::preprocess_inline);
13 # Hook to change to do pinging since it's called late.
14 # This ensures each page only pings once and prevents slow
15 # pings interrupting page builds.
16 IkiWiki::hook(type => "change", id => "inline",
17 call => \&IkiWiki::pingurl);
20 # Back to ikiwiki namespace for the rest, this code is very much
21 # internal to ikiwiki even though it's separated into a plugin.
25 my $processing_inline=0;
27 sub preprocess_inline (@) { #{{{
30 if (! exists $params{pages}) {
33 if (! exists $params{archive}) {
34 $params{archive}="no";
36 if (! exists $params{show} && $params{archive} eq "no") {
39 if (! exists $params{rss}) {
43 # Avoid nested inlines, to avoid loops etc.
44 if ($processing_inline) {
50 foreach my $page (keys %pagesources) {
51 next if $page eq $params{page};
52 if (pagespec_match($page, $params{pages})) {
56 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
57 if ($params{show} && @list > $params{show}) {
58 @list=@list[0..$params{show} - 1];
61 add_depends($params{page}, $params{pages});
65 if (exists $params{rootpage} && $config{cgiurl}) {
66 # Add a blog post form, with a rss link button.
67 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
68 $formtemplate->param(cgiurl => $config{cgiurl});
69 $formtemplate->param(rootpage => $params{rootpage});
71 $formtemplate->param(rssurl => rsspage(basename($params{page})));
73 $ret.=$formtemplate->output;
75 elsif ($config{rss} && $params{rss} eq "yes") {
76 # Add a rss link button.
77 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
78 $linktemplate->param(rssurl => rsspage(basename($params{page})));
79 $ret.=$linktemplate->output;
82 my $template=template(
83 (($params{archive} eq "no")
85 : "inlinepagetitle.tmpl"),
89 foreach my $page (@list) {
90 # Don't use htmllink because this way the title is separate
91 # and can be overridden by other plugins.
92 my $link=htmlpage(bestlink($params{page}, $page));
93 $link=abs2rel($link, dirname($params{page}));
94 $template->param(pageurl => $link);
95 $template->param(title => pagetitle(basename($page)));
96 # TODO: if $params{archive} eq "no", the only reason to do this
97 # is to let the meta plugin get page title info; so stop
98 # calling this next line then once the meta plugin can
99 # store that accross runs (also tags plugin).
100 $template->param(content => get_inline_content($page, $params{page}));
101 $template->param(ctime => displaytime($pagectime{$page}));
103 run_hooks(pagetemplate => sub {
104 shift->(page => $page, destpage => $params{page},
105 template => $template,);
108 $ret.=$template->output;
109 $template->clear_params;
112 # TODO: should really add this to renderedfiles and call
113 # check_overwrite, but currently renderedfiles
114 # only supports listing one file per page.
115 if ($config{rss} && $params{rss} eq "yes") {
116 writefile(rsspage($params{page}), $config{destdir},
117 genrss($params{page}, @list));
118 $toping{$params{page}}=1 unless $config{rebuild};
121 $processing_inline=0;
126 sub get_inline_content ($$) { #{{{
130 my $file=$pagesources{$page};
131 my $type=pagetype($file);
133 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
140 sub date_822 ($) { #{{{
144 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
145 POSIX::setlocale(&POSIX::LC_TIME, "C");
146 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
147 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
151 sub absolute_urls ($$) { #{{{
152 # sucky sub because rss sucks
158 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
159 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
163 sub rsspage ($) { #{{{
169 sub genrss ($@) { #{{{
173 my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
175 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
177 foreach my $p (@pages) {
178 next unless exists $renderedfiles{$p};
180 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
182 $itemtemplate->param(
183 title => pagetitle(basename($p)),
186 pubdate => date_822($pagectime{$p}),
187 content => absolute_urls(get_inline_content($p, $page), $url),
189 run_hooks(pagetemplate => sub {
190 shift->(page => $p, destpage => $page,
191 template => $itemtemplate);
194 $content.=$itemtemplate->output;
195 $itemtemplate->clear_params;
198 my $template=template("rsspage.tmpl", blind_cache => 1);
200 title => $config{wikiname},
201 wikiname => $config{wikiname},
205 run_hooks(pagetemplate => sub {
206 shift->(page => $page, destpage => $page,
207 template => $template);
210 return $template->output;
213 sub pingurl (@) { #{{{
214 return unless $config{pingurl} && %toping;
216 eval q{require RPC::XML::Client};
218 debug("RPC::XML::Client not found, not pinging");
222 foreach my $page (keys %toping) {
223 my $title=pagetitle(basename($page));
224 my $url="$config{url}/".htmlpage($page);
225 foreach my $pingurl (@{$config{pingurl}}) {
226 my $client = RPC::XML::Client->new($pingurl);
227 my $req = RPC::XML::request->new('weblogUpdates.ping',
229 debug("Pinging $pingurl for $page");
230 my $res = $client->send_request($req);
232 debug("Did not receive response to ping");
235 if (! exists $r->{flerror} || $r->{flerror}) {
236 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));