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 ($config{cgiurl} && (exists $params{rootpage} ||
125 (exists $params{postform} && yesno($params{postform})))) {
126 # Add a blog post form, with feed buttons.
127 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
128 $formtemplate->param(cgiurl => $config{cgiurl});
129 $formtemplate->param(rootpage =>
130 exists $params{rootpage} ? $params{rootpage} : $params{page});
131 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
132 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
133 $ret.=$formtemplate->output;
137 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
138 $linktemplate->param(rssurl => $rssurl) if $rss;
139 $linktemplate->param(atomurl => $atomurl) if $atom;
140 $ret.=$linktemplate->output;
143 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
145 return sprintf(gettext("nonexistant template %s"), $params{template});
147 my $template=HTML::Template->new(@params) unless $raw;
149 foreach my $page (@list) {
150 my $file = $pagesources{$page};
151 my $type = pagetype($file);
152 if (! $raw || ($raw && ! defined $type)) {
153 unless ($archive && $quick) {
154 # Get the content before populating the
155 # template, since getting the content uses
156 # the same template if inlines are nested.
157 my $content=get_inline_content($page, $params{destpage});
158 $template->param(content => $content);
160 # Don't use htmllink because this way the
161 # title is separate and can be overridden by
163 my $link=bestlink($params{page}, $page);
164 $link=htmlpage($link) if defined $type;
165 $link=abs2rel($link, dirname($params{destpage}));
166 $template->param(pageurl => $link);
167 $template->param(title => pagetitle(basename($page)));
168 $template->param(ctime => displaytime($pagectime{$page}));
171 my $file = $pagesources{$page};
172 my $type = pagetype($file);
173 if ($config{discussion}) {
174 my $discussionlink=gettext("discussion");
175 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
176 (length $config{cgiurl} ||
177 exists $links{$page."/".$discussionlink})) {
178 $template->param(have_actions => 1);
179 $template->param(discussionlink =>
182 gettext("Discussion"),
187 if (length $config{cgiurl} && defined $type) {
188 $template->param(have_actions => 1);
189 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
193 run_hooks(pagetemplate => sub {
194 shift->(page => $page, destpage => $params{page},
195 template => $template,);
198 $ret.=$template->output;
199 $template->clear_params;
204 linkify($page, $params{page},
205 preprocess($page, $params{page},
207 readfile(srcfile($file)))));
213 if (exists $params{feedshow} && @list > $params{feedshow}) {
214 @list=@list[0..$params{feedshow} - 1];
218 will_render($params{page}, rsspage($params{page}));
219 writefile(rsspage($params{page}), $config{destdir},
220 genfeed("rss", $rssurl, $desc, $params{page}, @list));
221 $toping{$params{page}}=1 unless $config{rebuild};
222 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
225 will_render($params{page}, atompage($params{page}));
226 writefile(atompage($params{page}), $config{destdir},
227 genfeed("atom", $atomurl, $desc, $params{page}, @list));
228 $toping{$params{page}}=1 unless $config{rebuild};
229 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
236 sub pagetemplate_inline (@) { #{{{
238 my $page=$params{page};
239 my $template=$params{template};
241 $template->param(feedlinks => $feedlinks{$page})
242 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
245 sub get_inline_content ($$) { #{{{
249 my $file=$pagesources{$page};
250 my $type=pagetype($file);
252 return htmlize($page, $type,
253 linkify($page, $destpage,
254 preprocess($page, $destpage,
256 readfile(srcfile($file))))));
263 sub date_822 ($) { #{{{
268 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
269 POSIX::setlocale(&POSIX::LC_TIME, "C");
270 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
271 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
275 sub date_3339 ($) { #{{{
280 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
281 POSIX::setlocale(&POSIX::LC_TIME, "C");
282 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
283 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
287 sub absolute_urls ($$) { #{{{
288 # sucky sub because rss sucks
295 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
296 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
297 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
301 sub rsspage ($) { #{{{
307 sub atompage ($) { #{{{
310 return $page.".atom";
313 sub genfeed ($$$$@) { #{{{
320 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
322 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
325 foreach my $p (@pages) {
326 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
328 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
330 $itemtemplate->param(
331 title => pagetitle(basename($p), 1),
334 date_822 => date_822($pagectime{$p}),
335 date_3339 => date_3339($pagectime{$p}),
338 if ($itemtemplate->query(name => "enclosure")) {
339 my $file=$pagesources{$p};
340 my $type=pagetype($file);
342 $itemtemplate->param(content => $pcontent);
345 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
347 eval q{use File::MimeInfo};
349 $mime = mimetype($file);
351 $itemtemplate->param(
359 $itemtemplate->param(content => $pcontent);
362 run_hooks(pagetemplate => sub {
363 shift->(page => $p, destpage => $page,
364 template => $itemtemplate);
367 $content.=$itemtemplate->output;
368 $itemtemplate->clear_params;
370 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
373 my $template=template($feedtype."page.tmpl", blind_cache => 1);
375 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
376 wikiname => $config{wikiname},
379 feeddesc => $feeddesc,
380 feeddate => date_3339($lasttime),
382 version => $IkiWiki::version,
384 run_hooks(pagetemplate => sub {
385 shift->(page => $page, destpage => $page,
386 template => $template);
389 return $template->output;
392 sub pingurl (@) { #{{{
393 return unless @{$config{pingurl}} && %toping;
395 eval q{require RPC::XML::Client};
397 debug(gettext("RPC::XML::Client not found, not pinging"));
401 # daemonize here so slow pings don't slow down wiki updates
402 defined(my $pid = fork) or error("Can't fork: $!");
405 eval q{use POSIX 'setsid'};
406 setsid() or error("Can't start a new session: $!");
407 open STDIN, '/dev/null';
408 open STDOUT, '>/dev/null';
409 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
411 # Don't need to keep a lock on the wiki as a daemon.
412 IkiWiki::unlockwiki();
414 foreach my $page (keys %toping) {
415 my $title=pagetitle(basename($page), 0);
416 my $url="$config{url}/".htmlpage($page);
417 foreach my $pingurl (@{$config{pingurl}}) {
418 debug("Pinging $pingurl for $page");
420 my $client = RPC::XML::Client->new($pingurl);
421 my $req = RPC::XML::request->new('weblogUpdates.ping',
423 my $res = $client->send_request($req);
425 debug("Did not receive response to ping");
428 if (! exists $r->{flerror} || $r->{flerror}) {
429 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
433 debug "Ping failed: $@";
438 exit 0; # daemon done