2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
8 use IkiWiki::Render; # for displaytime
12 hook(type => "preprocess", id => "inline",
13 call => \&IkiWiki::preprocess_inline);
14 hook(type => "pagetemplate", id => "inline",
15 call => \&IkiWiki::pagetemplate_inline);
16 # Hook to change to do pinging since it's called late.
17 # This ensures each page only pings once and prevents slow
18 # pings interrupting page builds.
19 hook(type => "change", id => "inline",
20 call => \&IkiWiki::pingurl);
23 # Back to ikiwiki namespace for the rest, this code is very much
24 # internal to ikiwiki even though it's separated into a plugin.
32 return (defined $val && lc($val) eq "yes");
35 sub preprocess_inline (@) { #{{{
38 if (! exists $params{pages}) {
41 my $raw=yesno($params{raw});
42 my $archive=yesno($params{archive});
43 my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
44 my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
45 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : 1;
46 if (! exists $params{show} && ! $archive) {
50 if (exists $params{description}) {
51 $desc = $params{description}
53 $desc = $config{wikiname};
55 my $actions=yesno($params{actions});
58 foreach my $page (keys %pagesources) {
59 next if $page eq $params{page};
60 if (pagespec_match($page, $params{pages})) {
65 if (exists $params{sort} && $params{sort} eq 'title') {
68 elsif (! exists $params{sort} || $params{sort} eq 'age') {
69 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
72 return "unknown sort type $params{sort}";
75 if ($params{show} && @list > $params{show}) {
76 @list=@list[0..$params{show} - 1];
79 add_depends($params{page}, $params{pages});
81 my $rssurl=rsspage(basename($params{page}));
82 my $atomurl=atompage(basename($params{page}));
85 if (exists $params{rootpage} && $config{cgiurl}) {
86 # Add a blog post form, with feed buttons.
87 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
88 $formtemplate->param(cgiurl => $config{cgiurl});
89 $formtemplate->param(rootpage => $params{rootpage});
90 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
91 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
92 $ret.=$formtemplate->output;
96 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
97 $linktemplate->param(rssurl => $rssurl) if $rss;
98 $linktemplate->param(atomurl => $atomurl) if $atom;
99 $ret.=$linktemplate->output;
102 my $template=template(
103 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
107 foreach my $page (@list) {
109 # Get the content before populating the template,
110 # since getting the content uses the same template
111 # if inlines are nested.
112 # TODO: if $archive=1, the only reason to do this
113 # is to let the meta plugin get page title info; so stop
114 # calling this next line then once the meta plugin can
115 # store that accross runs (also tags plugin).
116 my $content=get_inline_content($page, $params{destpage});
117 # Don't use htmllink because this way the title is separate
118 # and can be overridden by other plugins.
119 my $link=htmlpage(bestlink($params{page}, $page));
120 $link=abs2rel($link, dirname($params{destpage}));
121 $template->param(pageurl => $link);
122 $template->param(title => pagetitle(basename($page)));
123 $template->param(content => $content);
124 $template->param(ctime => displaytime($pagectime{$page}));
127 my $file = $pagesources{$page};
128 my $type = pagetype($file);
129 if ($config{discussion}) {
130 $template->param(have_actions => 1);
131 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
133 if (length $config{cgiurl} && defined $type) {
134 $template->param(have_actions => 1);
135 $template->param(editurl => cgiurl(do => "edit", page => $page));
139 run_hooks(pagetemplate => sub {
140 shift->(page => $page, destpage => $params{page},
141 template => $template,);
144 $ret.=$template->output;
145 $template->clear_params;
148 my $file=$pagesources{$page};
149 my $type=pagetype($file);
152 linkify($page, $params{page},
153 preprocess($page, $params{page},
155 readfile(srcfile($file)))));
160 if ($feeds && $rss) {
161 will_render($params{page}, rsspage($params{page}));
162 writefile(rsspage($params{page}), $config{destdir},
163 genfeed("rss", $rssurl, $desc, $params{page}, @list));
164 $toping{$params{page}}=1 unless $config{rebuild};
165 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
167 if ($feeds && $atom) {
168 will_render($params{page}, atompage($params{page}));
169 writefile(atompage($params{page}), $config{destdir},
170 genfeed("atom", $atomurl, $desc, $params{page}, @list));
171 $toping{$params{page}}=1 unless $config{rebuild};
172 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
178 sub pagetemplate_inline (@) { #{{{
180 my $page=$params{page};
181 my $template=$params{template};
183 $template->param(feedlinks => $feedlinks{$page})
184 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
187 sub get_inline_content ($$) { #{{{
191 my $file=$pagesources{$page};
192 my $type=pagetype($file);
194 return htmlize($page, $type,
195 linkify($page, $destpage,
196 preprocess($page, $destpage,
198 readfile(srcfile($file))))));
205 sub date_822 ($) { #{{{
209 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
210 POSIX::setlocale(&POSIX::LC_TIME, "C");
211 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
212 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
216 sub date_3339 ($) { #{{{
220 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
221 POSIX::setlocale(&POSIX::LC_TIME, "C");
222 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
223 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
227 sub absolute_urls ($$) { #{{{
228 # sucky sub because rss sucks
234 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
235 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
239 sub rsspage ($) { #{{{
245 sub atompage ($) { #{{{
248 return $page.".atom";
251 sub genfeed ($$$$@) { #{{{
258 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
260 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
263 foreach my $p (@pages) {
264 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
266 $itemtemplate->param(
267 title => pagetitle(basename($p)),
270 date_822 => date_822($pagectime{$p}),
271 date_3339 => date_3339($pagectime{$p}),
272 content => absolute_urls(get_inline_content($p, $page), $url),
274 run_hooks(pagetemplate => sub {
275 shift->(page => $p, destpage => $page,
276 template => $itemtemplate);
279 $content.=$itemtemplate->output;
280 $itemtemplate->clear_params;
282 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
285 my $template=template($feedtype."page.tmpl", blind_cache => 1);
287 title => $config{wikiname},
288 wikiname => $config{wikiname},
291 feeddesc => $feeddesc,
292 feeddate => date_3339($lasttime),
294 version => $IkiWiki::version,
296 run_hooks(pagetemplate => sub {
297 shift->(page => $page, destpage => $page,
298 template => $template);
301 return $template->output;
304 sub pingurl (@) { #{{{
305 return unless $config{pingurl} && %toping;
307 eval q{require RPC::XML::Client};
309 debug("RPC::XML::Client not found, not pinging");
313 foreach my $page (keys %toping) {
314 my $title=pagetitle(basename($page));
315 my $url="$config{url}/".htmlpage($page);
316 foreach my $pingurl (@{$config{pingurl}}) {
317 my $client = RPC::XML::Client->new($pingurl);
318 my $req = RPC::XML::request->new('weblogUpdates.ping',
320 debug("Pinging $pingurl for $page");
321 my $res = $client->send_request($req);
323 debug("Did not receive response to ping");
326 if (! exists $r->{flerror} || $r->{flerror}) {
327 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));