* Switch pagetemplate hooks to using named parameters.
[ikiwiki] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 sub import { #{{{
10         IkiWiki::hook(type => "preprocess", id => "inline", 
11                 call => \&IkiWiki::preprocess_inline);
12         # Hook to change to do pinging since it's called late.
13         # This ensures each page only pings once and prevents slow
14         # pings interrupting page builds.
15         IkiWiki::hook(type => "change", id => "inline", 
16                 call => \&IkiWiki::pingurl);
17 } # }}}
18
19 # Back to ikiwiki namespace for the rest, this code is very much
20 # internal to ikiwiki even though it's separated into a plugin.
21 package IkiWiki;
22
23 my %toping;
24
25 sub preprocess_inline (@) { #{{{
26         my %params=@_;
27
28         if (! exists $params{pages}) {
29                 return "";
30         }
31         if (! exists $params{archive}) {
32                 $params{archive}="no";
33         }
34         if (! exists $params{show} && $params{archive} eq "no") {
35                 $params{show}=10;
36         }
37
38         my @list;
39         foreach my $page (keys %pagesources) {
40                 next if $page eq $params{page};
41                 if (globlist_match($page, $params{pages})) {
42                         push @list, $page;
43                 }
44         }
45         @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
46         if ($params{show} && @list > $params{show}) {
47                 @list=@list[0..$params{show} - 1];
48         }
49
50         add_depends($params{page}, $params{pages});
51
52         my $ret="";
53         
54         if (exists $params{rootpage}) {
55                 # Add a blog post form, with a rss link button.
56                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
57                 $formtemplate->param(cgiurl => $config{cgiurl});
58                 $formtemplate->param(rootpage => $params{rootpage});
59                 if ($config{rss}) {
60                         $formtemplate->param(rssurl => rsspage(basename($params{page})));
61                 }
62                 $ret.=$formtemplate->output;
63         }
64         elsif ($config{rss}) {
65                 # Add a rss link button.
66                 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
67                 $linktemplate->param(rssurl => rsspage(basename($params{page})));
68                 $ret.=$linktemplate->output;
69         }
70         
71         my $template=template(
72                 (($params{archive} eq "no")
73                         ? "inlinepage.tmpl"
74                         : "inlinepagetitle.tmpl"),
75                 blind_cache => 1,
76         );
77         
78         foreach my $page (@list) {
79                 $template->param(pagelink => htmllink($params{page}, $params{page}, $page));
80                 $template->param(content => get_inline_content($page, $params{page}))
81                         if $params{archive} eq "no";
82                 $template->param(ctime => displaytime($pagectime{$page}));
83
84                 if (exists $hooks{pagetemplate}) {
85                         foreach my $id (keys %{$hooks{pagetemplate}}) {
86                                 $hooks{pagetemplate}{$id}{call}->(
87                                         page => $page,
88                                         destpage => $params{page},
89                                         template => $template,
90                                 );
91                         }
92                 }
93
94                 $ret.=$template->output;
95                 $template->clear_params;
96         }
97         
98         # TODO: should really add this to renderedfiles and call
99         # check_overwrite, but currently renderedfiles
100         # only supports listing one file per page.
101         if ($config{rss}) {
102                 writefile(rsspage($params{page}), $config{destdir},
103                         genrss($params{page}, @list));
104                 $toping{$params{page}}=1 unless $config{rebuild};
105         }
106         
107         return $ret;
108 } #}}}
109
110 sub get_inline_content ($$) { #{{{
111         my $page=shift;
112         my $destpage=shift;
113         
114         my $file=$pagesources{$page};
115         my $type=pagetype($file);
116         if (defined $type) {
117                 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file))), 1));
118         }
119         else {
120                 return "";
121         }
122 } #}}}
123
124 sub date_822 ($) { #{{{
125         my $time=shift;
126
127         eval q{use POSIX};
128         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
129 } #}}}
130
131 sub absolute_urls ($$) { #{{{
132         # sucky sub because rss sucks
133         my $content=shift;
134         my $url=shift;
135
136         $url=~s/[^\/]+$//;
137         
138         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
139         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
140         return $content;
141 } #}}}
142
143 sub rsspage ($) { #{{{
144         my $page=shift;
145
146         return $page.".rss";
147 } #}}}
148
149 sub genrss ($@) { #{{{
150         my $page=shift;
151         my @pages=@_;
152         
153         my $url="$config{url}/".htmlpage($page);
154         
155         my $template=template("rsspage.tmpl", blind_cache => 1);
156         
157         my @items;
158         foreach my $p (@pages) {
159                 push @items, {
160                         itemtitle => pagetitle(basename($p)),
161                         itemurl => "$config{url}/$renderedfiles{$p}",
162                         itempubdate => date_822($pagectime{$p}),
163                         itemcontent => absolute_urls(get_inline_content($p, $page), $url),
164                 } if exists $renderedfiles{$p};
165         }
166
167         $template->param(
168                 title => $config{wikiname},
169                 pageurl => $url,
170                 items => \@items,
171         );
172         
173         return $template->output;
174 } #}}}
175
176 sub pingurl (@) { #{{{
177         return unless $config{pingurl} && %toping;
178
179         eval q{require RPC::XML::Client};
180         if ($@) {
181                 debug("RPC::XML::Client not found, not pinging");
182                 return;
183         }
184
185         foreach my $page (keys %toping) {
186                 my $title=pagetitle(basename($page));
187                 my $url="$config{url}/".htmlpage($page);
188                 foreach my $pingurl (@{$config{pingurl}}) {
189                         my $client = RPC::XML::Client->new($pingurl);
190                         my $req = RPC::XML::request->new('weblogUpdates.ping',
191                                 $title, $url);
192                         debug("Pinging $pingurl for $page");
193                         my $res = $client->send_request($req);
194                         if (! ref $res) {
195                                 debug("Did not receive response to ping");
196                         }
197                         my $r=$res->value;
198                         if (! exists $r->{flerror} || $r->{flerror}) {
199                                 debug("Ping rejected: ".$r->{message});
200                         }
201                 }
202         }
203 } #}}}
204
205 1