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;
29 return (defined $val && lc($val) eq "yes");
32 sub preprocess_inline (@) { #{{{
35 # Avoid nested inlines, to avoid loops etc.
36 return "" if grep { $_ eq $params{page} } @processing_inline;
37 push @processing_inline, $params{page};
39 if (! exists $params{pages}) {
42 my $raw=yesno($params{raw});
43 my $archive=yesno($params{archive});
44 my $rss=exists $params{rss} ? yesno($params{rss}) : 1;
45 if (! exists $params{show} && ! $archive) {
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} && $rss) {
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 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
87 foreach my $page (@list) {
89 # Get the content before populating the template,
90 # since getting the content uses the same template
91 # if inlines are nested.
92 # TODO: if $archive=1, the only reason to do this
93 # is to let the meta plugin get page title info; so stop
94 # calling this next line then once the meta plugin can
95 # store that accross runs (also tags plugin).
96 my $content=get_inline_content($page, $params{page});
97 # Don't use htmllink because this way the title is separate
98 # and can be overridden by other plugins.
99 my $link=htmlpage(bestlink($params{page}, $page));
100 $link=abs2rel($link, dirname($params{page}));
101 $template->param(pageurl => $link);
102 $template->param(title => pagetitle(basename($page)));
103 $template->param(content => $content);
104 $template->param(ctime => displaytime($pagectime{$page}));
106 run_hooks(pagetemplate => sub {
107 shift->(page => $page, destpage => $params{page},
108 template => $template,);
111 $ret.=$template->output;
112 $template->clear_params;
115 my $file=$pagesources{$page};
116 my $type=pagetype($file);
119 preprocess($page, $params{page},
120 linkify($page, $params{page},
122 readfile(srcfile($file)))));
127 # TODO: should really add this to renderedfiles and call
128 # check_overwrite, but currently renderedfiles
129 # only supports listing one file per page.
130 if ($config{rss} && $rss) {
131 writefile(rsspage($params{page}), $config{destdir},
132 genrss($params{page}, @list));
133 $toping{$params{page}}=1 unless $config{rebuild};
136 pop @processing_inline;
140 sub get_inline_content ($$) { #{{{
144 my $file=$pagesources{$page};
145 my $type=pagetype($file);
147 return htmlize($type,
148 preprocess($page, $destpage,
149 linkify($page, $destpage,
151 readfile(srcfile($file))))));
158 sub date_822 ($) { #{{{
162 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
163 POSIX::setlocale(&POSIX::LC_TIME, "C");
164 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
165 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
169 sub absolute_urls ($$) { #{{{
170 # sucky sub because rss sucks
176 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
177 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
181 sub rsspage ($) { #{{{
187 sub genrss ($@) { #{{{
191 my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
193 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
195 foreach my $p (@pages) {
196 next unless exists $renderedfiles{$p};
198 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
200 $itemtemplate->param(
201 title => pagetitle(basename($p)),
204 pubdate => date_822($pagectime{$p}),
205 content => absolute_urls(get_inline_content($p, $page), $url),
207 run_hooks(pagetemplate => sub {
208 shift->(page => $p, destpage => $page,
209 template => $itemtemplate);
212 $content.=$itemtemplate->output;
213 $itemtemplate->clear_params;
216 my $template=template("rsspage.tmpl", blind_cache => 1);
218 title => $config{wikiname},
219 wikiname => $config{wikiname},
223 run_hooks(pagetemplate => sub {
224 shift->(page => $page, destpage => $page,
225 template => $template);
228 return $template->output;
231 sub pingurl (@) { #{{{
232 return unless $config{pingurl} && %toping;
234 eval q{require RPC::XML::Client};
236 debug("RPC::XML::Client not found, not pinging");
240 foreach my $page (keys %toping) {
241 my $title=pagetitle(basename($page));
242 my $url="$config{url}/".htmlpage($page);
243 foreach my $pingurl (@{$config{pingurl}}) {
244 my $client = RPC::XML::Client->new($pingurl);
245 my $req = RPC::XML::request->new('weblogUpdates.ping',
247 debug("Pinging $pingurl for $page");
248 my $res = $client->send_request($req);
250 debug("Did not receive response to ping");
253 if (! exists $r->{flerror} || $r->{flerror}) {
254 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));