2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
11 IkiWiki::hook(type => "preprocess", id => "inline",
12 call => \&IkiWiki::preprocess_inline);
13 IkiWiki::hook(type => "pagetemplate", id => "inline",
14 call => \&IkiWiki::pagetemplate_inline);
15 # Hook to change to do pinging since it's called late.
16 # This ensures each page only pings once and prevents slow
17 # pings interrupting page builds.
18 IkiWiki::hook(type => "change", id => "inline",
19 call => \&IkiWiki::pingurl);
22 # Back to ikiwiki namespace for the rest, this code is very much
23 # internal to ikiwiki even though it's separated into a plugin.
31 return (defined $val && lc($val) eq "yes");
34 sub preprocess_inline (@) { #{{{
37 if (! exists $params{pages}) {
40 my $raw=yesno($params{raw});
41 my $archive=yesno($params{archive});
42 my $rss=exists $params{rss} ? yesno($params{rss}) : 1;
43 if (! exists $params{show} && ! $archive) {
47 if (exists $params{description}) {
48 $desc = $params{description}
50 $desc = $config{wikiname};
52 my $actions=yesno($params{actions});
55 foreach my $page (keys %pagesources) {
56 next if $page eq $params{page};
57 if (pagespec_match($page, $params{pages})) {
61 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
62 if ($params{show} && @list > $params{show}) {
63 @list=@list[0..$params{show} - 1];
66 add_depends($params{page}, $params{pages});
68 my $rssurl=rsspage(basename($params{page}));
71 if (exists $params{rootpage} && $config{cgiurl}) {
72 # Add a blog post form, with a rss link button.
73 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
74 $formtemplate->param(cgiurl => $config{cgiurl});
75 $formtemplate->param(rootpage => $params{rootpage});
77 $formtemplate->param(rssurl => $rssurl);
79 $ret.=$formtemplate->output;
81 elsif ($config{rss} && $rss) {
82 # Add a rss link button.
83 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
84 $linktemplate->param(rssurl => $rssurl);
85 $ret.=$linktemplate->output;
88 my $template=template(
89 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
93 foreach my $page (@list) {
95 # Get the content before populating the template,
96 # since getting the content uses the same template
97 # if inlines are nested.
98 # TODO: if $archive=1, the only reason to do this
99 # is to let the meta plugin get page title info; so stop
100 # calling this next line then once the meta plugin can
101 # store that accross runs (also tags plugin).
102 my $content=get_inline_content($page, $params{destpage});
103 # Don't use htmllink because this way the title is separate
104 # and can be overridden by other plugins.
105 my $link=htmlpage(bestlink($params{page}, $page));
106 $link=abs2rel($link, dirname($params{destpage}));
107 $template->param(pageurl => $link);
108 $template->param(title => pagetitle(basename($page)));
109 $template->param(content => $content);
110 $template->param(ctime => displaytime($pagectime{$page}));
113 my $file = $pagesources{$page};
114 my $type = pagetype($file);
115 if ($config{discussion}) {
116 $template->param(have_actions => 1);
117 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
119 if (length $config{cgiurl} && defined $type) {
120 $template->param(have_actions => 1);
121 $template->param(editurl => cgiurl(do => "edit", page => $page));
125 run_hooks(pagetemplate => sub {
126 shift->(page => $page, destpage => $params{page},
127 template => $template,);
130 $ret.=$template->output;
131 $template->clear_params;
134 my $file=$pagesources{$page};
135 my $type=pagetype($file);
138 linkify($page, $params{page},
139 preprocess($page, $params{page},
141 readfile(srcfile($file)))));
146 # TODO: should really add this to renderedfiles and call
147 # check_overwrite, but currently renderedfiles
148 # only supports listing one file per page.
149 if ($config{rss} && $rss) {
150 writefile(rsspage($params{page}), $config{destdir},
151 genrss($desc, $params{page}, @list));
152 $toping{$params{page}}=1 unless $config{rebuild};
153 $rsslinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
159 sub pagetemplate_inline (@) { #{{{
161 my $page=$params{page};
162 my $template=$params{template};
164 $template->param(rsslink => $rsslinks{$page})
165 if exists $rsslinks{$page} && $template->query(name => "rsslink");
168 sub get_inline_content ($$) { #{{{
172 my $file=$pagesources{$page};
173 my $type=pagetype($file);
175 return htmlize($page, $type,
176 linkify($page, $destpage,
177 preprocess($page, $destpage,
179 readfile(srcfile($file))))));
186 sub date_822 ($) { #{{{
190 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
191 POSIX::setlocale(&POSIX::LC_TIME, "C");
192 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
193 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
197 sub absolute_urls ($$) { #{{{
198 # sucky sub because rss sucks
204 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
205 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
209 sub rsspage ($) { #{{{
215 sub genrss ($$@) { #{{{
220 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
222 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
224 foreach my $p (@pages) {
225 next unless exists $renderedfiles{$p};
227 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
229 $itemtemplate->param(
230 title => pagetitle(basename($p)),
233 pubdate => date_822($pagectime{$p}),
234 content => absolute_urls(get_inline_content($p, $page), $url),
236 run_hooks(pagetemplate => sub {
237 shift->(page => $p, destpage => $page,
238 template => $itemtemplate);
241 $content.=$itemtemplate->output;
242 $itemtemplate->clear_params;
245 my $template=template("rsspage.tmpl", blind_cache => 1);
247 title => $config{wikiname},
248 wikiname => $config{wikiname},
253 run_hooks(pagetemplate => sub {
254 shift->(page => $page, destpage => $page,
255 template => $template);
258 return $template->output;
261 sub pingurl (@) { #{{{
262 return unless $config{pingurl} && %toping;
264 eval q{require RPC::XML::Client};
266 debug("RPC::XML::Client not found, not pinging");
270 foreach my $page (keys %toping) {
271 my $title=pagetitle(basename($page));
272 my $url="$config{url}/".htmlpage($page);
273 foreach my $pingurl (@{$config{pingurl}}) {
274 my $client = RPC::XML::Client->new($pingurl);
275 my $req = RPC::XML::request->new('weblogUpdates.ping',
277 debug("Pinging $pingurl for $page");
278 my $res = $client->send_request($req);
280 debug("Did not receive response to ping");
283 if (! exists $r->{flerror} || $r->{flerror}) {
284 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));