2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
8 use IkiWiki::Render; # for displaytime
12 hook(type => "preprocess", id => "inline",
13 call => \&IkiWiki::preprocess_inline);
14 hook(type => "pagetemplate", id => "inline",
15 call => \&IkiWiki::pagetemplate_inline);
16 # Hook to change to do pinging since it's called late.
17 # This ensures each page only pings once and prevents slow
18 # pings interrupting page builds.
19 hook(type => "change", id => "inline",
20 call => \&IkiWiki::pingurl);
23 # Back to ikiwiki namespace for the rest, this code is very much
24 # internal to ikiwiki even though it's separated into a plugin.
32 return (defined $val && lc($val) eq "yes");
35 sub preprocess_inline (@) { #{{{
38 if (! exists $params{pages}) {
41 my $raw=yesno($params{raw});
42 my $archive=yesno($params{archive});
43 my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
44 my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
45 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : 1;
46 if (! exists $params{show} && ! $archive) {
50 if (exists $params{description}) {
51 $desc = $params{description}
53 $desc = $config{wikiname};
55 my $actions=yesno($params{actions});
58 foreach my $page (keys %pagesources) {
59 next if $page eq $params{page};
60 if (pagespec_match($page, $params{pages})) {
65 if (exists $params{sort} && $params{sort} eq 'title') {
68 elsif (! exists $params{sort} || $params{sort} eq 'age') {
69 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
72 return "unknown sort type $params{sort}";
75 if (exists $params{skip}) {
76 @list=@list[$params{skip} .. scalar @list - 1];
79 if ($params{show} && @list > $params{show}) {
80 @list=@list[0..$params{show} - 1];
83 add_depends($params{page}, $params{pages});
85 my $rssurl=rsspage(basename($params{page}));
86 my $atomurl=atompage(basename($params{page}));
89 if (exists $params{rootpage} && $config{cgiurl}) {
90 # Add a blog post form, with feed buttons.
91 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
92 $formtemplate->param(cgiurl => $config{cgiurl});
93 $formtemplate->param(rootpage => $params{rootpage});
94 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
95 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
96 $ret.=$formtemplate->output;
100 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
101 $linktemplate->param(rssurl => $rssurl) if $rss;
102 $linktemplate->param(atomurl => $atomurl) if $atom;
103 $ret.=$linktemplate->output;
106 my $template=template(
107 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
111 foreach my $page (@list) {
112 my $file = $pagesources{$page};
113 my $type = pagetype($file);
114 if (! $raw || ($raw && ! defined $type)) {
115 # Get the content before populating the template,
116 # since getting the content uses the same template
117 # if inlines are nested.
118 # TODO: if $archive=1, the only reason to do this
119 # is to let the meta plugin get page title info; so stop
120 # calling this next line then once the meta plugin can
121 # store that accross runs (also tags plugin).
122 my $content=get_inline_content($page, $params{destpage});
123 # Don't use htmllink because this way the title is separate
124 # and can be overridden by other plugins.
125 my $link=bestlink($params{page}, $page);
126 $link=htmlpage($link) if defined $type;
127 $link=abs2rel($link, dirname($params{destpage}));
128 $template->param(pageurl => $link);
129 $template->param(title => pagetitle(basename($page)));
130 $template->param(content => $content);
131 $template->param(ctime => displaytime($pagectime{$page}));
134 my $file = $pagesources{$page};
135 my $type = pagetype($file);
136 if ($config{discussion}) {
137 $template->param(have_actions => 1);
138 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
140 if (length $config{cgiurl} && defined $type) {
141 $template->param(have_actions => 1);
142 $template->param(editurl => cgiurl(do => "edit", page => $page));
146 run_hooks(pagetemplate => sub {
147 shift->(page => $page, destpage => $params{page},
148 template => $template,);
151 $ret.=$template->output;
152 $template->clear_params;
157 linkify($page, $params{page},
158 preprocess($page, $params{page},
160 readfile(srcfile($file)))));
165 if ($feeds && $rss) {
166 will_render($params{page}, rsspage($params{page}));
167 writefile(rsspage($params{page}), $config{destdir},
168 genfeed("rss", $rssurl, $desc, $params{page}, @list));
169 $toping{$params{page}}=1 unless $config{rebuild};
170 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
172 if ($feeds && $atom) {
173 will_render($params{page}, atompage($params{page}));
174 writefile(atompage($params{page}), $config{destdir},
175 genfeed("atom", $atomurl, $desc, $params{page}, @list));
176 $toping{$params{page}}=1 unless $config{rebuild};
177 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
183 sub pagetemplate_inline (@) { #{{{
185 my $page=$params{page};
186 my $template=$params{template};
188 $template->param(feedlinks => $feedlinks{$page})
189 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
192 sub get_inline_content ($$) { #{{{
196 my $file=$pagesources{$page};
197 my $type=pagetype($file);
199 return htmlize($page, $type,
200 linkify($page, $destpage,
201 preprocess($page, $destpage,
203 readfile(srcfile($file))))));
210 sub date_822 ($) { #{{{
215 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
216 POSIX::setlocale(&POSIX::LC_TIME, "C");
217 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
218 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
222 sub date_3339 ($) { #{{{
227 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
228 POSIX::setlocale(&POSIX::LC_TIME, "C");
229 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
230 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
234 sub absolute_urls ($$) { #{{{
235 # sucky sub because rss sucks
241 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
242 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
246 sub rsspage ($) { #{{{
252 sub atompage ($) { #{{{
255 return $page.".atom";
258 sub genfeed ($$$$@) { #{{{
265 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
267 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
270 foreach my $p (@pages) {
271 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
273 $itemtemplate->param(
274 title => pagetitle(basename($p)),
277 date_822 => date_822($pagectime{$p}),
278 date_3339 => date_3339($pagectime{$p}),
281 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
282 if ($itemtemplate->query(name => "enclosure")) {
283 my $file=$pagesources{$p};
284 my $type=pagetype($file);
286 $itemtemplate->param(content => $pcontent);
289 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
291 eval q{use File::MimeInfo};
293 $mime = mimetype($file);
295 $itemtemplate->param(
303 $itemtemplate->param(content => $pcontent);
306 run_hooks(pagetemplate => sub {
307 shift->(page => $p, destpage => $page,
308 template => $itemtemplate);
311 $content.=$itemtemplate->output;
312 $itemtemplate->clear_params;
314 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
317 my $template=template($feedtype."page.tmpl", blind_cache => 1);
319 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
320 wikiname => $config{wikiname},
323 feeddesc => $feeddesc,
324 feeddate => date_3339($lasttime),
326 version => $IkiWiki::version,
328 run_hooks(pagetemplate => sub {
329 shift->(page => $page, destpage => $page,
330 template => $template);
333 return $template->output;
336 sub pingurl (@) { #{{{
337 return unless $config{pingurl} && %toping;
339 eval q{require RPC::XML::Client};
341 debug("RPC::XML::Client not found, not pinging");
345 # TODO: daemonize here so slow pings don't slow down wiki updates
347 foreach my $page (keys %toping) {
348 my $title=pagetitle(basename($page));
349 my $url="$config{url}/".htmlpage($page);
350 foreach my $pingurl (@{$config{pingurl}}) {
351 debug("Pinging $pingurl for $page");
353 my $client = RPC::XML::Client->new($pingurl);
354 my $req = RPC::XML::request->new('weblogUpdates.ping',
356 my $res = $client->send_request($req);
358 debug("Did not receive response to ping");
361 if (! exists $r->{flerror} || $r->{flerror}) {
362 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
366 debug "Ping failed: $@";