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})) {
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 (exists $params{skip}) {
100 @list=@list[$params{skip} .. scalar @list - 1];
103 if ($params{show} && @list > $params{show}) {
104 @list=@list[0..$params{show} - 1];
107 add_depends($params{page}, $params{pages});
109 my $rssurl=rsspage(basename($params{page}));
110 my $atomurl=atompage(basename($params{page}));
113 if (exists $params{rootpage} && $config{cgiurl}) {
114 # Add a blog post form, with feed buttons.
115 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
116 $formtemplate->param(cgiurl => $config{cgiurl});
117 $formtemplate->param(rootpage => $params{rootpage});
118 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
119 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
120 $ret.=$formtemplate->output;
124 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
125 $linktemplate->param(rssurl => $rssurl) if $rss;
126 $linktemplate->param(atomurl => $atomurl) if $atom;
127 $ret.=$linktemplate->output;
130 my $template=template(
131 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
135 foreach my $page (@list) {
136 my $file = $pagesources{$page};
137 my $type = pagetype($file);
138 if (! $raw || ($raw && ! defined $type)) {
139 unless ($archive && $quick) {
140 # Get the content before populating the
141 # template, since getting the content uses
142 # the same template if inlines are nested.
143 my $content=get_inline_content($page, $params{destpage});
144 $template->param(content => $content);
146 # Don't use htmllink because this way the
147 # title is separate and can be overridden by
149 my $link=bestlink($params{page}, $page);
150 $link=htmlpage($link) if defined $type;
151 $link=abs2rel($link, dirname($params{destpage}));
152 $template->param(pageurl => $link);
153 $template->param(title => pagetitle(basename($page)));
154 $template->param(ctime => displaytime($pagectime{$page}));
157 my $file = $pagesources{$page};
158 my $type = pagetype($file);
159 if ($config{discussion}) {
160 my $discussionlink=gettext("discussion");
161 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
162 (length $config{cgiurl} ||
163 exists $links{$page."/".$discussionlink})) {
164 $template->param(have_actions => 1);
165 $template->param(discussionlink => htmllink($page, $params{page}, gettext("Discussion"), 1, 1));
168 if (length $config{cgiurl} && defined $type) {
169 $template->param(have_actions => 1);
170 $template->param(editurl => cgiurl(do => "edit", page => $page));
174 run_hooks(pagetemplate => sub {
175 shift->(page => $page, destpage => $params{page},
176 template => $template,);
179 $ret.=$template->output;
180 $template->clear_params;
185 linkify($page, $params{page},
186 preprocess($page, $params{page},
188 readfile(srcfile($file)))));
193 if ($feeds && $rss) {
194 will_render($params{page}, rsspage($params{page}));
195 writefile(rsspage($params{page}), $config{destdir},
196 genfeed("rss", $rssurl, $desc, $params{page}, @list));
197 $toping{$params{page}}=1 unless $config{rebuild};
198 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
200 if ($feeds && $atom) {
201 will_render($params{page}, atompage($params{page}));
202 writefile(atompage($params{page}), $config{destdir},
203 genfeed("atom", $atomurl, $desc, $params{page}, @list));
204 $toping{$params{page}}=1 unless $config{rebuild};
205 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
211 sub pagetemplate_inline (@) { #{{{
213 my $page=$params{page};
214 my $template=$params{template};
216 $template->param(feedlinks => $feedlinks{$page})
217 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
220 sub get_inline_content ($$) { #{{{
224 my $file=$pagesources{$page};
225 my $type=pagetype($file);
227 return htmlize($page, $type,
228 linkify($page, $destpage,
229 preprocess($page, $destpage,
231 readfile(srcfile($file))))));
238 sub date_822 ($) { #{{{
243 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
244 POSIX::setlocale(&POSIX::LC_TIME, "C");
245 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
246 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
250 sub date_3339 ($) { #{{{
255 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
256 POSIX::setlocale(&POSIX::LC_TIME, "C");
257 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
258 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
262 sub absolute_urls ($$) { #{{{
263 # sucky sub because rss sucks
270 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
271 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
272 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
276 sub rsspage ($) { #{{{
282 sub atompage ($) { #{{{
285 return $page.".atom";
288 sub genfeed ($$$$@) { #{{{
295 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
297 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
300 foreach my $p (@pages) {
301 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
303 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
305 $itemtemplate->param(
306 title => pagetitle(basename($p), 1),
309 date_822 => date_822($pagectime{$p}),
310 date_3339 => date_3339($pagectime{$p}),
313 if ($itemtemplate->query(name => "enclosure")) {
314 my $file=$pagesources{$p};
315 my $type=pagetype($file);
317 $itemtemplate->param(content => $pcontent);
320 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
322 eval q{use File::MimeInfo};
324 $mime = mimetype($file);
326 $itemtemplate->param(
334 $itemtemplate->param(content => $pcontent);
337 run_hooks(pagetemplate => sub {
338 shift->(page => $p, destpage => $page,
339 template => $itemtemplate);
342 $content.=$itemtemplate->output;
343 $itemtemplate->clear_params;
345 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
348 my $template=template($feedtype."page.tmpl", blind_cache => 1);
350 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
351 wikiname => $config{wikiname},
354 feeddesc => $feeddesc,
355 feeddate => date_3339($lasttime),
357 version => $IkiWiki::version,
359 run_hooks(pagetemplate => sub {
360 shift->(page => $page, destpage => $page,
361 template => $template);
364 return $template->output;
367 sub pingurl (@) { #{{{
368 return unless @{$config{pingurl}} && %toping;
370 eval q{require RPC::XML::Client};
372 debug(gettext("RPC::XML::Client not found, not pinging"));
376 # daemonize here so slow pings don't slow down wiki updates
377 defined(my $pid = fork) or error("Can't fork: $!");
380 eval q{use POSIX 'setsid'};
381 setsid() or error("Can't start a new session: $!");
382 open STDIN, '/dev/null';
383 open STDOUT, '>/dev/null';
384 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
386 # Don't need to keep a lock on the wiki as a daemon.
387 IkiWiki::unlockwiki();
389 foreach my $page (keys %toping) {
390 my $title=pagetitle(basename($page), 0);
391 my $url="$config{url}/".htmlpage($page);
392 foreach my $pingurl (@{$config{pingurl}}) {
393 debug("Pinging $pingurl for $page");
395 my $client = RPC::XML::Client->new($pingurl);
396 my $req = RPC::XML::request->new('weblogUpdates.ping',
398 my $res = $client->send_request($req);
400 debug("Did not receive response to ping");
403 if (! exists $r->{flerror} || $r->{flerror}) {
404 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
408 debug "Ping failed: $@";
413 exit 0; # daemon done