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 =>
172 gettext("Discussion"),
177 if (length $config{cgiurl} && defined $type) {
178 $template->param(have_actions => 1);
179 $template->param(editurl => cgiurl(do => "edit", page => $page));
183 run_hooks(pagetemplate => sub {
184 shift->(page => $page, destpage => $params{page},
185 template => $template,);
188 $ret.=$template->output;
189 $template->clear_params;
194 linkify($page, $params{page},
195 preprocess($page, $params{page},
197 readfile(srcfile($file)))));
203 if (exists $params{feedshow} && @list > $params{feedshow}) {
204 @list=@list[0..$params{feedshow} - 1];
208 will_render($params{page}, rsspage($params{page}));
209 writefile(rsspage($params{page}), $config{destdir},
210 genfeed("rss", $rssurl, $desc, $params{page}, @list));
211 $toping{$params{page}}=1 unless $config{rebuild};
212 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
215 will_render($params{page}, atompage($params{page}));
216 writefile(atompage($params{page}), $config{destdir},
217 genfeed("atom", $atomurl, $desc, $params{page}, @list));
218 $toping{$params{page}}=1 unless $config{rebuild};
219 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
226 sub pagetemplate_inline (@) { #{{{
228 my $page=$params{page};
229 my $template=$params{template};
231 $template->param(feedlinks => $feedlinks{$page})
232 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
235 sub get_inline_content ($$) { #{{{
239 my $file=$pagesources{$page};
240 my $type=pagetype($file);
242 return htmlize($page, $type,
243 linkify($page, $destpage,
244 preprocess($page, $destpage,
246 readfile(srcfile($file))))));
253 sub date_822 ($) { #{{{
258 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
259 POSIX::setlocale(&POSIX::LC_TIME, "C");
260 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
261 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
265 sub date_3339 ($) { #{{{
270 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
271 POSIX::setlocale(&POSIX::LC_TIME, "C");
272 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
273 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
277 sub absolute_urls ($$) { #{{{
278 # sucky sub because rss sucks
285 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
286 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
287 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
291 sub rsspage ($) { #{{{
297 sub atompage ($) { #{{{
300 return $page.".atom";
303 sub genfeed ($$$$@) { #{{{
310 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
312 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
315 foreach my $p (@pages) {
316 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
318 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
320 $itemtemplate->param(
321 title => pagetitle(basename($p), 1),
324 date_822 => date_822($pagectime{$p}),
325 date_3339 => date_3339($pagectime{$p}),
328 if ($itemtemplate->query(name => "enclosure")) {
329 my $file=$pagesources{$p};
330 my $type=pagetype($file);
332 $itemtemplate->param(content => $pcontent);
335 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
337 eval q{use File::MimeInfo};
339 $mime = mimetype($file);
341 $itemtemplate->param(
349 $itemtemplate->param(content => $pcontent);
352 run_hooks(pagetemplate => sub {
353 shift->(page => $p, destpage => $page,
354 template => $itemtemplate);
357 $content.=$itemtemplate->output;
358 $itemtemplate->clear_params;
360 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
363 my $template=template($feedtype."page.tmpl", blind_cache => 1);
365 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
366 wikiname => $config{wikiname},
369 feeddesc => $feeddesc,
370 feeddate => date_3339($lasttime),
372 version => $IkiWiki::version,
374 run_hooks(pagetemplate => sub {
375 shift->(page => $page, destpage => $page,
376 template => $template);
379 return $template->output;
382 sub pingurl (@) { #{{{
383 return unless @{$config{pingurl}} && %toping;
385 eval q{require RPC::XML::Client};
387 debug(gettext("RPC::XML::Client not found, not pinging"));
391 # daemonize here so slow pings don't slow down wiki updates
392 defined(my $pid = fork) or error("Can't fork: $!");
395 eval q{use POSIX 'setsid'};
396 setsid() or error("Can't start a new session: $!");
397 open STDIN, '/dev/null';
398 open STDOUT, '>/dev/null';
399 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
401 # Don't need to keep a lock on the wiki as a daemon.
402 IkiWiki::unlockwiki();
404 foreach my $page (keys %toping) {
405 my $title=pagetitle(basename($page), 0);
406 my $url="$config{url}/".htmlpage($page);
407 foreach my $pingurl (@{$config{pingurl}}) {
408 debug("Pinging $pingurl for $page");
410 my $client = RPC::XML::Client->new($pingurl);
411 my $req = RPC::XML::request->new('weblogUpdates.ping',
413 my $res = $client->send_request($req);
415 debug("Did not receive response to ping");
418 if (! exists $r->{flerror} || $r->{flerror}) {
419 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
423 debug "Ping failed: $@";
428 exit 0; # daemon done