* Put categories in rss feeds for tagged items.
[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
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                 die_on_bad_params => 0);
157         
158         my @items;
159         foreach my $p (@pages) {
160                 push @items, {
161                         itemtitle => pagetitle(basename($p)),
162                         itemurl => "$config{url}/$renderedfiles{$p}",
163                         itempubdate => date_822($pagectime{$p}),
164                         itemcontent => absolute_urls(get_inline_content($p, $page), $url),
165                         page => $p, # used by category adding code in tag plugin
166                 } if exists $renderedfiles{$p};
167         }
168
169         $template->param(
170                 title => $config{wikiname},
171                 pageurl => $url,
172                 items => \@items,
173         );
174         
175         foreach my $id (keys %{$hooks{pagetemplate}}) {
176                 $hooks{pagetemplate}{$id}{call}->(
177                         page => $page,
178                         destpage => $page,
179                         template => $template,
180                 );
181         }
182         
183         return $template->output;
184 } #}}}
185
186 sub pingurl (@) { #{{{
187         return unless $config{pingurl} && %toping;
188
189         eval q{require RPC::XML::Client};
190         if ($@) {
191                 debug("RPC::XML::Client not found, not pinging");
192                 return;
193         }
194
195         foreach my $page (keys %toping) {
196                 my $title=pagetitle(basename($page));
197                 my $url="$config{url}/".htmlpage($page);
198                 foreach my $pingurl (@{$config{pingurl}}) {
199                         my $client = RPC::XML::Client->new($pingurl);
200                         my $req = RPC::XML::request->new('weblogUpdates.ping',
201                                 $title, $url);
202                         debug("Pinging $pingurl for $page");
203                         my $res = $client->send_request($req);
204                         if (! ref $res) {
205                                 debug("Did not receive response to ping");
206                         }
207                         my $r=$res->value;
208                         if (! exists $r->{flerror} || $r->{flerror}) {
209                                 debug("Ping rejected: ".$r->{message});
210                         }
211                 }
212         }
213 } #}}}
214
215 1