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 if ($config{rss} && $rss) {
158 will_render($params{page}, rsspage($params{page}));
159 writefile(rsspage($params{page}), $config{destdir},
160 genrss($desc, $params{page}, @list));
161 $toping{$params{page}}=1 unless $config{rebuild};
162 $rsslinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
168 sub pagetemplate_inline (@) { #{{{
170 my $page=$params{page};
171 my $template=$params{template};
173 $template->param(rsslink => $rsslinks{$page})
174 if exists $rsslinks{$page} && $template->query(name => "rsslink");
177 sub get_inline_content ($$) { #{{{
181 my $file=$pagesources{$page};
182 my $type=pagetype($file);
184 return htmlize($page, $type,
185 linkify($page, $destpage,
186 preprocess($page, $destpage,
188 readfile(srcfile($file))))));
195 sub date_822 ($) { #{{{
199 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
200 POSIX::setlocale(&POSIX::LC_TIME, "C");
201 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
202 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
206 sub absolute_urls ($$) { #{{{
207 # sucky sub because rss sucks
213 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
214 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
218 sub rsspage ($) { #{{{
224 sub genrss ($$@) { #{{{
229 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
231 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
233 foreach my $p (@pages) {
234 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
236 $itemtemplate->param(
237 title => pagetitle(basename($p)),
240 pubdate => date_822($pagectime{$p}),
241 content => absolute_urls(get_inline_content($p, $page), $url),
243 run_hooks(pagetemplate => sub {
244 shift->(page => $p, destpage => $page,
245 template => $itemtemplate);
248 $content.=$itemtemplate->output;
249 $itemtemplate->clear_params;
252 my $template=template("rsspage.tmpl", blind_cache => 1);
254 title => $config{wikiname},
255 wikiname => $config{wikiname},
260 run_hooks(pagetemplate => sub {
261 shift->(page => $page, destpage => $page,
262 template => $template);
265 return $template->output;
268 sub pingurl (@) { #{{{
269 return unless $config{pingurl} && %toping;
271 eval q{require RPC::XML::Client};
273 debug("RPC::XML::Client not found, not pinging");
277 foreach my $page (keys %toping) {
278 my $title=pagetitle(basename($page));
279 my $url="$config{url}/".htmlpage($page);
280 foreach my $pingurl (@{$config{pingurl}}) {
281 my $client = RPC::XML::Client->new($pingurl);
282 my $req = RPC::XML::request->new('weblogUpdates.ping',
284 debug("Pinging $pingurl for $page");
285 my $res = $client->send_request($req);
287 debug("Did not receive response to ping");
290 if (! exists $r->{flerror} || $r->{flerror}) {
291 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));