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