Removed the rss feeds for every page, which wasn't really entirely useful.
[ikiwiki] / IkiWiki / Render.pm
1 package IkiWiki;
2
3 use warnings;
4 use strict;
5 use File::Spec;
6
7 sub linkify ($$) { #{{{
8         my $content=shift;
9         my $page=shift;
10
11         $content =~ s{(\\?)$config{wiki_link_regexp}}{
12                 $1 ? "[[$2]]" : htmllink($page, $2)
13         }eg;
14         
15         return $content;
16 } #}}}
17
18 sub htmlize ($$) { #{{{
19         my $type=shift;
20         my $content=shift;
21         
22         if (! $INC{"/usr/bin/markdown"}) {
23                 no warnings 'once';
24                 $blosxom::version="is a proper perl module too much to ask?";
25                 use warnings 'all';
26                 do "/usr/bin/markdown";
27         }
28         
29         if ($type eq '.mdwn') {
30                 return Markdown::Markdown($content);
31         }
32         else {
33                 error("htmlization of $type not supported");
34         }
35 } #}}}
36
37 sub backlinks ($) { #{{{
38         my $page=shift;
39
40         my @links;
41         foreach my $p (keys %links) {
42                 next if bestlink($page, $p) eq $page;
43                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
44                         my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
45                         
46                         # Trim common dir prefixes from both pages.
47                         my $p_trimmed=$p;
48                         my $page_trimmed=$page;
49                         my $dir;
50                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
51                                 defined $dir &&
52                                 $p_trimmed=~s/^\Q$dir\E// &&
53                                 $page_trimmed=~s/^\Q$dir\E//;
54                                        
55                         push @links, { url => $href, page => $p_trimmed };
56                 }
57         }
58
59         return sort { $a->{page} cmp $b->{page} } @links;
60 } #}}}
61
62 sub parentlinks ($) { #{{{
63         my $page=shift;
64         
65         my @ret;
66         my $pagelink="";
67         my $path="";
68         my $skip=1;
69         foreach my $dir (reverse split("/", $page)) {
70                 if (! $skip) {
71                         $path.="../";
72                         unshift @ret, { url => "$path$dir.html", page => $dir };
73                 }
74                 else {
75                         $skip=0;
76                 }
77         }
78         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
79         return @ret;
80 } #}}}
81
82 sub rsspage ($) { #{{{
83         my $page=shift;
84
85         return $page.".rss";
86 } #}}}
87
88 sub postprocess { #{{{
89         # Takes content to postprocess followed by a list of postprocessor
90         # commands and subroutine references to run for the commands.
91         my $page=shift;
92         my $content=shift;
93         my %commands=@_;
94         
95         my $handle=sub {
96                 my $escape=shift;
97                 my $command=shift;
98                 my $params=shift;
99                 if (length $escape) {
100                         "[[$command $params]]";
101                 }
102                 elsif (exists $commands{$command}) {
103                         my %params;
104                         while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
105                                 $params{$1}=$2;
106                         }
107                         $commands{$command}->($page, %params);
108                 }
109                 else {
110                         "[[bad directive $command]]";
111                 }
112         };
113         
114         $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
115         return $content;
116 } #}}}
117
118 sub blog_list ($$) { #{{{
119         my $globlist=shift;
120         my $maxitems=shift;
121         
122         my @list;
123         foreach my $page (keys %pagesources) {
124                 if (globlist_match($page, $globlist)) {
125                         push @list, $page;
126                 }
127         }
128
129         @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
130         return @list if ! $maxitems || @list <= $maxitems;
131         return @list[0..$maxitems - 1];
132 } #}}}
133
134 sub get_inline_content ($$) { #{{{
135         my $parentpage=shift;
136         my $page=shift;
137         
138         my $file=$pagesources{$page};
139         my $type=pagetype($file);
140         if ($type ne 'unknown') {
141                 return htmlize($type, linkify(readfile("$config{srcdir}/$file"), $parentpage));
142         }
143         else {
144                 return "";
145         }
146 } #}}}
147
148 sub postprocess_html_inline { #{{{
149         my $parentpage=shift;
150         my %params=@_;
151         
152         if (! exists $params{pages}) {
153                 return "";
154         }
155         if (! exists $params{archive}) {
156                 $params{archive}="no";
157         }
158         if (! exists $params{show} && $params{archive} eq "no") {
159                 $params{show}=10;
160         }
161         $inlinepages{$parentpage}=$params{pages};
162         
163         my $template=HTML::Template->new(blind_cache => 1,
164                 filename => (($params{archive} eq "no") 
165                                 ? "$config{templatedir}/inlinepage.tmpl"
166                                 : "$config{templatedir}/inlinepagetitle.tmpl"));
167         
168         my $ret="";
169         foreach my $page (blog_list($params{pages}, $params{show})) {
170                 next if $page eq $parentpage;
171                 $template->param(pagelink => htmllink($parentpage, $page));
172                 $template->param(content => get_inline_content($parentpage, $page))
173                         if $params{archive} eq "no";
174                 $template->param(ctime => scalar(gmtime($pagectime{$page})));
175                 $ret.=$template->output;
176         }
177         
178         return $ret;
179 } #}}}
180
181 sub genpage ($$$) { #{{{
182         my $content=shift;
183         my $page=shift;
184         my $mtime=shift;
185
186         $content = postprocess($page, $content, inline => \&postprocess_html_inline);
187         
188         my $title=pagetitle(basename($page));
189         
190         my $template=HTML::Template->new(blind_cache => 1,
191                 filename => "$config{templatedir}/page.tmpl");
192         
193         if (length $config{cgiurl}) {
194                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
195                 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
196                 if ($config{rcs}) {
197                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
198                 }
199         }
200
201         if (length $config{historyurl}) {
202                 my $u=$config{historyurl};
203                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
204                 $template->param(historyurl => $u);
205         }
206
207         if ($config{rss} && $inlinepages{$page}) {
208                 $template->param(rssurl => rsspage($page));
209         }
210         
211         $template->param(
212                 title => $title,
213                 wikiname => $config{wikiname},
214                 parentlinks => [parentlinks($page)],
215                 content => $content,
216                 backlinks => [backlinks($page)],
217                 discussionlink => htmllink($page, "Discussion", 1, 1),
218                 mtime => scalar(gmtime($mtime)),
219         );
220         
221         return $template->output;
222 } #}}}
223
224 sub date_822 ($) { #{{{
225         my $time=shift;
226
227         eval q{use POSIX};
228         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
229 } #}}}
230
231 sub absolute_urls ($$) { #{{{
232         # sucky sub because rss sucks
233         my $content=shift;
234         my $url=shift;
235
236         $url=~s/[^\/]+$//;
237         
238         $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
239         $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
240         return $content;
241 } #}}}
242
243 sub genrss ($$$) { #{{{
244         my $content=shift;
245         my $page=shift;
246         my $mtime=shift;
247         
248         my $url="$config{url}/".htmlpage($page);
249         
250         my $template=HTML::Template->new(blind_cache => 1,
251                 filename => "$config{templatedir}/rsspage.tmpl");
252         
253         my @items;
254         my $isblog=0;
255         my $gen_blog=sub {
256                 my $parentpage=shift;
257                 my %params=@_;
258                 
259                 return "" if exists $params{archive} && $params{archive} eq 'yes';
260                 
261                 if (! exists $params{show}) {
262                         $params{show}=10;
263                 }
264                 if (! exists $params{pages}) {
265                         return "";
266                 }
267                 
268                 $isblog=1;
269                 foreach my $page (blog_list($params{pages}, $params{show})) {
270                         next if $page eq $parentpage;
271                         push @items, {
272                                 itemtitle => pagetitle(basename($page)),
273                                 itemurl => "$config{url}/$renderedfiles{$page}",
274                                 itempubdate => date_822($pagectime{$page}),
275                                 itemcontent => absolute_urls(get_inline_content($parentpage, $page), $url),
276                         } if exists $renderedfiles{$page};
277                 }
278                 
279                 return "";
280         };
281         
282         $content = postprocess($page, $content, inline => $gen_blog);
283
284         $template->param(
285                 title => $config{wikiname},
286                 pageurl => $url,
287                 items => \@items,
288         );
289         
290         return $template->output;
291 } #}}}
292
293 sub check_overwrite ($$) { #{{{
294         # Important security check. Make sure to call this before saving
295         # any files to the source directory.
296         my $dest=shift;
297         my $src=shift;
298         
299         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
300                 error("$dest already exists and was rendered from ".
301                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
302                                 %renderedfiles)).
303                         ", before, so not rendering from $src");
304         }
305 } #}}}
306
307 sub mtime ($) { #{{{
308         my $page=shift;
309         
310         return (stat($page))[9];
311 } #}}}
312
313 sub findlinks ($$) { #{{{
314         my $content=shift;
315         my $page=shift;
316
317         my @links;
318         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
319                 push @links, lc($1);
320         }
321         # Discussion links are a special case since they're not in the text
322         # of the page, but on its template.
323         return @links, "$page/discussion";
324 } #}}}
325
326 sub render ($) { #{{{
327         my $file=shift;
328         
329         my $type=pagetype($file);
330         my $content=readfile("$config{srcdir}/$file");
331         if ($type ne 'unknown') {
332                 my $page=pagename($file);
333                 
334                 $links{$page}=[findlinks($content, $page)];
335                 delete $inlinepages{$page};
336                 
337                 $content=linkify($content, $page);
338                 $content=htmlize($type, $content);
339                 
340                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
341                 writefile("$config{destdir}/".htmlpage($page),
342                         genpage($content, $page, mtime("$config{srcdir}/$file")));
343                 $oldpagemtime{$page}=time;
344                 $renderedfiles{$page}=htmlpage($page);
345
346                 # TODO: should really add this to renderedfiles and call
347                 # check_overwrite, as above, but currently renderedfiles
348                 # only supports listing one file per page.
349                 if ($config{rss} && exists $inlinepages{$page}) {
350                         writefile("$config{destdir}/".rsspage($page),
351                                 genrss($content, $page, mtime("$config{srcdir}/$file")));
352                 }
353         }
354         else {
355                 $links{$file}=[];
356                 check_overwrite("$config{destdir}/$file", $file);
357                 writefile("$config{destdir}/$file", $content);
358                 $oldpagemtime{$file}=time;
359                 $renderedfiles{$file}=$file;
360         }
361 } #}}}
362
363 sub prune ($) { #{{{
364         my $file=shift;
365
366         unlink($file);
367         my $dir=dirname($file);
368         while (rmdir($dir)) {
369                 $dir=dirname($dir);
370         }
371 } #}}}
372
373 sub refresh () { #{{{
374         # find existing pages
375         my %exists;
376         my @files;
377         eval q{use File::Find};
378         find({
379                 no_chdir => 1,
380                 wanted => sub {
381                         if (/$config{wiki_file_prune_regexp}/) {
382                                 no warnings 'once';
383                                 $File::Find::prune=1;
384                                 use warnings "all";
385                         }
386                         elsif (! -d $_ && ! -l $_) {
387                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
388                                 if (! defined $f) {
389                                         warn("skipping bad filename $_\n");
390                                 }
391                                 else {
392                                         $f=~s/^\Q$config{srcdir}\E\/?//;
393                                         push @files, $f;
394                                         $exists{pagename($f)}=1;
395                                 }
396                         }
397                 },
398         }, $config{srcdir});
399
400         my %rendered;
401
402         # check for added or removed pages
403         my @add;
404         foreach my $file (@files) {
405                 my $page=pagename($file);
406                 if (! $oldpagemtime{$page}) {
407                         debug("new page $page") unless exists $pagectime{$page};
408                         push @add, $file;
409                         $links{$page}=[];
410                         $pagesources{$page}=$file;
411                         $pagectime{$page}=time unless exists $pagectime{$page};
412                 }
413         }
414         my @del;
415         foreach my $page (keys %oldpagemtime) {
416                 if (! $exists{$page}) {
417                         debug("removing old page $page");
418                         push @del, $pagesources{$page};
419                         prune($config{destdir}."/".$renderedfiles{$page});
420                         delete $renderedfiles{$page};
421                         $oldpagemtime{$page}=0;
422                         delete $pagesources{$page};
423                 }
424         }
425         
426         # render any updated files
427         foreach my $file (@files) {
428                 my $page=pagename($file);
429                 
430                 if (! exists $oldpagemtime{$page} ||
431                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
432                         debug("rendering changed file $file");
433                         render($file);
434                         $rendered{$file}=1;
435                 }
436         }
437         
438         # if any files were added or removed, check to see if each page
439         # needs an update due to linking to them or inlining them.
440         # TODO: inefficient; pages may get rendered above and again here;
441         # problem is the bestlink may have changed and we won't know until
442         # now
443         if (@add || @del) {
444 FILE:           foreach my $file (@files) {
445                         my $page=pagename($file);
446                         foreach my $f (@add, @del) {
447                                 my $p=pagename($f);
448                                 foreach my $link (@{$links{$page}}) {
449                                         if (bestlink($page, $link) eq $p) {
450                                                 debug("rendering $file, which links to $p");
451                                                 render($file);
452                                                 $rendered{$file}=1;
453                                                 next FILE;
454                                         }
455                                 }
456                         }
457                 }
458         }
459
460         # Handle backlinks; if a page has added/removed links, update the
461         # pages it links to. Also handle inlining here.
462         # TODO: inefficient; pages may get rendered above and again here;
463         # problem is the backlinks could be wrong in the first pass render
464         # above
465         if (%rendered || @del) {
466                 my %linkchanged;
467                 foreach my $file (keys %rendered, @del) {
468                         my $page=pagename($file);
469                         
470                         foreach my $f (@files) {
471                                 my $p=pagename($f);
472                                 if (exists $inlinepages{$p} && 
473                                     globlist_match($page, $inlinepages{$p})) {
474                                         debug("rendering $f, which inlines $page");
475                                         render($f);
476                                         next;
477                                 }
478                         }
479                         
480                         if (exists $links{$page}) {
481                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
482                                         if (length $link &&
483                                             ! exists $oldlinks{$page} ||
484                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
485                                                 $linkchanged{$link}=1;
486                                         }
487                                 }
488                         }
489                         if (exists $oldlinks{$page}) {
490                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
491                                         if (length $link &&
492                                             ! exists $links{$page} ||
493                                             ! grep { $_ eq $link } @{$links{$page}}) {
494                                                 $linkchanged{$link}=1;
495                                         }
496                                 }
497                         }
498                 }
499                 foreach my $link (keys %linkchanged) {
500                         my $linkfile=$pagesources{$link};
501                         if (defined $linkfile) {
502                                 debug("rendering $linkfile, to update its backlinks");
503                                 render($linkfile);
504                         }
505                 }
506         }
507 } #}}}
508
509 1