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.
28 return (defined $val && lc($val) eq "yes");
31 sub preprocess_inline (@) { #{{{
34 if (! exists $params{pages}) {
37 my $raw=yesno($params{raw});
38 my $archive=yesno($params{archive});
39 my $rss=exists $params{rss} ? yesno($params{rss}) : 1;
40 if (! exists $params{show} && ! $archive) {
44 if (exists $params{description}) {
45 $desc = $params{description}
47 $desc = $config{wikiname};
49 my $actions=yesno($params{actions});
52 foreach my $page (keys %pagesources) {
53 next if $page eq $params{page};
54 if (pagespec_match($page, $params{pages})) {
58 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
59 if ($params{show} && @list > $params{show}) {
60 @list=@list[0..$params{show} - 1];
63 add_depends($params{page}, $params{pages});
67 if (exists $params{rootpage} && $config{cgiurl}) {
68 # Add a blog post form, with a rss link button.
69 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
70 $formtemplate->param(cgiurl => $config{cgiurl});
71 $formtemplate->param(rootpage => $params{rootpage});
73 $formtemplate->param(rssurl => rsspage(basename($params{page})));
75 $ret.=$formtemplate->output;
77 elsif ($config{rss} && $rss) {
78 # Add a rss link button.
79 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
80 $linktemplate->param(rssurl => rsspage(basename($params{page})));
81 $ret.=$linktemplate->output;
84 my $template=template(
85 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
89 foreach my $page (@list) {
91 # Get the content before populating the template,
92 # since getting the content uses the same template
93 # if inlines are nested.
94 # TODO: if $archive=1, the only reason to do this
95 # is to let the meta plugin get page title info; so stop
96 # calling this next line then once the meta plugin can
97 # store that accross runs (also tags plugin).
98 my $content=get_inline_content($page, $params{page});
99 # Don't use htmllink because this way the title is separate
100 # and can be overridden by other plugins.
101 my $link=htmlpage(bestlink($params{page}, $page));
102 $link=abs2rel($link, dirname($params{page}));
103 $template->param(pageurl => $link);
104 $template->param(title => pagetitle(basename($page)));
105 $template->param(content => $content);
106 $template->param(ctime => displaytime($pagectime{$page}));
109 my $file = $pagesources{$page};
110 my $type = pagetype($file);
111 $template->param(have_actions => 1);
112 if ($config{discussion}) {
113 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
115 if (length $config{cgiurl} && defined $type) {
116 $template->param(editurl => cgiurl(do => "edit", page => $page));
120 run_hooks(pagetemplate => sub {
121 shift->(page => $page, destpage => $params{page},
122 template => $template,);
125 $ret.=$template->output;
126 $template->clear_params;
129 my $file=$pagesources{$page};
130 my $type=pagetype($file);
133 linkify($page, $params{page},
134 preprocess($page, $params{page},
136 readfile(srcfile($file)))));
141 # TODO: should really add this to renderedfiles and call
142 # check_overwrite, but currently renderedfiles
143 # only supports listing one file per page.
144 if ($config{rss} && $rss) {
145 writefile(rsspage($params{page}), $config{destdir},
146 genrss($desc, $params{page}, @list));
147 $toping{$params{page}}=1 unless $config{rebuild};
153 sub get_inline_content ($$) { #{{{
157 my $file=$pagesources{$page};
158 my $type=pagetype($file);
160 return htmlize($page, $type,
161 linkify($page, $destpage,
162 preprocess($page, $destpage,
164 readfile(srcfile($file))))));
171 sub date_822 ($) { #{{{
175 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
176 POSIX::setlocale(&POSIX::LC_TIME, "C");
177 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
178 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
182 sub absolute_urls ($$) { #{{{
183 # sucky sub because rss sucks
189 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
190 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
194 sub rsspage ($) { #{{{
200 sub genrss ($$@) { #{{{
205 my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
207 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
209 foreach my $p (@pages) {
210 next unless exists $renderedfiles{$p};
212 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
214 $itemtemplate->param(
215 title => pagetitle(basename($p)),
218 pubdate => date_822($pagectime{$p}),
219 content => absolute_urls(get_inline_content($p, $page), $url),
221 run_hooks(pagetemplate => sub {
222 shift->(page => $p, destpage => $page,
223 template => $itemtemplate);
226 $content.=$itemtemplate->output;
227 $itemtemplate->clear_params;
230 my $template=template("rsspage.tmpl", blind_cache => 1);
232 title => $config{wikiname},
233 wikiname => $config{wikiname},
238 run_hooks(pagetemplate => sub {
239 shift->(page => $page, destpage => $page,
240 template => $template);
243 return $template->output;
246 sub pingurl (@) { #{{{
247 return unless $config{pingurl} && %toping;
249 eval q{require RPC::XML::Client};
251 debug("RPC::XML::Client not found, not pinging");
255 foreach my $page (keys %toping) {
256 my $title=pagetitle(basename($page));
257 my $url="$config{url}/".htmlpage($page);
258 foreach my $pingurl (@{$config{pingurl}}) {
259 my $client = RPC::XML::Client->new($pingurl);
260 my $req = RPC::XML::request->new('weblogUpdates.ping',
262 debug("Pinging $pingurl for $page");
263 my $res = $client->send_request($req);
265 debug("Did not receive response to ping");
268 if (! exists $r->{flerror} || $r->{flerror}) {
269 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));