* Added plugin system, currently only supporting for PreProcessorDirectives.
[ikiwiki] / 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
8 sub import { #{{{
9         IkiWiki::register_plugin("preprocess", "inline", \&IkiWiki::preprocess_inline);
10 } # }}}
11
12 # Back to ikiwiki namespace for the rest, this code is very much
13 # internal to ikiwiki even though it's separated into a plugin.
14 package IkiWiki;
15         
16 sub preprocess_inline (@) { #{{{
17         my %params=@_;
18
19         if (! exists $params{pages}) {
20                 return "";
21         }
22         if (! exists $params{archive}) {
23                 $params{archive}="no";
24         }
25         if (! exists $params{show} && $params{archive} eq "no") {
26                 $params{show}=10;
27         }
28         add_depends($params{page}, $params{pages});
29
30         my $ret="";
31         
32         if (exists $params{rootpage}) {
33                 # Add a blog post form, with a rss link button.
34                 my $formtemplate=HTML::Template->new(blind_cache => 1,
35                         filename => "$config{templatedir}/blogpost.tmpl");
36                 $formtemplate->param(cgiurl => $config{cgiurl});
37                 $formtemplate->param(rootpage => $params{rootpage});
38                 if ($config{rss}) {
39                         $formtemplate->param(rssurl => rsspage(basename($params{page})));
40                 }
41                 $ret.=$formtemplate->output;
42         }
43         elsif ($config{rss}) {
44                 # Add a rss link button.
45                 my $linktemplate=HTML::Template->new(blind_cache => 1,
46                         filename => "$config{templatedir}/rsslink.tmpl");
47                 $linktemplate->param(rssurl => rsspage(basename($params{page})));
48                 $ret.=$linktemplate->output;
49         }
50         
51         my $template=HTML::Template->new(blind_cache => 1,
52                 filename => (($params{archive} eq "no") 
53                                 ? "$config{templatedir}/inlinepage.tmpl"
54                                 : "$config{templatedir}/inlinepagetitle.tmpl"));
55         
56         my @pages;
57         foreach my $page (blog_list($params{pages}, $params{show})) {
58                 next if $page eq $params{page};
59                 push @pages, $page;
60                 $template->param(pagelink => htmllink($params{page}, $page));
61                 $template->param(content => get_inline_content($params{page}, $page))
62                         if $params{archive} eq "no";
63                 $template->param(ctime => scalar(gmtime($pagectime{$page})));
64                 $ret.=$template->output;
65         }
66         
67         # TODO: should really add this to renderedfiles and call
68         # check_overwrite, but currently renderedfiles
69         # only supports listing one file per page.
70         if ($config{rss}) {
71                 writefile(rsspage($params{page}), $config{destdir},
72                         genrss($params{page}, @pages));
73         }
74         
75         return $ret;
76 } #}}}
77
78 sub blog_list ($$) { #{{{
79         my $globlist=shift;
80         my $maxitems=shift;
81
82         my @list;
83         foreach my $page (keys %pagesources) {
84                 if (globlist_match($page, $globlist)) {
85                         push @list, $page;
86                 }
87         }
88
89         @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
90         return @list if ! $maxitems || @list <= $maxitems;
91         return @list[0..$maxitems - 1];
92 } #}}}
93
94 sub get_inline_content ($$) { #{{{
95         my $parentpage=shift;
96         my $page=shift;
97         
98         my $file=$pagesources{$page};
99         my $type=pagetype($file);
100         if ($type ne 'unknown') {
101                 return htmlize($type, linkify(readfile(srcfile($file)), $parentpage));
102         }
103         else {
104                 return "";
105         }
106 } #}}}
107
108 sub date_822 ($) { #{{{
109         my $time=shift;
110
111         eval q{use POSIX};
112         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
113 } #}}}
114
115 sub absolute_urls ($$) { #{{{
116         # sucky sub because rss sucks
117         my $content=shift;
118         my $url=shift;
119
120         $url=~s/[^\/]+$//;
121         
122         $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
123         $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
124         return $content;
125 } #}}}
126
127 sub rsspage ($) { #{{{
128         my $page=shift;
129
130         return $page.".rss";
131 } #}}}
132
133 sub genrss ($@) { #{{{
134         my $page=shift;
135         my @pages=@_;
136         
137         my $url="$config{url}/".htmlpage($page);
138         
139         my $template=HTML::Template->new(blind_cache => 1,
140                 filename => "$config{templatedir}/rsspage.tmpl");
141         
142         my @items;
143         foreach my $p (@pages) {
144                 push @items, {
145                         itemtitle => pagetitle(basename($p)),
146                         itemurl => "$config{url}/$renderedfiles{$p}",
147                         itempubdate => date_822($pagectime{$p}),
148                         itemcontent => absolute_urls(get_inline_content($page, $p), $url),
149                 } if exists $renderedfiles{$p};
150         }
151
152         $template->param(
153                 title => $config{wikiname},
154                 pageurl => $url,
155                 items => \@items,
156         );
157         
158         return $template->output;
159 } #}}}
160
161 1