* Add a run_hooks function for the common task of running all hooks of a
[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 my $processing_inline=0;
25
26 sub preprocess_inline (@) { #{{{
27         my %params=@_;
28
29         if (! exists $params{pages}) {
30                 return "";
31         }
32         if (! exists $params{archive}) {
33                 $params{archive}="no";
34         }
35         if (! exists $params{show} && $params{archive} eq "no") {
36                 $params{show}=10;
37         }
38
39         # Avoid nested inlines, to avoid loops etc.
40         if ($processing_inline) {
41                 return "";
42         }
43         $processing_inline=1;
44
45         my @list;
46         foreach my $page (keys %pagesources) {
47                 next if $page eq $params{page};
48                 if (globlist_match($page, $params{pages})) {
49                         push @list, $page;
50                 }
51         }
52         @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
53         if ($params{show} && @list > $params{show}) {
54                 @list=@list[0..$params{show} - 1];
55         }
56
57         add_depends($params{page}, $params{pages});
58
59         my $ret="";
60         
61         if (exists $params{rootpage} && $config{cgiurl}) {
62                 # Add a blog post form, with a rss link button.
63                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
64                 $formtemplate->param(cgiurl => $config{cgiurl});
65                 $formtemplate->param(rootpage => $params{rootpage});
66                 if ($config{rss}) {
67                         $formtemplate->param(rssurl => rsspage(basename($params{page})));
68                 }
69                 $ret.=$formtemplate->output;
70         }
71         elsif ($config{rss}) {
72                 # Add a rss link button.
73                 my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
74                 $linktemplate->param(rssurl => rsspage(basename($params{page})));
75                 $ret.=$linktemplate->output;
76         }
77         
78         my $template=template(
79                 (($params{archive} eq "no")
80                         ? "inlinepage.tmpl"
81                         : "inlinepagetitle.tmpl"),
82                 blind_cache => 1,
83         );
84         
85         foreach my $page (@list) {
86                 $template->param(pagelink => htmllink($params{page}, $params{page}, $page));
87                 $template->param(content => get_inline_content($page, $params{page}))
88                         if $params{archive} eq "no";
89                 $template->param(ctime => displaytime($pagectime{$page}));
90
91                 run_hooks(pagetemplate => sub {
92                         shift->(page => $page, destpage => $params{page},
93                                 template => $template,);
94                 });
95
96                 $ret.=$template->output;
97                 $template->clear_params;
98         }
99         
100         # TODO: should really add this to renderedfiles and call
101         # check_overwrite, but currently renderedfiles
102         # only supports listing one file per page.
103         if ($config{rss}) {
104                 writefile(rsspage($params{page}), $config{destdir},
105                         genrss($params{page}, @list));
106                 $toping{$params{page}}=1 unless $config{rebuild};
107         }
108         
109         $processing_inline=0;
110
111         return $ret;
112 } #}}}
113
114 sub get_inline_content ($$) { #{{{
115         my $page=shift;
116         my $destpage=shift;
117         
118         my $file=$pagesources{$page};
119         my $type=pagetype($file);
120         if (defined $type) {
121                 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
122         }
123         else {
124                 return "";
125         }
126 } #}}}
127
128 sub date_822 ($) { #{{{
129         my $time=shift;
130
131         eval q{use POSIX};
132         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
133 } #}}}
134
135 sub absolute_urls ($$) { #{{{
136         # sucky sub because rss sucks
137         my $content=shift;
138         my $url=shift;
139
140         $url=~s/[^\/]+$//;
141         
142         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
143         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
144         return $content;
145 } #}}}
146
147 sub rsspage ($) { #{{{
148         my $page=shift;
149
150         return $page.".rss";
151 } #}}}
152
153 sub genrss ($@) { #{{{
154         my $page=shift;
155         my @pages=@_;
156         
157         my $url="$config{url}/".htmlpage($page);
158         
159         my $template=template("rsspage.tmpl", blind_cache => 1,
160                 die_on_bad_params => 0);
161         
162         my @items;
163         foreach my $p (@pages) {
164                 push @items, {
165                         itemtitle => pagetitle(basename($p)),
166                         itemurl => "$config{url}/$renderedfiles{$p}",
167                         itempubdate => date_822($pagectime{$p}),
168                         itemcontent => absolute_urls(get_inline_content($p, $page), $url),
169                         page => $p, # used by category adding code in tag plugin
170                 } if exists $renderedfiles{$p};
171         }
172
173         $template->param(
174                 title => $config{wikiname},
175                 pageurl => $url,
176                 items => \@items,
177         );
178         
179         run_hooks(pagetemplate => sub {
180                 shift->(page => $page, destpage => $page,
181                         template => $template);
182         });
183         
184         return $template->output;
185 } #}}}
186
187 sub pingurl (@) { #{{{
188         return unless $config{pingurl} && %toping;
189
190         eval q{require RPC::XML::Client};
191         if ($@) {
192                 debug("RPC::XML::Client not found, not pinging");
193                 return;
194         }
195
196         foreach my $page (keys %toping) {
197                 my $title=pagetitle(basename($page));
198                 my $url="$config{url}/".htmlpage($page);
199                 foreach my $pingurl (@{$config{pingurl}}) {
200                         my $client = RPC::XML::Client->new($pingurl);
201                         my $req = RPC::XML::request->new('weblogUpdates.ping',
202                                 $title, $url);
203                         debug("Pinging $pingurl for $page");
204                         my $res = $client->send_request($req);
205                         if (! ref $res) {
206                                 debug("Did not receive response to ping");
207                         }
208                         my $r=$res->value;
209                         if (! exists $r->{flerror} || $r->{flerror}) {
210                                 debug("Ping rejected: ".$r->{message});
211                         }
212                 }
213         }
214 } #}}}
215
216 1