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 ($params{show} && @list > $params{show}) {
76 @list=@list[0..$params{show} - 1];
79 add_depends($params{page}, $params{pages});
81 my $rssurl=rsspage(basename($params{page}));
82 my $atomurl=atompage(basename($params{page}));
85 if (exists $params{rootpage} && $config{cgiurl}) {
86 # Add a blog post form, with feed buttons.
87 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
88 $formtemplate->param(cgiurl => $config{cgiurl});
89 $formtemplate->param(rootpage => $params{rootpage});
90 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
91 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
92 $ret.=$formtemplate->output;
96 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
97 $linktemplate->param(rssurl => $rssurl) if $rss;
98 $linktemplate->param(atomurl => $atomurl) if $atom;
99 $ret.=$linktemplate->output;
102 my $template=template(
103 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
107 foreach my $page (@list) {
108 my $file = $pagesources{$page};
109 my $type = pagetype($file);
110 if (! $raw || ($raw && ! defined $type)) {
111 # Get the content before populating the template,
112 # since getting the content uses the same template
113 # if inlines are nested.
114 # TODO: if $archive=1, the only reason to do this
115 # is to let the meta plugin get page title info; so stop
116 # calling this next line then once the meta plugin can
117 # store that accross runs (also tags plugin).
118 my $content=get_inline_content($page, $params{destpage});
119 # Don't use htmllink because this way the title is separate
120 # and can be overridden by other plugins.
121 my $link=bestlink($params{page}, $page);
122 $link=htmlpage($link) if defined $type;
123 $link=abs2rel($link, dirname($params{destpage}));
124 $template->param(pageurl => $link);
125 $template->param(title => pagetitle(basename($page)));
126 $template->param(content => $content);
127 $template->param(ctime => displaytime($pagectime{$page}));
130 my $file = $pagesources{$page};
131 my $type = pagetype($file);
132 if ($config{discussion}) {
133 $template->param(have_actions => 1);
134 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
136 if (length $config{cgiurl} && defined $type) {
137 $template->param(have_actions => 1);
138 $template->param(editurl => cgiurl(do => "edit", page => $page));
142 run_hooks(pagetemplate => sub {
143 shift->(page => $page, destpage => $params{page},
144 template => $template,);
147 $ret.=$template->output;
148 $template->clear_params;
153 linkify($page, $params{page},
154 preprocess($page, $params{page},
156 readfile(srcfile($file)))));
161 if ($feeds && $rss) {
162 will_render($params{page}, rsspage($params{page}));
163 writefile(rsspage($params{page}), $config{destdir},
164 genfeed("rss", $rssurl, $desc, $params{page}, @list));
165 $toping{$params{page}}=1 unless $config{rebuild};
166 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
168 if ($feeds && $atom) {
169 will_render($params{page}, atompage($params{page}));
170 writefile(atompage($params{page}), $config{destdir},
171 genfeed("atom", $atomurl, $desc, $params{page}, @list));
172 $toping{$params{page}}=1 unless $config{rebuild};
173 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
179 sub pagetemplate_inline (@) { #{{{
181 my $page=$params{page};
182 my $template=$params{template};
184 $template->param(feedlinks => $feedlinks{$page})
185 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
188 sub get_inline_content ($$) { #{{{
192 my $file=$pagesources{$page};
193 my $type=pagetype($file);
195 return htmlize($page, $type,
196 linkify($page, $destpage,
197 preprocess($page, $destpage,
199 readfile(srcfile($file))))));
206 sub date_822 ($) { #{{{
210 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
211 POSIX::setlocale(&POSIX::LC_TIME, "C");
212 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
213 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
217 sub date_3339 ($) { #{{{
221 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
222 POSIX::setlocale(&POSIX::LC_TIME, "C");
223 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
224 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
228 sub absolute_urls ($$) { #{{{
229 # sucky sub because rss sucks
235 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
236 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
240 sub rsspage ($) { #{{{
246 sub atompage ($) { #{{{
249 return $page.".atom";
252 sub genfeed ($$$$@) { #{{{
259 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
261 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
264 foreach my $p (@pages) {
265 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
267 $itemtemplate->param(
268 title => pagetitle(basename($p)),
271 date_822 => date_822($pagectime{$p}),
272 date_3339 => date_3339($pagectime{$p}),
275 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
276 if ($itemtemplate->query(name => "enclosure")) {
277 my $file=$pagesources{$p};
278 my $type=pagetype($file);
280 $itemtemplate->param(content => $pcontent);
283 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
285 eval q{use File::MimeInfo};
287 $mime = mimetype($file);
289 $itemtemplate->param(
297 $itemtemplate->param(content => $pcontent);
300 run_hooks(pagetemplate => sub {
301 shift->(page => $p, destpage => $page,
302 template => $itemtemplate);
305 $content.=$itemtemplate->output;
306 $itemtemplate->clear_params;
308 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
311 my $template=template($feedtype."page.tmpl", blind_cache => 1);
313 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
314 wikiname => $config{wikiname},
317 feeddesc => $feeddesc,
318 feeddate => date_3339($lasttime),
320 version => $IkiWiki::version,
322 run_hooks(pagetemplate => sub {
323 shift->(page => $page, destpage => $page,
324 template => $template);
327 return $template->output;
330 sub pingurl (@) { #{{{
331 return unless $config{pingurl} && %toping;
333 eval q{require RPC::XML::Client};
335 debug("RPC::XML::Client not found, not pinging");
339 # TODO: daemonize here so slow pings don't slow down wiki updates
341 foreach my $page (keys %toping) {
342 my $title=pagetitle(basename($page));
343 my $url="$config{url}/".htmlpage($page);
344 foreach my $pingurl (@{$config{pingurl}}) {
345 debug("Pinging $pingurl for $page");
347 my $client = RPC::XML::Client->new($pingurl);
348 my $req = RPC::XML::request->new('weblogUpdates.ping',
350 my $res = $client->send_request($req);
352 debug("Did not receive response to ping");
355 if (! exists $r->{flerror} || $r->{flerror}) {
356 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
360 debug "Ping failed: $@";