There was a bug that [[inline archive="yes]] created an RSS link, but no feed.
[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 $ret="";
164         
165         if (exists $params{rootpage}) {
166                 my $formtemplate=HTML::Template->new(blind_cache => 1,
167                         filename => "$config{templatedir}/blogpost.tmpl");
168                 $formtemplate->param(cgiurl => $config{cgiurl});
169                 $formtemplate->param(rootpage => $params{rootpage});
170                 my $form=$formtemplate->output;
171                 $ret.=$form;
172         }
173         
174         my $template=HTML::Template->new(blind_cache => 1,
175                 filename => (($params{archive} eq "no") 
176                                 ? "$config{templatedir}/inlinepage.tmpl"
177                                 : "$config{templatedir}/inlinepagetitle.tmpl"));
178         
179         foreach my $page (blog_list($params{pages}, $params{show})) {
180                 next if $page eq $parentpage;
181                 $template->param(pagelink => htmllink($parentpage, $page));
182                 $template->param(content => get_inline_content($parentpage, $page))
183                         if $params{archive} eq "no";
184                 $template->param(ctime => scalar(gmtime($pagectime{$page})));
185                 $ret.=$template->output;
186         }
187         
188         return $ret;
189 } #}}}
190
191 sub genpage ($$$) { #{{{
192         my $content=shift;
193         my $page=shift;
194         my $mtime=shift;
195
196         $content = postprocess($page, $content, inline => \&postprocess_html_inline);
197         
198         my $title=pagetitle(basename($page));
199         
200         my $template=HTML::Template->new(blind_cache => 1,
201                 filename => "$config{templatedir}/page.tmpl");
202         
203         if (length $config{cgiurl}) {
204                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
205                 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
206                 if ($config{rcs}) {
207                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
208                 }
209         }
210
211         if (length $config{historyurl}) {
212                 my $u=$config{historyurl};
213                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
214                 $template->param(historyurl => $u);
215         }
216
217         if ($config{rss} && $inlinepages{$page}) {
218                 $template->param(rssurl => rsspage(basename($page)));
219         }
220         
221         $template->param(
222                 title => $title,
223                 wikiname => $config{wikiname},
224                 parentlinks => [parentlinks($page)],
225                 content => $content,
226                 backlinks => [backlinks($page)],
227                 discussionlink => htmllink($page, "Discussion", 1, 1),
228                 mtime => scalar(gmtime($mtime)),
229         );
230         
231         return $template->output;
232 } #}}}
233
234 sub date_822 ($) { #{{{
235         my $time=shift;
236
237         eval q{use POSIX};
238         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
239 } #}}}
240
241 sub absolute_urls ($$) { #{{{
242         # sucky sub because rss sucks
243         my $content=shift;
244         my $url=shift;
245
246         $url=~s/[^\/]+$//;
247         
248         $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
249         $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
250         return $content;
251 } #}}}
252
253 sub genrss ($$$) { #{{{
254         my $content=shift;
255         my $page=shift;
256         my $mtime=shift;
257         
258         my $url="$config{url}/".htmlpage($page);
259         
260         my $template=HTML::Template->new(blind_cache => 1,
261                 filename => "$config{templatedir}/rsspage.tmpl");
262         
263         my @items;
264         my $isblog=0;
265         my $gen_blog=sub {
266                 my $parentpage=shift;
267                 my %params=@_;
268                 
269                 if (! exists $params{show}) {
270                         $params{show}=10;
271                 }
272                 if (! exists $params{pages}) {
273                         return "";
274                 }
275                 
276                 $isblog=1;
277                 foreach my $page (blog_list($params{pages}, $params{show})) {
278                         next if $page eq $parentpage;
279                         push @items, {
280                                 itemtitle => pagetitle(basename($page)),
281                                 itemurl => "$config{url}/$renderedfiles{$page}",
282                                 itempubdate => date_822($pagectime{$page}),
283                                 itemcontent => absolute_urls(get_inline_content($parentpage, $page), $url),
284                         } if exists $renderedfiles{$page};
285                 }
286                 
287                 return "";
288         };
289         
290         $content = postprocess($page, $content, inline => $gen_blog);
291
292         $template->param(
293                 title => $config{wikiname},
294                 pageurl => $url,
295                 items => \@items,
296         );
297         
298         return $template->output;
299 } #}}}
300
301 sub check_overwrite ($$) { #{{{
302         # Important security check. Make sure to call this before saving
303         # any files to the source directory.
304         my $dest=shift;
305         my $src=shift;
306         
307         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
308                 error("$dest already exists and was rendered from ".
309                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
310                                 %renderedfiles)).
311                         ", before, so not rendering from $src");
312         }
313 } #}}}
314
315 sub mtime ($) { #{{{
316         my $file=shift;
317         
318         return (stat($file))[9];
319 } #}}}
320
321 sub findlinks ($$) { #{{{
322         my $content=shift;
323         my $page=shift;
324
325         my @links;
326         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
327                 push @links, lc($1);
328         }
329         # Discussion links are a special case since they're not in the text
330         # of the page, but on its template.
331         return @links, "$page/discussion";
332 } #}}}
333
334 sub render ($) { #{{{
335         my $file=shift;
336         
337         my $type=pagetype($file);
338         my $content=readfile("$config{srcdir}/$file");
339         if ($type ne 'unknown') {
340                 my $page=pagename($file);
341                 
342                 $links{$page}=[findlinks($content, $page)];
343                 delete $inlinepages{$page};
344                 
345                 $content=linkify($content, $page);
346                 $content=htmlize($type, $content);
347                 
348                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
349                 writefile("$config{destdir}/".htmlpage($page),
350                         genpage($content, $page, mtime("$config{srcdir}/$file")));
351                 $oldpagemtime{$page}=time;
352                 $renderedfiles{$page}=htmlpage($page);
353
354                 # TODO: should really add this to renderedfiles and call
355                 # check_overwrite, as above, but currently renderedfiles
356                 # only supports listing one file per page.
357                 if ($config{rss} && exists $inlinepages{$page}) {
358                         writefile("$config{destdir}/".rsspage($page),
359                                 genrss($content, $page, mtime("$config{srcdir}/$file")));
360                 }
361         }
362         else {
363                 $links{$file}=[];
364                 check_overwrite("$config{destdir}/$file", $file);
365                 writefile("$config{destdir}/$file", $content);
366                 $oldpagemtime{$file}=time;
367                 $renderedfiles{$file}=$file;
368         }
369 } #}}}
370
371 sub prune ($) { #{{{
372         my $file=shift;
373
374         unlink($file);
375         my $dir=dirname($file);
376         while (rmdir($dir)) {
377                 $dir=dirname($dir);
378         }
379 } #}}}
380
381 sub refresh () { #{{{
382         # find existing pages
383         my %exists;
384         my @files;
385         eval q{use File::Find};
386         find({
387                 no_chdir => 1,
388                 wanted => sub {
389                         if (/$config{wiki_file_prune_regexp}/) {
390                                 no warnings 'once';
391                                 $File::Find::prune=1;
392                                 use warnings "all";
393                         }
394                         elsif (! -d $_ && ! -l $_) {
395                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
396                                 if (! defined $f) {
397                                         warn("skipping bad filename $_\n");
398                                 }
399                                 else {
400                                         $f=~s/^\Q$config{srcdir}\E\/?//;
401                                         push @files, $f;
402                                         $exists{pagename($f)}=1;
403                                 }
404                         }
405                 },
406         }, $config{srcdir});
407
408         my %rendered;
409
410         # check for added or removed pages
411         my @add;
412         foreach my $file (@files) {
413                 my $page=pagename($file);
414                 if (! $oldpagemtime{$page}) {
415                         debug("new page $page") unless exists $pagectime{$page};
416                         push @add, $file;
417                         $links{$page}=[];
418                         $pagesources{$page}=$file;
419                         $pagectime{$page}=mtime("$config{srcdir}/$file") 
420                                 unless exists $pagectime{$page};
421                 }
422         }
423         my @del;
424         foreach my $page (keys %oldpagemtime) {
425                 if (! $exists{$page}) {
426                         debug("removing old page $page");
427                         push @del, $pagesources{$page};
428                         prune($config{destdir}."/".$renderedfiles{$page});
429                         delete $renderedfiles{$page};
430                         $oldpagemtime{$page}=0;
431                         delete $pagesources{$page};
432                 }
433         }
434         
435         # render any updated files
436         foreach my $file (@files) {
437                 my $page=pagename($file);
438                 
439                 if (! exists $oldpagemtime{$page} ||
440                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
441                         debug("rendering changed file $file");
442                         render($file);
443                         $rendered{$file}=1;
444                 }
445         }
446         
447         # if any files were added or removed, check to see if each page
448         # needs an update due to linking to them or inlining them.
449         # TODO: inefficient; pages may get rendered above and again here;
450         # problem is the bestlink may have changed and we won't know until
451         # now
452         if (@add || @del) {
453 FILE:           foreach my $file (@files) {
454                         my $page=pagename($file);
455                         foreach my $f (@add, @del) {
456                                 my $p=pagename($f);
457                                 foreach my $link (@{$links{$page}}) {
458                                         if (bestlink($page, $link) eq $p) {
459                                                 debug("rendering $file, which links to $p");
460                                                 render($file);
461                                                 $rendered{$file}=1;
462                                                 next FILE;
463                                         }
464                                 }
465                         }
466                 }
467         }
468
469         # Handle backlinks; if a page has added/removed links, update the
470         # pages it links to. Also handle inlining here.
471         # TODO: inefficient; pages may get rendered above and again here;
472         # problem is the backlinks could be wrong in the first pass render
473         # above
474         if (%rendered || @del) {
475                 foreach my $f (@files) {
476                         my $p=pagename($f);
477                         if (exists $inlinepages{$p}) {
478                                 foreach my $file (keys %rendered, @del) {
479                                         my $page=pagename($file);
480                                         if (globlist_match($page, $inlinepages{$p})) {
481                                                 debug("rendering $f, which inlines $page");
482                                                 render($f);
483                                                 last;
484                                         }
485                                 }
486                         }
487                 }
488                 
489                 my %linkchanged;
490                 foreach my $file (keys %rendered, @del) {
491                         my $page=pagename($file);
492                         
493                         if (exists $links{$page}) {
494                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
495                                         if (length $link &&
496                                             ! exists $oldlinks{$page} ||
497                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
498                                                 $linkchanged{$link}=1;
499                                         }
500                                 }
501                         }
502                         if (exists $oldlinks{$page}) {
503                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
504                                         if (length $link &&
505                                             ! exists $links{$page} ||
506                                             ! grep { $_ eq $link } @{$links{$page}}) {
507                                                 $linkchanged{$link}=1;
508                                         }
509                                 }
510                         }
511                 }
512                 foreach my $link (keys %linkchanged) {
513                         my $linkfile=$pagesources{$link};
514                         if (defined $linkfile) {
515                                 debug("rendering $linkfile, to update its backlinks");
516                                 render($linkfile);
517                         }
518                 }
519         }
520 } #}}}
521
522 1