2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
11 hook(type => "getopt", id => "inline", call => \&getopt);
12 hook(type => "checkconfig", id => "inline", call => \&checkconfig);
13 hook(type => "preprocess", id => "inline",
14 call => \&IkiWiki::preprocess_inline);
15 hook(type => "pagetemplate", id => "inline",
16 call => \&IkiWiki::pagetemplate_inline);
17 # Hook to change to do pinging since it's called late.
18 # This ensures each page only pings once and prevents slow
19 # pings interrupting page builds.
20 hook(type => "change", id => "inline",
21 call => \&IkiWiki::pingurl);
25 eval q{use Getopt::Long};
27 Getopt::Long::Configure('pass_through');
29 "rss!" => \$config{rss},
30 "atom!" => \$config{atom},
34 sub checkconfig () { #{{{
35 if (($config{rss} || $config{atom}) && ! length $config{url}) {
36 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
39 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
42 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
46 # Back to ikiwiki namespace for the rest, this code is very much
47 # internal to ikiwiki even though it's separated into a plugin.
55 return (defined $val && lc($val) eq "yes");
58 sub preprocess_inline (@) { #{{{
61 if (! exists $params{pages}) {
64 my $raw=yesno($params{raw});
65 my $archive=yesno($params{archive});
66 my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
67 my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
68 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
69 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
70 $feeds=0 if $params{preview};
71 if (! exists $params{show} && ! $archive) {
75 if (exists $params{description}) {
76 $desc = $params{description}
78 $desc = $config{wikiname};
80 my $actions=yesno($params{actions});
81 if (exists $params{template}) {
82 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
85 $params{template} = $archive ? "archivepage" : "inlinepage";
89 foreach my $page (keys %pagesources) {
90 next if $page eq $params{page};
91 if (pagespec_match($page, $params{pages}, $params{page})) {
96 if (exists $params{sort} && $params{sort} eq 'title') {
99 elsif (! exists $params{sort} || $params{sort} eq 'age') {
100 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
103 return sprintf(gettext("unknown sort type %s"), $params{sort});
106 if (yesno($params{reverse})) {
107 @list=reverse(@list);
110 if (exists $params{skip}) {
111 @list=@list[$params{skip} .. scalar @list - 1];
114 if ($params{show} && @list > $params{show}) {
115 @list=@list[0..$params{show} - 1];
118 add_depends($params{page}, $params{pages});
120 my $rssurl=rsspage(basename($params{page}));
121 my $atomurl=atompage(basename($params{page}));
124 if (exists $params{rootpage} && $config{cgiurl}) {
125 # Add a blog post form, with feed buttons.
126 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
127 $formtemplate->param(cgiurl => $config{cgiurl});
128 $formtemplate->param(rootpage => $params{rootpage});
129 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
130 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
131 $ret.=$formtemplate->output;
135 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
136 $linktemplate->param(rssurl => $rssurl) if $rss;
137 $linktemplate->param(atomurl => $atomurl) if $atom;
138 $ret.=$linktemplate->output;
141 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
143 return sprintf(gettext("nonexistant template %s"), $params{template});
145 my $template=HTML::Template->new(@params) unless $raw;
147 foreach my $page (@list) {
148 my $file = $pagesources{$page};
149 my $type = pagetype($file);
150 if (! $raw || ($raw && ! defined $type)) {
151 unless ($archive && $quick) {
152 # Get the content before populating the
153 # template, since getting the content uses
154 # the same template if inlines are nested.
155 my $content=get_inline_content($page, $params{destpage});
156 $template->param(content => $content);
158 # Don't use htmllink because this way the
159 # title is separate and can be overridden by
161 my $link=bestlink($params{page}, $page);
162 $link=htmlpage($link) if defined $type;
163 $link=abs2rel($link, dirname($params{destpage}));
164 $template->param(pageurl => $link);
165 $template->param(title => pagetitle(basename($page)));
166 $template->param(ctime => displaytime($pagectime{$page}));
169 my $file = $pagesources{$page};
170 my $type = pagetype($file);
171 if ($config{discussion}) {
172 my $discussionlink=gettext("discussion");
173 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
174 (length $config{cgiurl} ||
175 exists $links{$page."/".$discussionlink})) {
176 $template->param(have_actions => 1);
177 $template->param(discussionlink =>
180 gettext("Discussion"),
185 if (length $config{cgiurl} && defined $type) {
186 $template->param(have_actions => 1);
187 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
191 run_hooks(pagetemplate => sub {
192 shift->(page => $page, destpage => $params{page},
193 template => $template,);
196 $ret.=$template->output;
197 $template->clear_params;
202 linkify($page, $params{page},
203 preprocess($page, $params{page},
205 readfile(srcfile($file)))));
211 if (exists $params{feedshow} && @list > $params{feedshow}) {
212 @list=@list[0..$params{feedshow} - 1];
216 will_render($params{page}, rsspage($params{page}));
217 writefile(rsspage($params{page}), $config{destdir},
218 genfeed("rss", $rssurl, $desc, $params{page}, @list));
219 $toping{$params{page}}=1 unless $config{rebuild};
220 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
223 will_render($params{page}, atompage($params{page}));
224 writefile(atompage($params{page}), $config{destdir},
225 genfeed("atom", $atomurl, $desc, $params{page}, @list));
226 $toping{$params{page}}=1 unless $config{rebuild};
227 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
234 sub pagetemplate_inline (@) { #{{{
236 my $page=$params{page};
237 my $template=$params{template};
239 $template->param(feedlinks => $feedlinks{$page})
240 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
243 sub get_inline_content ($$) { #{{{
247 my $file=$pagesources{$page};
248 my $type=pagetype($file);
250 return htmlize($page, $type,
251 linkify($page, $destpage,
252 preprocess($page, $destpage,
254 readfile(srcfile($file))))));
261 sub date_822 ($) { #{{{
266 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
267 POSIX::setlocale(&POSIX::LC_TIME, "C");
268 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
269 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
273 sub date_3339 ($) { #{{{
278 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
279 POSIX::setlocale(&POSIX::LC_TIME, "C");
280 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
281 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
285 sub absolute_urls ($$) { #{{{
286 # sucky sub because rss sucks
293 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
294 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
295 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
299 sub rsspage ($) { #{{{
305 sub atompage ($) { #{{{
308 return $page.".atom";
311 sub genfeed ($$$$@) { #{{{
318 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
320 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
323 foreach my $p (@pages) {
324 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
326 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
328 $itemtemplate->param(
329 title => pagetitle(basename($p), 1),
332 date_822 => date_822($pagectime{$p}),
333 date_3339 => date_3339($pagectime{$p}),
336 if ($itemtemplate->query(name => "enclosure")) {
337 my $file=$pagesources{$p};
338 my $type=pagetype($file);
340 $itemtemplate->param(content => $pcontent);
343 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
345 eval q{use File::MimeInfo};
347 $mime = mimetype($file);
349 $itemtemplate->param(
357 $itemtemplate->param(content => $pcontent);
360 run_hooks(pagetemplate => sub {
361 shift->(page => $p, destpage => $page,
362 template => $itemtemplate);
365 $content.=$itemtemplate->output;
366 $itemtemplate->clear_params;
368 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
371 my $template=template($feedtype."page.tmpl", blind_cache => 1);
373 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
374 wikiname => $config{wikiname},
377 feeddesc => $feeddesc,
378 feeddate => date_3339($lasttime),
380 version => $IkiWiki::version,
382 run_hooks(pagetemplate => sub {
383 shift->(page => $page, destpage => $page,
384 template => $template);
387 return $template->output;
390 sub pingurl (@) { #{{{
391 return unless @{$config{pingurl}} && %toping;
393 eval q{require RPC::XML::Client};
395 debug(gettext("RPC::XML::Client not found, not pinging"));
399 # daemonize here so slow pings don't slow down wiki updates
400 defined(my $pid = fork) or error("Can't fork: $!");
403 eval q{use POSIX 'setsid'};
404 setsid() or error("Can't start a new session: $!");
405 open STDIN, '/dev/null';
406 open STDOUT, '>/dev/null';
407 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
409 # Don't need to keep a lock on the wiki as a daemon.
410 IkiWiki::unlockwiki();
412 foreach my $page (keys %toping) {
413 my $title=pagetitle(basename($page), 0);
414 my $url="$config{url}/".htmlpage($page);
415 foreach my $pingurl (@{$config{pingurl}}) {
416 debug("Pinging $pingurl for $page");
418 my $client = RPC::XML::Client->new($pingurl);
419 my $req = RPC::XML::request->new('weblogUpdates.ping',
421 my $res = $client->send_request($req);
423 debug("Did not receive response to ping");
426 if (! exists $r->{flerror} || $r->{flerror}) {
427 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
431 debug "Ping failed: $@";
436 exit 0; # daemon done