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 if (! exists $params{show} && ! $archive) {
74 if (exists $params{description}) {
75 $desc = $params{description}
77 $desc = $config{wikiname};
79 my $actions=yesno($params{actions});
80 if (exists $params{template}) {
81 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
84 $params{template} = $archive ? "archivepage" : "inlinepage";
88 foreach my $page (keys %pagesources) {
89 next if $page eq $params{page};
90 if (pagespec_match($page, $params{pages}, $params{page})) {
95 if (exists $params{sort} && $params{sort} eq 'title') {
98 elsif (! exists $params{sort} || $params{sort} eq 'age') {
99 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
102 return sprintf(gettext("unknown sort type %s"), $params{sort});
105 if (yesno($params{reverse})) {
106 @list=reverse(@list);
109 if (exists $params{skip}) {
110 @list=@list[$params{skip} .. scalar @list - 1];
113 if ($params{show} && @list > $params{show}) {
114 @list=@list[0..$params{show} - 1];
117 add_depends($params{page}, $params{pages});
119 my $rssurl=rsspage(basename($params{page}));
120 my $atomurl=atompage(basename($params{page}));
123 if (exists $params{rootpage} && $config{cgiurl}) {
124 # Add a blog post form, with feed buttons.
125 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
126 $formtemplate->param(cgiurl => $config{cgiurl});
127 $formtemplate->param(rootpage => $params{rootpage});
128 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
129 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
130 $ret.=$formtemplate->output;
134 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
135 $linktemplate->param(rssurl => $rssurl) if $rss;
136 $linktemplate->param(atomurl => $atomurl) if $atom;
137 $ret.=$linktemplate->output;
140 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
142 return sprintf(gettext("nonexistant template %s @params"), $params{template});
144 my $template=HTML::Template->new(@params) unless $raw;
146 foreach my $page (@list) {
147 my $file = $pagesources{$page};
148 my $type = pagetype($file);
149 if (! $raw || ($raw && ! defined $type)) {
150 unless ($archive && $quick) {
151 # Get the content before populating the
152 # template, since getting the content uses
153 # the same template if inlines are nested.
154 my $content=get_inline_content($page, $params{destpage});
155 $template->param(content => $content);
157 # Don't use htmllink because this way the
158 # title is separate and can be overridden by
160 my $link=bestlink($params{page}, $page);
161 $link=htmlpage($link) if defined $type;
162 $link=abs2rel($link, dirname($params{destpage}));
163 $template->param(pageurl => $link);
164 $template->param(title => pagetitle(basename($page)));
165 $template->param(ctime => displaytime($pagectime{$page}));
168 my $file = $pagesources{$page};
169 my $type = pagetype($file);
170 if ($config{discussion}) {
171 my $discussionlink=gettext("discussion");
172 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
173 (length $config{cgiurl} ||
174 exists $links{$page."/".$discussionlink})) {
175 $template->param(have_actions => 1);
176 $template->param(discussionlink =>
179 gettext("Discussion"),
184 if (length $config{cgiurl} && defined $type) {
185 $template->param(have_actions => 1);
186 $template->param(editurl => cgiurl(do => "edit", page => $page));
190 run_hooks(pagetemplate => sub {
191 shift->(page => $page, destpage => $params{page},
192 template => $template,);
195 $ret.=$template->output;
196 $template->clear_params;
201 linkify($page, $params{page},
202 preprocess($page, $params{page},
204 readfile(srcfile($file)))));
210 if (exists $params{feedshow} && @list > $params{feedshow}) {
211 @list=@list[0..$params{feedshow} - 1];
215 will_render($params{page}, rsspage($params{page}));
216 writefile(rsspage($params{page}), $config{destdir},
217 genfeed("rss", $rssurl, $desc, $params{page}, @list));
218 $toping{$params{page}}=1 unless $config{rebuild};
219 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
222 will_render($params{page}, atompage($params{page}));
223 writefile(atompage($params{page}), $config{destdir},
224 genfeed("atom", $atomurl, $desc, $params{page}, @list));
225 $toping{$params{page}}=1 unless $config{rebuild};
226 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
233 sub pagetemplate_inline (@) { #{{{
235 my $page=$params{page};
236 my $template=$params{template};
238 $template->param(feedlinks => $feedlinks{$page})
239 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
242 sub get_inline_content ($$) { #{{{
246 my $file=$pagesources{$page};
247 my $type=pagetype($file);
249 return htmlize($page, $type,
250 linkify($page, $destpage,
251 preprocess($page, $destpage,
253 readfile(srcfile($file))))));
260 sub date_822 ($) { #{{{
265 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
266 POSIX::setlocale(&POSIX::LC_TIME, "C");
267 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
268 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
272 sub date_3339 ($) { #{{{
277 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
278 POSIX::setlocale(&POSIX::LC_TIME, "C");
279 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
280 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
284 sub absolute_urls ($$) { #{{{
285 # sucky sub because rss sucks
292 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
293 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
294 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
298 sub rsspage ($) { #{{{
304 sub atompage ($) { #{{{
307 return $page.".atom";
310 sub genfeed ($$$$@) { #{{{
317 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
319 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
322 foreach my $p (@pages) {
323 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
325 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
327 $itemtemplate->param(
328 title => pagetitle(basename($p), 1),
331 date_822 => date_822($pagectime{$p}),
332 date_3339 => date_3339($pagectime{$p}),
335 if ($itemtemplate->query(name => "enclosure")) {
336 my $file=$pagesources{$p};
337 my $type=pagetype($file);
339 $itemtemplate->param(content => $pcontent);
342 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
344 eval q{use File::MimeInfo};
346 $mime = mimetype($file);
348 $itemtemplate->param(
356 $itemtemplate->param(content => $pcontent);
359 run_hooks(pagetemplate => sub {
360 shift->(page => $p, destpage => $page,
361 template => $itemtemplate);
364 $content.=$itemtemplate->output;
365 $itemtemplate->clear_params;
367 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
370 my $template=template($feedtype."page.tmpl", blind_cache => 1);
372 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
373 wikiname => $config{wikiname},
376 feeddesc => $feeddesc,
377 feeddate => date_3339($lasttime),
379 version => $IkiWiki::version,
381 run_hooks(pagetemplate => sub {
382 shift->(page => $page, destpage => $page,
383 template => $template);
386 return $template->output;
389 sub pingurl (@) { #{{{
390 return unless @{$config{pingurl}} && %toping;
392 eval q{require RPC::XML::Client};
394 debug(gettext("RPC::XML::Client not found, not pinging"));
398 # daemonize here so slow pings don't slow down wiki updates
399 defined(my $pid = fork) or error("Can't fork: $!");
402 eval q{use POSIX 'setsid'};
403 setsid() or error("Can't start a new session: $!");
404 open STDIN, '/dev/null';
405 open STDOUT, '>/dev/null';
406 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
408 # Don't need to keep a lock on the wiki as a daemon.
409 IkiWiki::unlockwiki();
411 foreach my $page (keys %toping) {
412 my $title=pagetitle(basename($page), 0);
413 my $url="$config{url}/".htmlpage($page);
414 foreach my $pingurl (@{$config{pingurl}}) {
415 debug("Pinging $pingurl for $page");
417 my $client = RPC::XML::Client->new($pingurl);
418 my $req = RPC::XML::request->new('weblogUpdates.ping',
420 my $res = $client->send_request($req);
422 debug("Did not receive response to ping");
425 if (! exists $r->{flerror} || $r->{flerror}) {
426 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
430 debug "Ping failed: $@";
435 exit 0; # daemon done