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) {
45 foreach my $page (keys %pagesources) {
46 next if $page eq $params{page};
47 if (pagespec_match($page, $params{pages})) {
51 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
52 if ($params{show} && @list > $params{show}) {
53 @list=@list[0..$params{show} - 1];
56 add_depends($params{page}, $params{pages});
60 if (exists $params{rootpage} && $config{cgiurl}) {
61 # Add a blog post form, with a rss link button.
62 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
63 $formtemplate->param(cgiurl => $config{cgiurl});
64 $formtemplate->param(rootpage => $params{rootpage});
66 $formtemplate->param(rssurl => rsspage(basename($params{page})));
68 $ret.=$formtemplate->output;
70 elsif ($config{rss} && $rss) {
71 # Add a rss link button.
72 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
73 $linktemplate->param(rssurl => rsspage(basename($params{page})));
74 $ret.=$linktemplate->output;
77 my $template=template(
78 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
82 foreach my $page (@list) {
84 # Get the content before populating the template,
85 # since getting the content uses the same template
86 # if inlines are nested.
87 # TODO: if $archive=1, the only reason to do this
88 # is to let the meta plugin get page title info; so stop
89 # calling this next line then once the meta plugin can
90 # store that accross runs (also tags plugin).
91 my $content=get_inline_content($page, $params{page});
92 # Don't use htmllink because this way the title is separate
93 # and can be overridden by other plugins.
94 my $link=htmlpage(bestlink($params{page}, $page));
95 $link=abs2rel($link, dirname($params{page}));
96 $template->param(pageurl => $link);
97 $template->param(title => pagetitle(basename($page)));
98 $template->param(content => $content);
99 $template->param(ctime => displaytime($pagectime{$page}));
101 run_hooks(pagetemplate => sub {
102 shift->(page => $page, destpage => $params{page},
103 template => $template,);
106 $ret.=$template->output;
107 $template->clear_params;
110 my $file=$pagesources{$page};
111 my $type=pagetype($file);
114 linkify($page, $params{page},
115 preprocess($page, $params{page},
117 readfile(srcfile($file)))));
122 # TODO: should really add this to renderedfiles and call
123 # check_overwrite, but currently renderedfiles
124 # only supports listing one file per page.
125 if ($config{rss} && $rss) {
126 writefile(rsspage($params{page}), $config{destdir},
127 genrss($params{page}, @list));
128 $toping{$params{page}}=1 unless $config{rebuild};
134 sub get_inline_content ($$) { #{{{
138 my $file=$pagesources{$page};
139 my $type=pagetype($file);
141 return htmlize($type,
142 linkify($page, $destpage,
143 preprocess($page, $destpage,
145 readfile(srcfile($file))))));
152 sub date_822 ($) { #{{{
156 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
157 POSIX::setlocale(&POSIX::LC_TIME, "C");
158 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
159 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
163 sub absolute_urls ($$) { #{{{
164 # sucky sub because rss sucks
170 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
171 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
175 sub rsspage ($) { #{{{
181 sub genrss ($@) { #{{{
185 my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
187 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
189 foreach my $p (@pages) {
190 next unless exists $renderedfiles{$p};
192 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
194 $itemtemplate->param(
195 title => pagetitle(basename($p)),
198 pubdate => date_822($pagectime{$p}),
199 content => absolute_urls(get_inline_content($p, $page), $url),
201 run_hooks(pagetemplate => sub {
202 shift->(page => $p, destpage => $page,
203 template => $itemtemplate);
206 $content.=$itemtemplate->output;
207 $itemtemplate->clear_params;
210 my $template=template("rsspage.tmpl", blind_cache => 1);
212 title => $config{wikiname},
213 wikiname => $config{wikiname},
217 run_hooks(pagetemplate => sub {
218 shift->(page => $page, destpage => $page,
219 template => $template);
222 return $template->output;
225 sub pingurl (@) { #{{{
226 return unless $config{pingurl} && %toping;
228 eval q{require RPC::XML::Client};
230 debug("RPC::XML::Client not found, not pinging");
234 foreach my $page (keys %toping) {
235 my $title=pagetitle(basename($page));
236 my $url="$config{url}/".htmlpage($page);
237 foreach my $pingurl (@{$config{pingurl}}) {
238 my $client = RPC::XML::Client->new($pingurl);
239 my $req = RPC::XML::request->new('weblogUpdates.ping',
241 debug("Pinging $pingurl for $page");
242 my $res = $client->send_request($req);
244 debug("Did not receive response to ping");
247 if (! exists $r->{flerror} || $r->{flerror}) {
248 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));