When editing a page, show that page's sidebar. (Thanks, privat)
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9
10 my (%backlinks, %rendered);
11 our %brokenlinks;
12 my $links_calculated=0;
13
14 sub calculate_links () {
15         return if $links_calculated;
16         %backlinks=%brokenlinks=();
17         foreach my $page (keys %links) {
18                 foreach my $link (@{$links{$page}}) {
19                         my $bestlink=bestlink($page, $link);
20                         if (length $bestlink) {
21                                 $backlinks{$bestlink}{$page}=1
22                                         if $bestlink ne $page;
23                         }
24                         else {
25                                 push @{$brokenlinks{$link}}, $page;
26                         }
27                 }
28         }
29         $links_calculated=1;
30 }
31
32 sub backlink_pages ($) {
33         my $page=shift;
34
35         calculate_links();
36
37         return keys %{$backlinks{$page}};
38 }
39
40 sub backlinks ($) {
41         my $page=shift;
42
43         my @links;
44         foreach my $p (backlink_pages($page)) {
45                 my $href=urlto($p, $page);
46
47                 # Trim common dir prefixes from both pages.
48                 my $p_trimmed=$p;
49                 my $page_trimmed=$page;
50                 my $dir;
51                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
52                         defined $dir &&
53                         $p_trimmed=~s/^\Q$dir\E// &&
54                         $page_trimmed=~s/^\Q$dir\E//;
55                                
56                 push @links, { url => $href, page => pagetitle($p_trimmed) };
57         }
58         return @links;
59 }
60
61 sub genpage ($$) {
62         my $page=shift;
63         my $content=shift;
64         
65         run_hooks(indexhtml => sub {
66                 shift->(page => $page, destpage => $page, content => $content);
67         });
68
69         my $templatefile;
70         run_hooks(templatefile => sub {
71                 return if defined $templatefile;
72                 my $file=shift->(page => $page);
73                 if (defined $file && defined template_file($file)) {
74                         $templatefile=$file;
75                 }
76         });
77         my $template;
78         if (defined $templatefile) {
79                 $template=template_depends($templatefile, $page,
80                         blind_cache => 1);
81         }
82         else {
83                 # no explicit depends as special case
84                 $template=template('page.tmpl', 
85                         blind_cache => 1);
86         }
87
88         my $actions=0;
89         if (length $config{cgiurl}) {
90                 if (IkiWiki->can("cgi_editpage")) {
91                         $template->param(editurl => cgiurl(do => "edit", page => $page));
92                         $actions++;
93                 }
94         }
95         if (defined $config{historyurl} && length $config{historyurl}) {
96                 my $u=$config{historyurl};
97                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
98                 $template->param(historyurl => $u);
99                 $actions++;
100         }
101         if ($config{discussion}) {
102                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
103                    (length $config{cgiurl} ||
104                     exists $links{$page."/".$config{discussionpage}})) {
105                         $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
106                         $actions++;
107                 }
108         }
109         if ($actions) {
110                 $template->param(have_actions => 1);
111         }
112         templateactions($template, $page);
113
114         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
115         my ($backlinks, $more_backlinks);
116         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
117                 $backlinks=\@backlinks;
118                 $more_backlinks=[];
119         }
120         else {
121                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
122                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
123         }
124
125         $template->param(
126                 title => $page eq 'index' 
127                         ? $config{wikiname} 
128                         : pagetitle(basename($page)),
129                 wikiname => $config{wikiname},
130                 content => $content,
131                 backlinks => $backlinks,
132                 more_backlinks => $more_backlinks,
133                 mtime => displaytime($pagemtime{$page}),
134                 ctime => displaytime($pagectime{$page}, undef, 1),
135                 baseurl => baseurl($page),
136                 html5 => $config{html5},
137         );
138
139         run_hooks(pagetemplate => sub {
140                 shift->(page => $page, destpage => $page, template => $template);
141         });
142         
143         $content=$template->output;
144         
145         run_hooks(format => sub {
146                 $content=shift->(
147                         page => $page,
148                         content => $content,
149                 );
150         });
151
152         return $content;
153 }
154
155 sub scan ($) {
156         my $file=shift;
157
158         debug(sprintf(gettext("scanning %s"), $file));
159
160         my $type=pagetype($file);
161         if (defined $type) {
162                 my $srcfile=srcfile($file);
163                 my $content=readfile($srcfile);
164                 my $page=pagename($file);
165                 will_render($page, htmlpage($page), 1);
166
167                 if ($config{discussion}) {
168                         # Discussion links are a special case since they're
169                         # not in the text of the page, but on its template.
170                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
171                 }
172                 else {
173                         $links{$page}=[];
174                 }
175                 delete $typedlinks{$page};
176
177                 run_hooks(scan => sub {
178                         shift->(
179                                 page => $page,
180                                 content => $content,
181                         );
182                 });
183
184                 # Preprocess in scan-only mode.
185                 preprocess($page, $page, $content, 1);
186         }
187         else {
188                 will_render($file, $file, 1);
189         }
190 }
191
192 sub fast_file_copy (@) {
193         my $srcfile=shift;
194         my $destfile=shift;
195         my $srcfd=shift;
196         my $destfd=shift;
197         my $cleanup=shift;
198
199         my $blksize = 16384;
200         my ($len, $buf, $written);
201         while ($len = sysread $srcfd, $buf, $blksize) {
202                 if (! defined $len) {
203                         next if $! =~ /^Interrupted/;
204                         error("failed to read $srcfile: $!", $cleanup);
205                 }
206                 my $offset = 0;
207                 while ($len) {
208                         defined($written = syswrite $destfd, $buf, $len, $offset)
209                                 or error("failed to write $destfile: $!", $cleanup);
210                         $len -= $written;
211                         $offset += $written;
212                 }
213         }
214 }
215
216 sub render ($$) {
217         my $file=shift;
218         return if $rendered{$file};
219         debug(shift);
220         $rendered{$file}=1;
221         
222         my $type=pagetype($file);
223         my $srcfile=srcfile($file);
224         if (defined $type) {
225                 my $page=pagename($file);
226                 delete $depends{$page};
227                 delete $depends_simple{$page};
228                 will_render($page, htmlpage($page), 1);
229                 return if $type=~/^_/;
230                 
231                 my $content=htmlize($page, $page, $type,
232                         linkify($page, $page,
233                         preprocess($page, $page,
234                         filter($page, $page,
235                         readfile($srcfile)))));
236                 
237                 my $output=htmlpage($page);
238                 writefile($output, $config{destdir}, genpage($page, $content));
239         }
240         else {
241                 delete $depends{$file};
242                 delete $depends_simple{$file};
243                 will_render($file, $file, 1);
244                 
245                 if ($config{hardlink}) {
246                         # only hardlink if owned by same user
247                         my @stat=stat($srcfile);
248                         if ($stat[4] == $>) {
249                                 prep_writefile($file, $config{destdir});
250                                 unlink($config{destdir}."/".$file);
251                                 if (link($srcfile, $config{destdir}."/".$file)) {
252                                         return;
253                                 }
254                         }
255                         # if hardlink fails, fall back to copying
256                 }
257                 
258                 my $srcfd=readfile($srcfile, 1, 1);
259                 writefile($file, $config{destdir}, undef, 1, sub {
260                         fast_file_copy($srcfile, $file, $srcfd, @_);
261                 });
262         }
263 }
264
265 sub prune ($) {
266         my $file=shift;
267
268         unlink($file);
269         my $dir=dirname($file);
270         while (rmdir($dir)) {
271                 $dir=dirname($dir);
272         }
273 }
274
275 sub srcdir_check () {
276         # security check, avoid following symlinks in the srcdir path by default
277         my $test=$config{srcdir};
278         while (length $test) {
279                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
280                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
281                 }
282                 unless ($test=~s/\/+$//) {
283                         $test=dirname($test);
284                 }
285         }
286         
287 }
288
289 sub find_src_files () {
290         my @files;
291         my %pages;
292         eval q{use File::Find};
293         error($@) if $@;
294
295         my ($page, $dir, $underlay);
296         my $helper=sub {
297                 my $file=decode_utf8($_);
298
299                 return if -l $file || -d _;
300                 $file=~s/^\Q$dir\E\/?//;
301                 return if ! length $file;
302                 $page = pagename($file);
303                 if (! exists $pagesources{$page} &&
304                     file_pruned($file)) {
305                         $File::Find::prune=1;
306                         return;
307                 }
308
309                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
310                 if (! defined $f) {
311                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
312                         return;
313                 }
314         
315                 if ($underlay) {
316                         # avoid underlaydir override attacks; see security.mdwn
317                         if (! -l "$config{srcdir}/$f" && ! -e _) {
318                                 if (! $pages{$page}) {
319                                         push @files, $f;
320                                         $pages{$page}=1;
321                                 }
322                         }
323                 }
324                 else {
325                         push @files, $f;
326                         if ($pages{$page}) {
327                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
328                         }
329                         $pages{$page}=1;
330                 }
331         };
332
333         find({
334                 no_chdir => 1,
335                 wanted => $helper,
336         }, $dir=$config{srcdir});
337         $underlay=1;
338         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
339                 find({
340                         no_chdir => 1,
341                         wanted => $helper,
342                 }, $dir=$_);
343         };
344         return \@files, \%pages;
345 }
346
347 sub find_new_files ($) {
348         my $files=shift;
349         my @new;
350         my @internal_new;
351
352         my $times_noted;
353
354         foreach my $file (@$files) {
355                 my $page=pagename($file);
356                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
357                         # the page has changed its type
358                         $forcerebuild{$page}=1;
359                 }
360                 $pagesources{$page}=$file;
361                 if (! $pagemtime{$page}) {
362                         if (isinternal($page)) {
363                                 push @internal_new, $file;
364                         }
365                         elsif ($config{rcs}) {
366                                 push @new, $file;
367                                 if ($config{gettime} && -e "$config{srcdir}/$file") {
368                                         if (! $times_noted) {
369                                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
370                                                 $times_noted=1;
371                                         }
372
373                                         eval {
374                                                 my $ctime=rcs_getctime("$config{srcdir}/$file");
375                                                 if ($ctime > 0) {
376                                                         $pagectime{$page}=$ctime;
377                                                 }
378                                         };
379                                         if ($@) {
380                                                 print STDERR $@;
381                                         }
382                                         my $mtime;
383                                         eval {
384                                                 $mtime=rcs_getmtime("$config{srcdir}/$file");
385                                         };
386                                         if ($@) {
387                                                 print STDERR $@;
388                                         }
389                                         elsif ($mtime > 0) {
390                                                 utime($mtime, $mtime, "$config{srcdir}/$file");
391                                         }
392                                 }
393                         }
394                         $pagecase{lc $page}=$page;
395                         if (! exists $pagectime{$page}) {
396                                 $pagectime{$page}=(srcfile_stat($file))[10];
397                         }
398                 }
399         }
400
401         return \@new, \@internal_new;
402 }
403
404 sub find_del_files ($) {
405         my $pages=shift;
406         my @del;
407         my @internal_del;
408
409         foreach my $page (keys %pagesources) {
410                 if (! $pages->{$page}) {
411                         if (isinternal($page)) {
412                                 push @internal_del, $pagesources{$page};
413                         }
414                         else {
415                                 push @del, $pagesources{$page};
416                         }
417                         $links{$page}=[];
418                         delete $typedlinks{$page};
419                         $renderedfiles{$page}=[];
420                         $pagemtime{$page}=0;
421                 }
422         }
423
424         return \@del, \@internal_del;
425 }
426
427 sub remove_del (@) {
428         foreach my $file (@_) {
429                 my $page=pagename($file);
430                 if (! isinternal($page)) {
431                         debug(sprintf(gettext("removing obsolete %s"), $page));
432                 }
433         
434                 foreach my $old (@{$oldrenderedfiles{$page}}) {
435                         prune($config{destdir}."/".$old);
436                 }
437
438                 foreach my $source (keys %destsources) {
439                         if ($destsources{$source} eq $page) {
440                                 delete $destsources{$source};
441                         }
442                 }
443         
444                 delete $pagecase{lc $page};
445                 $delpagesources{$page}=$pagesources{$page};
446                 delete $pagesources{$page};
447         }
448 }
449
450 sub find_changed ($) {
451         my $files=shift;
452         my @changed;
453         my @internal_changed;
454         foreach my $file (@$files) {
455                 my $page=pagename($file);
456                 my ($srcfile, @stat)=srcfile_stat($file);
457                 if (! exists $pagemtime{$page} ||
458                     $stat[9] > $pagemtime{$page} ||
459                     $forcerebuild{$page}) {
460                         $pagemtime{$page}=$stat[9];
461
462                         if (isinternal($page)) {
463                                 # Preprocess internal page in scan-only mode.
464                                 preprocess($page, $page, readfile($srcfile), 1);
465                                 push @internal_changed, $file;
466                         }
467                         else {
468                                 push @changed, $file;
469                         }
470                 }
471         }
472         return \@changed, \@internal_changed;
473 }
474
475 sub calculate_old_links ($$) {
476         my ($changed, $del)=@_;
477         my %oldlink_targets;
478         foreach my $file (@$changed, @$del) {
479                 my $page=pagename($file);
480                 if (exists $oldlinks{$page}) {
481                         foreach my $l (@{$oldlinks{$page}}) {
482                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
483                         }
484                 }
485         }
486         return \%oldlink_targets;
487 }
488
489 sub derender_internal ($) {
490         my $file=shift;
491         my $page=pagename($file);
492         delete $depends{$page};
493         delete $depends_simple{$page};
494         foreach my $old (@{$renderedfiles{$page}}) {
495                 delete $destsources{$old};
496         }
497         $renderedfiles{$page}=[];
498 }
499
500 sub render_linkers ($) {
501         my $f=shift;
502         my $p=pagename($f);
503         foreach my $page (keys %{$backlinks{$p}}) {
504                 my $file=$pagesources{$page};
505                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
506         }
507 }
508
509 sub remove_unrendered () {
510         foreach my $src (keys %rendered) {
511                 my $page=pagename($src);
512                 foreach my $file (@{$oldrenderedfiles{$page}}) {
513                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
514                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
515                                 prune($config{destdir}."/".$file);
516                         }
517                 }
518         }
519 }
520
521 sub link_types_changed ($$) {
522         # each is of the form { type => { link => 1 } }
523         my $new = shift;
524         my $old = shift;
525
526         return 0 if !defined $new && !defined $old;
527         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
528
529         while (my ($type, $links) = each %$new) {
530                 foreach my $link (keys %$links) {
531                         return 1 unless exists $old->{$type}{$link};
532                 }
533         }
534
535         while (my ($type, $links) = each %$old) {
536                 foreach my $link (keys %$links) {
537                         return 1 unless exists $new->{$type}{$link};
538                 }
539         }
540
541         return 0;
542 }
543
544 sub calculate_changed_links ($$$) {
545         my ($changed, $del, $oldlink_targets)=@_;
546
547         my (%backlinkchanged, %linkchangers);
548
549         foreach my $file (@$changed, @$del) {
550                 my $page=pagename($file);
551
552                 if (exists $links{$page}) {
553                         foreach my $l (@{$links{$page}}) {
554                                 my $target=bestlink($page, $l);
555                                 if (! exists $oldlink_targets->{$page}{$l} ||
556                                     $target ne $oldlink_targets->{$page}{$l}) {
557                                         $backlinkchanged{$target}=1;
558                                         $linkchangers{lc($page)}=1;
559                                 }
560                                 delete $oldlink_targets->{$page}{$l};
561                         }
562                 }
563                 if (exists $oldlink_targets->{$page} &&
564                     %{$oldlink_targets->{$page}}) {
565                         foreach my $target (values %{$oldlink_targets->{$page}}) {
566                                 $backlinkchanged{$target}=1;
567                         }
568                         $linkchangers{lc($page)}=1;
569                 }
570
571                 # we currently assume that changing the type of a link doesn't
572                 # change backlinks
573                 if (!exists $linkchangers{lc($page)}) {
574                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
575                                 $linkchangers{lc($page)}=1;
576                         }
577                 }
578         }
579
580         return \%backlinkchanged, \%linkchangers;
581 }
582
583 sub render_dependent ($$$$$$$) {
584         my ($files, $new, $internal_new, $del, $internal_del,
585                 $internal_changed, $linkchangers)=@_;
586
587         my @changed=(keys %rendered, @$del);
588         my @exists_changed=(@$new, @$del);
589         
590         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
591         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
592
593         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
594                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
595                         foreach my $f (@$files) {
596                                 next if $rendered{$f};
597                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
598                         }
599                         return 0;
600                 }
601         }
602          
603         foreach my $f (@$files) {
604                 next if $rendered{$f};
605                 my $p=pagename($f);
606                 my $reason = undef;
607
608                 if (exists $depends_simple{$p} && ! defined $reason) {
609                         foreach my $d (keys %{$depends_simple{$p}}) {
610                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
611                                      $lc_changed{$d})
612                                     ||
613                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
614                                      $lc_exists_changed{$d})
615                                     ||
616                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
617                                      $linkchangers->{$d})
618                                 ) {
619                                         $reason = $d;
620                                         last;
621                                 }
622                         }
623                 }
624         
625                 if (exists $depends{$p} && ! defined $reason) {
626                         foreach my $dep (keys %{$depends{$p}}) {
627                                 my $sub=pagespec_translate($dep);
628                                 next unless defined $sub;
629
630                                 # only consider internal files
631                                 # if the page explicitly depends
632                                 # on such files
633                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
634
635                                 my $in=sub {
636                                         my $list=shift;
637                                         my $type=shift;
638                                         foreach my $file (@$list) {
639                                                 next if $file eq $f;
640                                                 my $page=pagename($file);
641                                                 if ($sub->($page, location => $p)) {
642                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
643                                                                 next unless $linkchangers->{lc($page)};
644                                                         }
645                                                         $reason=$page;
646                                                         return 1;
647                                                 }
648                                         }
649                                         return undef;
650                                 };
651
652                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
653                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
654                                         last if $internal_dep && (
655                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
656                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
657                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
658                                         );
659                                 }
660                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
661                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
662                                         last if $internal_dep && (
663                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
664                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
665                                         );
666                                 }
667                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
668                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
669                                         last if $internal_dep && (
670                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
671                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
672                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
673                                         );
674                                 }
675                         }
676                 }
677         
678                 if (defined $reason) {
679                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
680                         return 1;
681                 }
682         }
683
684         return 0;
685 }
686
687 sub render_backlinks ($) {
688         my $backlinkchanged=shift;
689         foreach my $link (keys %$backlinkchanged) {
690                 my $linkfile=$pagesources{$link};
691                 if (defined $linkfile) {
692                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
693                 }
694         }
695 }
696
697 sub gen_autofile ($$$) {
698         my $autofile=shift;
699         my $pages=shift;
700         my $del=shift;
701
702         if (file_pruned($autofile)) {
703                 return;
704         }
705
706         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
707         if (! defined $file) {
708                 return;
709         }
710
711         # Remember autofiles that were tried, and never try them again later.
712         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
713                 return;
714         }
715         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
716
717         if (srcfile($autofile, 1) || file_pruned($autofile)) {
718                 return;
719         }
720         
721         if (-l $file || -d _ || -e _) {
722                 return;
723         }
724
725         my $page = pagename($file);
726         if ($pages->{$page}) {
727                 return;
728         }
729
730         if (grep { $_ eq $autofile } @$del) {
731                 return;
732         }
733
734         $autofiles{$autofile}{generator}->();
735         $pages->{$page}=1;
736         return 1;
737 }
738
739
740 sub refresh () {
741         srcdir_check();
742         run_hooks(refresh => sub { shift->() });
743         my ($files, $pages)=find_src_files();
744         my ($new, $internal_new)=find_new_files($files);
745         my ($del, $internal_del)=find_del_files($pages);
746         my ($changed, $internal_changed)=find_changed($files);
747         run_hooks(needsbuild => sub { shift->($changed) });
748         my $oldlink_targets=calculate_old_links($changed, $del);
749
750         foreach my $file (@$changed) {
751                 scan($file);
752         }
753
754         foreach my $autofile (keys %autofiles) {
755                 if (gen_autofile($autofile, $pages, $del)) {
756                         push @{$files}, $autofile;
757                         push @{$new}, $autofile if find_new_files([$autofile]);
758                         push @{$changed}, $autofile if find_changed([$autofile]);
759                         
760                         scan($autofile);
761                 }
762         }
763
764         calculate_links();
765         
766         remove_del(@$del, @$internal_del);
767
768         foreach my $file (@$changed) {
769                 render($file, sprintf(gettext("building %s"), $file));
770         }
771         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
772                 derender_internal($file);
773         }
774
775         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
776                 $del, $oldlink_targets);
777
778         foreach my $file (@$new, @$del) {
779                 render_linkers($file);
780         }
781
782         if (@$changed || @$internal_changed ||
783             @$del || @$internal_del || @$internal_new) {
784                 1 while render_dependent($files, $new, $internal_new,
785                         $del, $internal_del, $internal_changed,
786                         $linkchangers);
787         }
788
789         render_backlinks($backlinkchanged);
790         remove_unrendered();
791
792         if (@$del || @$internal_del) {
793                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
794         }
795         if (%rendered) {
796                 run_hooks(change => sub { shift->(keys %rendered) });
797         }
798 }
799
800 sub clean_rendered {
801         lockwiki();
802         loadindex();
803         remove_unrendered();
804         foreach my $page (keys %oldrenderedfiles) {
805                 foreach my $file (@{$oldrenderedfiles{$page}}) {
806                         prune($config{destdir}."/".$file);
807                 }
808         }
809 }
810
811 sub commandline_render () {
812         lockwiki();
813         loadindex();
814         unlockwiki();
815
816         my $srcfile=possibly_foolish_untaint($config{render});
817         my $file=$srcfile;
818         $file=~s/\Q$config{srcdir}\E\/?//;
819
820         my $type=pagetype($file);
821         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
822         my $content=readfile($srcfile);
823         my $page=pagename($file);
824         $pagesources{$page}=$file;
825         $content=filter($page, $page, $content);
826         $content=preprocess($page, $page, $content);
827         $content=linkify($page, $page, $content);
828         $content=htmlize($page, $page, $type, $content);
829         $pagemtime{$page}=(stat($srcfile))[9];
830         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
831
832         print genpage($page, $content);
833         exit 0;
834 }
835
836 1