]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
749e39fb63524b1065a8bcc04446e5b0497be702
[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($params{page}, $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}->($page, $template);
87                         }
88                 }
89
90                 $ret.=$template->output;
91                 $template->clear_params;
92         }
93         
94         # TODO: should really add this to renderedfiles and call
95         # check_overwrite, but currently renderedfiles
96         # only supports listing one file per page.
97         if ($config{rss}) {
98                 writefile(rsspage($params{page}), $config{destdir},
99                         genrss($params{page}, @list));
100                 $toping{$params{page}}=1 unless $config{rebuild};
101         }
102         
103         return $ret;
104 } #}}}
105
106 sub get_inline_content ($$) { #{{{
107         my $parentpage=shift;
108         my $page=shift;
109         
110         my $file=$pagesources{$page};
111         my $type=pagetype($file);
112         if (defined $type) {
113                 return htmlize($type, preprocess($page, linkify($page, $parentpage, readfile(srcfile($file))), 1));
114         }
115         else {
116                 return "";
117         }
118 } #}}}
119
120 sub date_822 ($) { #{{{
121         my $time=shift;
122
123         eval q{use POSIX};
124         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
125 } #}}}
126
127 sub absolute_urls ($$) { #{{{
128         # sucky sub because rss sucks
129         my $content=shift;
130         my $url=shift;
131
132         $url=~s/[^\/]+$//;
133         
134         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
135         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
136         return $content;
137 } #}}}
138
139 sub rsspage ($) { #{{{
140         my $page=shift;
141
142         return $page.".rss";
143 } #}}}
144
145 sub genrss ($@) { #{{{
146         my $page=shift;
147         my @pages=@_;
148         
149         my $url="$config{url}/".htmlpage($page);
150         
151         my $template=template("rsspage.tmpl", blind_cache => 1);
152         
153         my @items;
154         foreach my $p (@pages) {
155                 push @items, {
156                         itemtitle => pagetitle(basename($p)),
157                         itemurl => "$config{url}/$renderedfiles{$p}",
158                         itempubdate => date_822($pagectime{$p}),
159                         itemcontent => absolute_urls(get_inline_content($page, $p), $url),
160                 } if exists $renderedfiles{$p};
161         }
162
163         $template->param(
164                 title => $config{wikiname},
165                 pageurl => $url,
166                 items => \@items,
167         );
168         
169         return $template->output;
170 } #}}}
171
172 sub pingurl (@) { #{{{
173         return unless $config{pingurl} && %toping;
174
175         eval q{require RPC::XML::Client};
176         if ($@) {
177                 debug("RPC::XML::Client not found, not pinging");
178                 return;
179         }
180
181         foreach my $page (keys %toping) {
182                 my $title=pagetitle(basename($page));
183                 my $url="$config{url}/".htmlpage($page);
184                 foreach my $pingurl (@{$config{pingurl}}) {
185                         my $client = RPC::XML::Client->new($pingurl);
186                         my $req = RPC::XML::request->new('weblogUpdates.ping',
187                                 $title, $url);
188                         debug("Pinging $pingurl for $page");
189                         my $res = $client->send_request($req);
190                         if (! ref $res) {
191                                 debug("Did not receive response to ping");
192                         }
193                         my $r=$res->value;
194                         if (! exists $r->{flerror} || $r->{flerror}) {
195                                 debug("Ping rejected: ".$r->{message});
196                         }
197                 }
198         }
199 } #}}}
200
201 1