fix title metadata on blogs, reorg needed to do it, simplified tag some
[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
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                 # Don't use htmllink because this way the title is separate
87                 # and can be overridden by other plugins.
88                 my $link=htmlpage(bestlink($params{page}, $page));
89                 $link=abs2rel($link, dirname($params{page}));
90                 $template->param(pageurl => $link);
91                 $template->param(title => pagetitle(basename($page)));
92                 $template->param(content => get_inline_content($page, $params{page}))
93                         if $params{archive} eq "no";
94                 $template->param(ctime => displaytime($pagectime{$page}));
95
96                 run_hooks(pagetemplate => sub {
97                         shift->(page => $page, destpage => $params{page},
98                                 template => $template,);
99                 });
100
101                 $ret.=$template->output;
102                 $template->clear_params;
103         }
104         
105         # TODO: should really add this to renderedfiles and call
106         # check_overwrite, but currently renderedfiles
107         # only supports listing one file per page.
108         if ($config{rss}) {
109                 writefile(rsspage($params{page}), $config{destdir},
110                         genrss($params{page}, @list));
111                 $toping{$params{page}}=1 unless $config{rebuild};
112         }
113         
114         $processing_inline=0;
115
116         return $ret;
117 } #}}}
118
119 sub get_inline_content ($$) { #{{{
120         my $page=shift;
121         my $destpage=shift;
122         
123         my $file=$pagesources{$page};
124         my $type=pagetype($file);
125         if (defined $type) {
126                 return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
127         }
128         else {
129                 return "";
130         }
131 } #}}}
132
133 sub date_822 ($) { #{{{
134         my $time=shift;
135
136         eval q{use POSIX};
137         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
138 } #}}}
139
140 sub absolute_urls ($$) { #{{{
141         # sucky sub because rss sucks
142         my $content=shift;
143         my $url=shift;
144
145         $url=~s/[^\/]+$//;
146         
147         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
148         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
149         return $content;
150 } #}}}
151
152 sub rsspage ($) { #{{{
153         my $page=shift;
154
155         return $page.".rss";
156 } #}}}
157
158 sub genrss ($@) { #{{{
159         my $page=shift;
160         my @pages=@_;
161         
162         my $url="$config{url}/".htmlpage($page);
163         
164         my $itemtemplate=template("rssitem.tmpl", blind_cache => 1, 
165                 die_on_bad_params => 0);
166         my $content="";
167         foreach my $p (@pages) {
168                 next unless exists $renderedfiles{$p};
169
170                 $itemtemplate->param(
171                         title => pagetitle(basename($p)),
172                         url => "$config{url}/$renderedfiles{$p}",
173                         pubdate => date_822($pagectime{$p}),
174                         content => absolute_urls(get_inline_content($p, $page), $url),
175                 );
176                 run_hooks(pagetemplate => sub {
177                         shift->(page => $p, destpage => $page,
178                                 template => $itemtemplate);
179                 });
180                 $content.=$itemtemplate->output;
181                 $itemtemplate->clear_params;
182         }
183
184         my $template=template("rsspage.tmpl", blind_cache => 1);
185         $template->param(
186                 title => $config{wikiname},
187                 wikiname => $config{wikiname},
188                 pageurl => $url,
189                 content => $content,
190         );
191         
192         run_hooks(pagetemplate => sub {
193                 shift->(page => $page, destpage => $page,
194                         template => $template);
195         });
196         
197         return $template->output;
198 } #}}}
199
200 sub pingurl (@) { #{{{
201         return unless $config{pingurl} && %toping;
202
203         eval q{require RPC::XML::Client};
204         if ($@) {
205                 debug("RPC::XML::Client not found, not pinging");
206                 return;
207         }
208
209         foreach my $page (keys %toping) {
210                 my $title=pagetitle(basename($page));
211                 my $url="$config{url}/".htmlpage($page);
212                 foreach my $pingurl (@{$config{pingurl}}) {
213                         my $client = RPC::XML::Client->new($pingurl);
214                         my $req = RPC::XML::request->new('weblogUpdates.ping',
215                                 $title, $url);
216                         debug("Pinging $pingurl for $page");
217                         my $res = $client->send_request($req);
218                         if (! ref $res) {
219                                 debug("Did not receive response to ping");
220                         }
221                         my $r=$res->value;
222                         if (! exists $r->{flerror} || $r->{flerror}) {
223                                 debug("Ping rejected: ".$r->{message});
224                         }
225                 }
226         }
227 } #}}}
228
229 1