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});
82 foreach my $page (keys %pagesources) {
83 next if $page eq $params{page};
84 if (pagespec_match($page, $params{pages}, $params{page})) {
89 if (exists $params{sort} && $params{sort} eq 'title') {
92 elsif (! exists $params{sort} || $params{sort} eq 'age') {
93 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
96 return sprintf(gettext("unknown sort type %s"), $params{sort});
99 if (yesno($params{reverse})) {
100 @list=reverse(@list);
103 if (exists $params{skip}) {
104 @list=@list[$params{skip} .. scalar @list - 1];
107 if ($params{show} && @list > $params{show}) {
108 @list=@list[0..$params{show} - 1];
111 add_depends($params{page}, $params{pages});
113 my $rssurl=rsspage(basename($params{page}));
114 my $atomurl=atompage(basename($params{page}));
117 if (exists $params{rootpage} && $config{cgiurl}) {
118 # Add a blog post form, with feed buttons.
119 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
120 $formtemplate->param(cgiurl => $config{cgiurl});
121 $formtemplate->param(rootpage => $params{rootpage});
122 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
123 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
124 $ret.=$formtemplate->output;
128 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
129 $linktemplate->param(rssurl => $rssurl) if $rss;
130 $linktemplate->param(atomurl => $atomurl) if $atom;
131 $ret.=$linktemplate->output;
134 my $template=template(
135 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
139 foreach my $page (@list) {
140 my $file = $pagesources{$page};
141 my $type = pagetype($file);
142 if (! $raw || ($raw && ! defined $type)) {
143 unless ($archive && $quick) {
144 # Get the content before populating the
145 # template, since getting the content uses
146 # the same template if inlines are nested.
147 my $content=get_inline_content($page, $params{destpage});
148 $template->param(content => $content);
150 # Don't use htmllink because this way the
151 # title is separate and can be overridden by
153 my $link=bestlink($params{page}, $page);
154 $link=htmlpage($link) if defined $type;
155 $link=abs2rel($link, dirname($params{destpage}));
156 $template->param(pageurl => $link);
157 $template->param(title => pagetitle(basename($page)));
158 $template->param(ctime => displaytime($pagectime{$page}));
161 my $file = $pagesources{$page};
162 my $type = pagetype($file);
163 if ($config{discussion}) {
164 my $discussionlink=gettext("discussion");
165 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
166 (length $config{cgiurl} ||
167 exists $links{$page."/".$discussionlink})) {
168 $template->param(have_actions => 1);
169 $template->param(discussionlink => htmllink($page, $params{page}, gettext("Discussion"), 1, 1));
172 if (length $config{cgiurl} && defined $type) {
173 $template->param(have_actions => 1);
174 $template->param(editurl => cgiurl(do => "edit", page => $page));
178 run_hooks(pagetemplate => sub {
179 shift->(page => $page, destpage => $params{page},
180 template => $template,);
183 $ret.=$template->output;
184 $template->clear_params;
189 linkify($page, $params{page},
190 preprocess($page, $params{page},
192 readfile(srcfile($file)))));
198 if (exists $params{feedshow} && @list > $params{feedshow}) {
199 @list=@list[0..$params{feedshow} - 1];
203 will_render($params{page}, rsspage($params{page}));
204 writefile(rsspage($params{page}), $config{destdir},
205 genfeed("rss", $rssurl, $desc, $params{page}, @list));
206 $toping{$params{page}}=1 unless $config{rebuild};
207 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
210 will_render($params{page}, atompage($params{page}));
211 writefile(atompage($params{page}), $config{destdir},
212 genfeed("atom", $atomurl, $desc, $params{page}, @list));
213 $toping{$params{page}}=1 unless $config{rebuild};
214 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
221 sub pagetemplate_inline (@) { #{{{
223 my $page=$params{page};
224 my $template=$params{template};
226 $template->param(feedlinks => $feedlinks{$page})
227 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
230 sub get_inline_content ($$) { #{{{
234 my $file=$pagesources{$page};
235 my $type=pagetype($file);
237 return htmlize($page, $type,
238 linkify($page, $destpage,
239 preprocess($page, $destpage,
241 readfile(srcfile($file))))));
248 sub date_822 ($) { #{{{
253 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
254 POSIX::setlocale(&POSIX::LC_TIME, "C");
255 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
256 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
260 sub date_3339 ($) { #{{{
265 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
266 POSIX::setlocale(&POSIX::LC_TIME, "C");
267 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
268 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
272 sub absolute_urls ($$) { #{{{
273 # sucky sub because rss sucks
280 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
281 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
282 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
286 sub rsspage ($) { #{{{
292 sub atompage ($) { #{{{
295 return $page.".atom";
298 sub genfeed ($$$$@) { #{{{
305 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
307 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
310 foreach my $p (@pages) {
311 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
313 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
315 $itemtemplate->param(
316 title => pagetitle(basename($p), 1),
319 date_822 => date_822($pagectime{$p}),
320 date_3339 => date_3339($pagectime{$p}),
323 if ($itemtemplate->query(name => "enclosure")) {
324 my $file=$pagesources{$p};
325 my $type=pagetype($file);
327 $itemtemplate->param(content => $pcontent);
330 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
332 eval q{use File::MimeInfo};
334 $mime = mimetype($file);
336 $itemtemplate->param(
344 $itemtemplate->param(content => $pcontent);
347 run_hooks(pagetemplate => sub {
348 shift->(page => $p, destpage => $page,
349 template => $itemtemplate);
352 $content.=$itemtemplate->output;
353 $itemtemplate->clear_params;
355 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
358 my $template=template($feedtype."page.tmpl", blind_cache => 1);
360 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
361 wikiname => $config{wikiname},
364 feeddesc => $feeddesc,
365 feeddate => date_3339($lasttime),
367 version => $IkiWiki::version,
369 run_hooks(pagetemplate => sub {
370 shift->(page => $page, destpage => $page,
371 template => $template);
374 return $template->output;
377 sub pingurl (@) { #{{{
378 return unless @{$config{pingurl}} && %toping;
380 eval q{require RPC::XML::Client};
382 debug(gettext("RPC::XML::Client not found, not pinging"));
386 # daemonize here so slow pings don't slow down wiki updates
387 defined(my $pid = fork) or error("Can't fork: $!");
390 eval q{use POSIX 'setsid'};
391 setsid() or error("Can't start a new session: $!");
392 open STDIN, '/dev/null';
393 open STDOUT, '>/dev/null';
394 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
396 # Don't need to keep a lock on the wiki as a daemon.
397 IkiWiki::unlockwiki();
399 foreach my $page (keys %toping) {
400 my $title=pagetitle(basename($page), 0);
401 my $url="$config{url}/".htmlpage($page);
402 foreach my $pingurl (@{$config{pingurl}}) {
403 debug("Pinging $pingurl for $page");
405 my $client = RPC::XML::Client->new($pingurl);
406 my $req = RPC::XML::request->new('weblogUpdates.ping',
408 my $res = $client->send_request($req);
410 debug("Did not receive response to ping");
413 if (! exists $r->{flerror} || $r->{flerror}) {
414 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
418 debug "Ping failed: $@";
423 exit 0; # daemon done