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