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=exists $params{rss} ? yesno($params{rss}) : 1;
44 if (! exists $params{show} && ! $archive) {
48 if (exists $params{description}) {
49 $desc = $params{description}
51 $desc = $config{wikiname};
53 my $actions=yesno($params{actions});
56 foreach my $page (keys %pagesources) {
57 next if $page eq $params{page};
58 if (pagespec_match($page, $params{pages})) {
62 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
63 if ($params{show} && @list > $params{show}) {
64 @list=@list[0..$params{show} - 1];
67 add_depends($params{page}, $params{pages});
69 my $rssurl=rsspage(basename($params{page}));
72 if (exists $params{rootpage} && $config{cgiurl}) {
73 # Add a blog post form, with a rss link button.
74 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
75 $formtemplate->param(cgiurl => $config{cgiurl});
76 $formtemplate->param(rootpage => $params{rootpage});
78 $formtemplate->param(rssurl => $rssurl);
80 $ret.=$formtemplate->output;
82 elsif ($config{rss} && $rss) {
83 # Add a rss link button.
84 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
85 $linktemplate->param(rssurl => $rssurl);
86 $ret.=$linktemplate->output;
89 my $template=template(
90 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
94 foreach my $page (@list) {
96 # Get the content before populating the template,
97 # since getting the content uses the same template
98 # if inlines are nested.
99 # TODO: if $archive=1, the only reason to do this
100 # is to let the meta plugin get page title info; so stop
101 # calling this next line then once the meta plugin can
102 # store that accross runs (also tags plugin).
103 my $content=get_inline_content($page, $params{destpage});
104 # Don't use htmllink because this way the title is separate
105 # and can be overridden by other plugins.
106 my $link=htmlpage(bestlink($params{page}, $page));
107 $link=abs2rel($link, dirname($params{destpage}));
108 $template->param(pageurl => $link);
109 $template->param(title => pagetitle(basename($page)));
110 $template->param(content => $content);
111 $template->param(ctime => displaytime($pagectime{$page}));
114 my $file = $pagesources{$page};
115 my $type = pagetype($file);
116 if ($config{discussion}) {
117 $template->param(have_actions => 1);
118 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
120 if (length $config{cgiurl} && defined $type) {
121 $template->param(have_actions => 1);
122 $template->param(editurl => cgiurl(do => "edit", page => $page));
126 run_hooks(pagetemplate => sub {
127 shift->(page => $page, destpage => $params{page},
128 template => $template,);
131 $ret.=$template->output;
132 $template->clear_params;
135 my $file=$pagesources{$page};
136 my $type=pagetype($file);
139 linkify($page, $params{page},
140 preprocess($page, $params{page},
142 readfile(srcfile($file)))));
147 # TODO: should really add this to renderedfiles and call
148 # check_overwrite, but currently renderedfiles
149 # only supports listing one file per page.
150 if ($config{rss} && $rss) {
151 writefile(rsspage($params{page}), $config{destdir},
152 genrss($desc, $params{page}, @list));
153 $toping{$params{page}}=1 unless $config{rebuild};
154 $rsslinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
160 sub pagetemplate_inline (@) { #{{{
162 my $page=$params{page};
163 my $template=$params{template};
165 $template->param(rsslink => $rsslinks{$page})
166 if exists $rsslinks{$page} && $template->query(name => "rsslink");
169 sub get_inline_content ($$) { #{{{
173 my $file=$pagesources{$page};
174 my $type=pagetype($file);
176 return htmlize($page, $type,
177 linkify($page, $destpage,
178 preprocess($page, $destpage,
180 readfile(srcfile($file))))));
187 sub date_822 ($) { #{{{
191 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
192 POSIX::setlocale(&POSIX::LC_TIME, "C");
193 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
194 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
198 sub absolute_urls ($$) { #{{{
199 # sucky sub because rss sucks
205 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
206 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
210 sub rsspage ($) { #{{{
216 sub genrss ($$@) { #{{{
221 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
223 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
225 foreach my $p (@pages) {
226 next unless exists $renderedfiles{$p};
228 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
230 $itemtemplate->param(
231 title => pagetitle(basename($p)),
234 pubdate => date_822($pagectime{$p}),
235 content => absolute_urls(get_inline_content($p, $page), $url),
237 run_hooks(pagetemplate => sub {
238 shift->(page => $p, destpage => $page,
239 template => $itemtemplate);
242 $content.=$itemtemplate->output;
243 $itemtemplate->clear_params;
246 my $template=template("rsspage.tmpl", blind_cache => 1);
248 title => $config{wikiname},
249 wikiname => $config{wikiname},
254 run_hooks(pagetemplate => sub {
255 shift->(page => $page, destpage => $page,
256 template => $template);
259 return $template->output;
262 sub pingurl (@) { #{{{
263 return unless $config{pingurl} && %toping;
265 eval q{require RPC::XML::Client};
267 debug("RPC::XML::Client not found, not pinging");
271 foreach my $page (keys %toping) {
272 my $title=pagetitle(basename($page));
273 my $url="$config{url}/".htmlpage($page);
274 foreach my $pingurl (@{$config{pingurl}}) {
275 my $client = RPC::XML::Client->new($pingurl);
276 my $req = RPC::XML::request->new('weblogUpdates.ping',
278 debug("Pinging $pingurl for $page");
279 my $res = $client->send_request($req);
281 debug("Did not receive response to ping");
284 if (! exists $r->{flerror} || $r->{flerror}) {
285 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));