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};
49 my $actions=yesno($params{actions});
52 foreach my $page (keys %pagesources) {
53 next if $page eq $params{page};
54 if (pagespec_match($page, $params{pages})) {
58 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
59 if ($params{show} && @list > $params{show}) {
60 @list=@list[0..$params{show} - 1];
63 add_depends($params{page}, $params{pages});
67 if (exists $params{rootpage} && $config{cgiurl}) {
68 # Add a blog post form, with a rss link button.
69 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
70 $formtemplate->param(cgiurl => $config{cgiurl});
71 $formtemplate->param(rootpage => $params{rootpage});
73 $formtemplate->param(rssurl => rsspage(basename($params{page})));
75 $ret.=$formtemplate->output;
77 elsif ($config{rss} && $rss) {
78 # Add a rss link button.
79 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
80 $linktemplate->param(rssurl => rsspage(basename($params{page})));
81 $ret.=$linktemplate->output;
84 my $template=template(
85 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
89 foreach my $page (@list) {
91 # Get the content before populating the template,
92 # since getting the content uses the same template
93 # if inlines are nested.
94 # TODO: if $archive=1, the only reason to do this
95 # is to let the meta plugin get page title info; so stop
96 # calling this next line then once the meta plugin can
97 # store that accross runs (also tags plugin).
98 my $content=get_inline_content($page, $params{destpage});
99 # Don't use htmllink because this way the title is separate
100 # and can be overridden by other plugins.
101 my $link=htmlpage(bestlink($params{page}, $page));
102 $link=abs2rel($link, dirname($params{destpage}));
103 $template->param(pageurl => $link);
104 $template->param(title => pagetitle(basename($page)));
105 $template->param(content => $content);
106 $template->param(ctime => displaytime($pagectime{$page}));
109 my $file = $pagesources{$page};
110 my $type = pagetype($file);
111 if ($config{discussion}) {
112 $template->param(have_actions => 1);
113 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
115 if (length $config{cgiurl} && defined $type) {
116 $template->param(have_actions => 1);
117 $template->param(editurl => cgiurl(do => "edit", page => $page));
121 run_hooks(pagetemplate => sub {
122 shift->(page => $page, destpage => $params{page},
123 template => $template,);
126 $ret.=$template->output;
127 $template->clear_params;
130 my $file=$pagesources{$page};
131 my $type=pagetype($file);
134 linkify($page, $params{page},
135 preprocess($page, $params{page},
137 readfile(srcfile($file)))));
142 # TODO: should really add this to renderedfiles and call
143 # check_overwrite, but currently renderedfiles
144 # only supports listing one file per page.
145 if ($config{rss} && $rss) {
146 writefile(rsspage($params{page}), $config{destdir},
147 genrss($desc, $params{page}, @list));
148 $toping{$params{page}}=1 unless $config{rebuild};
154 sub get_inline_content ($$) { #{{{
158 my $file=$pagesources{$page};
159 my $type=pagetype($file);
161 return htmlize($page, $type,
162 linkify($page, $destpage,
163 preprocess($page, $destpage,
165 readfile(srcfile($file))))));
172 sub date_822 ($) { #{{{
176 my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
177 POSIX::setlocale(&POSIX::LC_TIME, "C");
178 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
179 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
183 sub absolute_urls ($$) { #{{{
184 # sucky sub because rss sucks
190 $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
191 $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
195 sub rsspage ($) { #{{{
201 sub genrss ($$@) { #{{{
206 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
208 my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
210 foreach my $p (@pages) {
211 next unless exists $renderedfiles{$p};
213 my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
215 $itemtemplate->param(
216 title => pagetitle(basename($p)),
219 pubdate => date_822($pagectime{$p}),
220 content => absolute_urls(get_inline_content($p, $page), $url),
222 run_hooks(pagetemplate => sub {
223 shift->(page => $p, destpage => $page,
224 template => $itemtemplate);
227 $content.=$itemtemplate->output;
228 $itemtemplate->clear_params;
231 my $template=template("rsspage.tmpl", blind_cache => 1);
233 title => $config{wikiname},
234 wikiname => $config{wikiname},
239 run_hooks(pagetemplate => sub {
240 shift->(page => $page, destpage => $page,
241 template => $template);
244 return $template->output;
247 sub pingurl (@) { #{{{
248 return unless $config{pingurl} && %toping;
250 eval q{require RPC::XML::Client};
252 debug("RPC::XML::Client not found, not pinging");
256 foreach my $page (keys %toping) {
257 my $title=pagetitle(basename($page));
258 my $url="$config{url}/".htmlpage($page);
259 foreach my $pingurl (@{$config{pingurl}}) {
260 my $client = RPC::XML::Client->new($pingurl);
261 my $req = RPC::XML::request->new('weblogUpdates.ping',
263 debug("Pinging $pingurl for $page");
264 my $res = $client->send_request($req);
266 debug("Did not receive response to ping");
269 if (! exists $r->{flerror} || $r->{flerror}) {
270 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));