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})) {
63 if (exists $params{sort} && $params{sort} eq 'title') {
66 elsif (! exists $params{sort} || $params{sort} eq 'age') {
67 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
70 return "unknown sort type $params{sort}";
73 if ($params{show} && @list > $params{show}) {
74 @list=@list[0..$params{show} - 1];
77 add_depends($params{page}, $params{pages});
79 my $rssurl=rsspage(basename($params{page}));
82 if (exists $params{rootpage} && $config{cgiurl}) {
83 # Add a blog post form, with a rss link button.
84 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
85 $formtemplate->param(cgiurl => $config{cgiurl});
86 $formtemplate->param(rootpage => $params{rootpage});
88 $formtemplate->param(rssurl => $rssurl);
90 $ret.=$formtemplate->output;
92 elsif ($config{rss} && $rss) {
93 # Add a rss link button.
94 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
95 $linktemplate->param(rssurl => $rssurl);
96 $ret.=$linktemplate->output;
99 my $template=template(
100 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
104 foreach my $page (@list) {
106 # Get the content before populating the template,
107 # since getting the content uses the same template
108 # if inlines are nested.
109 # TODO: if $archive=1, the only reason to do this
110 # is to let the meta plugin get page title info; so stop
111 # calling this next line then once the meta plugin can
112 # store that accross runs (also tags plugin).
113 my $content=get_inline_content($page, $params{destpage});
114 # Don't use htmllink because this way the title is separate
115 # and can be overridden by other plugins.
116 my $link=htmlpage(bestlink($params{page}, $page));
117 $link=abs2rel($link, dirname($params{destpage}));
118 $template->param(pageurl => $link);
119 $template->param(title => pagetitle(basename($page)));
120 $template->param(content => $content);
121 $template->param(ctime => displaytime($pagectime{$page}));
124 my $file = $pagesources{$page};
125 my $type = pagetype($file);
126 if ($config{discussion}) {
127 $template->param(have_actions => 1);
128 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
130 if (length $config{cgiurl} && defined $type) {
131 $template->param(have_actions => 1);
132 $template->param(editurl => cgiurl(do => "edit", page => $page));
136 run_hooks(pagetemplate => sub {
137 shift->(page => $page, destpage => $params{page},
138 template => $template,);
141 $ret.=$template->output;
142 $template->clear_params;
145 my $file=$pagesources{$page};
146 my $type=pagetype($file);
149 linkify($page, $params{page},
150 preprocess($page, $params{page},
152 readfile(srcfile($file)))));
157 # TODO: should really add this to renderedfiles and call
158 # check_overwrite, but currently renderedfiles
159 # only supports listing one file per page.
160 if ($config{rss} && $rss) {
161 writefile(rsspage($params{page}), $config{destdir},
162 genrss($desc, $params{page}, @list));
163 $toping{$params{page}}=1 unless $config{rebuild};
164 $rsslinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
170 sub pagetemplate_inline (@) { #{{{
172 my $page=$params{page};
173 my $template=$params{template};
175 $template->param(rsslink => $rsslinks{$page})
176 if exists $rsslinks{$page} && $template->query(name => "rsslink");
179 sub get_inline_content ($$) { #{{{
183 my $file=$pagesources{$page};
184 my $type=pagetype($file);
186 return htmlize($page, $type,
187 linkify($page, $destpage,
188 preprocess($page, $destpage,
190 readfile(srcfile($file))))));
197 sub date_822 ($) { #{{{
201 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
202 POSIX::setlocale(&POSIX::LC_TIME, "C");
203 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
204 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
208 sub absolute_urls ($$) { #{{{
209 # sucky sub because rss sucks
215 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
216 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
220 sub rsspage ($) { #{{{
226 sub genrss ($$@) { #{{{
231 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
233 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
235 foreach my $p (@pages) {
236 next unless exists $renderedfiles{$p};
238 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
240 $itemtemplate->param(
241 title => pagetitle(basename($p)),
244 pubdate => date_822($pagectime{$p}),
245 content => absolute_urls(get_inline_content($p, $page), $url),
247 run_hooks(pagetemplate => sub {
248 shift->(page => $p, destpage => $page,
249 template => $itemtemplate);
252 $content.=$itemtemplate->output;
253 $itemtemplate->clear_params;
256 my $template=template("rsspage.tmpl", blind_cache => 1);
258 title => $config{wikiname},
259 wikiname => $config{wikiname},
264 run_hooks(pagetemplate => sub {
265 shift->(page => $page, destpage => $page,
266 template => $template);
269 return $template->output;
272 sub pingurl (@) { #{{{
273 return unless $config{pingurl} && %toping;
275 eval q{require RPC::XML::Client};
277 debug("RPC::XML::Client not found, not pinging");
281 foreach my $page (keys %toping) {
282 my $title=pagetitle(basename($page));
283 my $url="$config{url}/".htmlpage($page);
284 foreach my $pingurl (@{$config{pingurl}}) {
285 my $client = RPC::XML::Client->new($pingurl);
286 my $req = RPC::XML::request->new('weblogUpdates.ping',
288 debug("Pinging $pingurl for $page");
289 my $res = $client->send_request($req);
291 debug("Did not receive response to ping");
294 if (! exists $r->{flerror} || $r->{flerror}) {
295 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));