2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
11 IkiWiki::hook(type => "preprocess", id => "inline",
12 call => \&IkiWiki::preprocess_inline);
13 # Hook to change to do pinging since it's called late.
14 # This ensures each page only pings once and prevents slow
15 # pings interrupting page builds.
16 IkiWiki::hook(type => "change", id => "inline",
17 call => \&IkiWiki::pingurl);
20 # Back to ikiwiki namespace for the rest, this code is very much
21 # internal to ikiwiki even though it's separated into a plugin.
28 return (defined $val && lc($val) eq "yes");
31 sub preprocess_inline (@) { #{{{
34 if (! exists $params{pages}) {
37 my $raw=yesno($params{raw});
38 my $archive=yesno($params{archive});
39 my $rss=exists $params{rss} ? yesno($params{rss}) : 1;
40 if (! exists $params{show} && ! $archive) {
44 if (exists $params{description}) {
45 $desc = $params{description}
47 $desc = $config{wikiname};
51 foreach my $page (keys %pagesources) {
52 next if $page eq $params{page};
53 if (pagespec_match($page, $params{pages})) {
57 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
58 if ($params{show} && @list > $params{show}) {
59 @list=@list[0..$params{show} - 1];
62 add_depends($params{page}, $params{pages});
66 if (exists $params{rootpage} && $config{cgiurl}) {
67 # Add a blog post form, with a rss link button.
68 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
69 $formtemplate->param(cgiurl => $config{cgiurl});
70 $formtemplate->param(rootpage => $params{rootpage});
72 $formtemplate->param(rssurl => rsspage(basename($params{page})));
74 $ret.=$formtemplate->output;
76 elsif ($config{rss} && $rss) {
77 # Add a rss link button.
78 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
79 $linktemplate->param(rssurl => rsspage(basename($params{page})));
80 $ret.=$linktemplate->output;
83 my $template=template(
84 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
88 foreach my $page (@list) {
90 # Get the content before populating the template,
91 # since getting the content uses the same template
92 # if inlines are nested.
93 # TODO: if $archive=1, the only reason to do this
94 # is to let the meta plugin get page title info; so stop
95 # calling this next line then once the meta plugin can
96 # store that accross runs (also tags plugin).
97 my $content=get_inline_content($page, $params{page});
98 # Don't use htmllink because this way the title is separate
99 # and can be overridden by other plugins.
100 my $link=htmlpage(bestlink($params{page}, $page));
101 $link=abs2rel($link, dirname($params{page}));
102 $template->param(pageurl => $link);
103 $template->param(title => pagetitle(basename($page)));
104 $template->param(content => $content);
105 $template->param(ctime => displaytime($pagectime{$page}));
107 run_hooks(pagetemplate => sub {
108 shift->(page => $page, destpage => $params{page},
109 template => $template,);
112 $ret.=$template->output;
113 $template->clear_params;
116 my $file=$pagesources{$page};
117 my $type=pagetype($file);
120 linkify($page, $params{page},
121 preprocess($page, $params{page},
123 readfile(srcfile($file)))));
128 # TODO: should really add this to renderedfiles and call
129 # check_overwrite, but currently renderedfiles
130 # only supports listing one file per page.
131 if ($config{rss} && $rss) {
132 writefile(rsspage($params{page}), $config{destdir},
133 genrss($desc, $params{page}, @list));
134 $toping{$params{page}}=1 unless $config{rebuild};
140 sub get_inline_content ($$) { #{{{
144 my $file=$pagesources{$page};
145 my $type=pagetype($file);
147 return htmlize($type,
148 linkify($page, $destpage,
149 preprocess($page, $destpage,
151 readfile(srcfile($file))))));
158 sub date_822 ($) { #{{{
162 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
163 POSIX::setlocale(&POSIX::LC_TIME, "C");
164 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
165 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
169 sub absolute_urls ($$) { #{{{
170 # sucky sub because rss sucks
176 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
177 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
181 sub rsspage ($) { #{{{
187 sub genrss ($$@) { #{{{
192 my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
194 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
196 foreach my $p (@pages) {
197 next unless exists $renderedfiles{$p};
199 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
201 $itemtemplate->param(
202 title => pagetitle(basename($p)),
205 pubdate => date_822($pagectime{$p}),
206 content => absolute_urls(get_inline_content($p, $page), $url),
208 run_hooks(pagetemplate => sub {
209 shift->(page => $p, destpage => $page,
210 template => $itemtemplate);
213 $content.=$itemtemplate->output;
214 $itemtemplate->clear_params;
217 my $template=template("rsspage.tmpl", blind_cache => 1);
219 title => $config{wikiname},
220 wikiname => $config{wikiname},
225 run_hooks(pagetemplate => sub {
226 shift->(page => $page, destpage => $page,
227 template => $template);
230 return $template->output;
233 sub pingurl (@) { #{{{
234 return unless $config{pingurl} && %toping;
236 eval q{require RPC::XML::Client};
238 debug("RPC::XML::Client not found, not pinging");
242 foreach my $page (keys %toping) {
243 my $title=pagetitle(basename($page));
244 my $url="$config{url}/".htmlpage($page);
245 foreach my $pingurl (@{$config{pingurl}}) {
246 my $client = RPC::XML::Client->new($pingurl);
247 my $req = RPC::XML::request->new('weblogUpdates.ping',
249 debug("Pinging $pingurl for $page");
250 my $res = $client->send_request($req);
252 debug("Did not receive response to ping");
255 if (! exists $r->{flerror} || $r->{flerror}) {
256 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));