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