Merge remote branch 'upstream/master' into prv/po
[ikiwiki] / 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                 # Preprocess in scan-only mode.
178                 preprocess($page, $page, $content, 1);
179
180                 run_hooks(scan => sub {
181                         shift->(
182                                 page => $page,
183                                 content => $content,
184                         );
185                 });
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         eval q{use Cwd};
296         die $@ if $@;
297         my $origdir=getcwd();
298         my $abssrcdir=Cwd::abs_path($config{srcdir});
299
300         my ($page, $underlay);
301         my $helper=sub {
302                 my $file=decode_utf8($_);
303
304                 return if -l $file || -d _;
305                 $file=~s/^\.\///;
306                 return if ! length $file;
307                 $page = pagename($file);
308                 if (! exists $pagesources{$page} &&
309                     file_pruned($file)) {
310                         $File::Find::prune=1;
311                         return;
312                 }
313
314                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
315                 if (! defined $f) {
316                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
317                         return;
318                 }
319         
320                 if ($underlay) {
321                         # avoid underlaydir override attacks; see security.mdwn
322                         if (! -l "$abssrcdir/$f" && ! -e _) {
323                                 if (! $pages{$page}) {
324                                         push @files, $f;
325                                         $pages{$page}=1;
326                                 }
327                         }
328                 }
329                 else {
330                         push @files, $f;
331                         if ($pages{$page}) {
332                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
333                         }
334                         $pages{$page}=1;
335                 }
336         };
337
338         chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
339         find({
340                 no_chdir => 1,
341                 wanted => $helper,
342         }, '.');
343         chdir($origdir) || die "chdir $origdir: $!";
344
345         $underlay=1;
346         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
347                 if (chdir($_)) {
348                         find({
349                                 no_chdir => 1,
350                                 wanted => $helper,
351                         }, '.');
352                         chdir($origdir) || die "chdir: $!";
353                 }
354         };
355
356         return \@files, \%pages;
357 }
358
359 sub find_new_files ($) {
360         my $files=shift;
361         my @new;
362         my @internal_new;
363
364         my $times_noted;
365
366         foreach my $file (@$files) {
367                 my $page=pagename($file);
368
369                 if ($config{rcs} && $config{gettime} &&
370                     -e "$config{srcdir}/$file") {
371                         if (! $times_noted) {
372                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
373                                 $times_noted=1;
374                         }
375
376                         eval {
377                                 my $ctime=rcs_getctime($file);
378                                 if ($ctime > 0) {
379                                         $pagectime{$page}=$ctime;
380                                 }
381                         };
382                         if ($@) {
383                                 print STDERR $@;
384                         }
385                         my $mtime;
386                         eval {
387                                 $mtime=rcs_getmtime($file);
388                         };
389                         if ($@) {
390                                 print STDERR $@;
391                         }
392                         elsif ($mtime > 0) {
393                                 utime($mtime, $mtime, "$config{srcdir}/$file");
394                         }
395                 }
396
397                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
398                         # the page has changed its type
399                         $forcerebuild{$page}=1;
400                 }
401                 $pagesources{$page}=$file;
402                 if (! $pagemtime{$page}) {
403                         if (isinternal($page)) {
404                                 push @internal_new, $file;
405                         }
406                         else {
407                                 push @new, $file;
408                         }
409                         $pagecase{lc $page}=$page;
410                         if (! exists $pagectime{$page}) {
411                                 $pagectime{$page}=(srcfile_stat($file))[10];
412                         }
413                 }
414         }
415
416         return \@new, \@internal_new;
417 }
418
419 sub find_del_files ($) {
420         my $pages=shift;
421         my @del;
422         my @internal_del;
423
424         foreach my $page (keys %pagesources) {
425                 if (! $pages->{$page}) {
426                         if (isinternal($page)) {
427                                 push @internal_del, $pagesources{$page};
428                         }
429                         else {
430                                 push @del, $pagesources{$page};
431                         }
432                         $links{$page}=[];
433                         delete $typedlinks{$page};
434                         $renderedfiles{$page}=[];
435                         $pagemtime{$page}=0;
436                 }
437         }
438
439         return \@del, \@internal_del;
440 }
441
442 sub remove_del (@) {
443         foreach my $file (@_) {
444                 my $page=pagename($file);
445                 if (! isinternal($page)) {
446                         debug(sprintf(gettext("removing obsolete %s"), $page));
447                 }
448         
449                 foreach my $old (@{$oldrenderedfiles{$page}}) {
450                         prune($config{destdir}."/".$old);
451                 }
452
453                 foreach my $source (keys %destsources) {
454                         if ($destsources{$source} eq $page) {
455                                 delete $destsources{$source};
456                         }
457                 }
458         
459                 delete $pagecase{lc $page};
460                 $delpagesources{$page}=$pagesources{$page};
461                 delete $pagesources{$page};
462         }
463 }
464
465 sub find_changed ($) {
466         my $files=shift;
467         my @changed;
468         my @internal_changed;
469         foreach my $file (@$files) {
470                 my $page=pagename($file);
471                 my ($srcfile, @stat)=srcfile_stat($file);
472                 if (! exists $pagemtime{$page} ||
473                     $stat[9] > $pagemtime{$page} ||
474                     $forcerebuild{$page}) {
475                         $pagemtime{$page}=$stat[9];
476
477                         if (isinternal($page)) {
478                                 # Preprocess internal page in scan-only mode.
479                                 preprocess($page, $page, readfile($srcfile), 1);
480                                 push @internal_changed, $file;
481                         }
482                         else {
483                                 push @changed, $file;
484                         }
485                 }
486         }
487         return \@changed, \@internal_changed;
488 }
489
490 sub calculate_old_links ($$) {
491         my ($changed, $del)=@_;
492         my %oldlink_targets;
493         foreach my $file (@$changed, @$del) {
494                 my $page=pagename($file);
495                 if (exists $oldlinks{$page}) {
496                         foreach my $l (@{$oldlinks{$page}}) {
497                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
498                         }
499                 }
500         }
501         return \%oldlink_targets;
502 }
503
504 sub derender_internal ($) {
505         my $file=shift;
506         my $page=pagename($file);
507         delete $depends{$page};
508         delete $depends_simple{$page};
509         foreach my $old (@{$renderedfiles{$page}}) {
510                 delete $destsources{$old};
511         }
512         $renderedfiles{$page}=[];
513 }
514
515 sub render_linkers ($) {
516         my $f=shift;
517         my $p=pagename($f);
518         foreach my $page (keys %{$backlinks{$p}}) {
519                 my $file=$pagesources{$page};
520                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
521         }
522 }
523
524 sub remove_unrendered () {
525         foreach my $src (keys %rendered) {
526                 my $page=pagename($src);
527                 foreach my $file (@{$oldrenderedfiles{$page}}) {
528                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
529                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
530                                 prune($config{destdir}."/".$file);
531                         }
532                 }
533         }
534 }
535
536 sub link_types_changed ($$) {
537         # each is of the form { type => { link => 1 } }
538         my $new = shift;
539         my $old = shift;
540
541         return 0 if !defined $new && !defined $old;
542         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
543
544         while (my ($type, $links) = each %$new) {
545                 foreach my $link (keys %$links) {
546                         return 1 unless exists $old->{$type}{$link};
547                 }
548         }
549
550         while (my ($type, $links) = each %$old) {
551                 foreach my $link (keys %$links) {
552                         return 1 unless exists $new->{$type}{$link};
553                 }
554         }
555
556         return 0;
557 }
558
559 sub calculate_changed_links ($$$) {
560         my ($changed, $del, $oldlink_targets)=@_;
561
562         my (%backlinkchanged, %linkchangers);
563
564         foreach my $file (@$changed, @$del) {
565                 my $page=pagename($file);
566
567                 if (exists $links{$page}) {
568                         foreach my $l (@{$links{$page}}) {
569                                 my $target=bestlink($page, $l);
570                                 if (! exists $oldlink_targets->{$page}{$l} ||
571                                     $target ne $oldlink_targets->{$page}{$l}) {
572                                         $backlinkchanged{$target}=1;
573                                         $linkchangers{lc($page)}=1;
574                                 }
575                                 delete $oldlink_targets->{$page}{$l};
576                         }
577                 }
578                 if (exists $oldlink_targets->{$page} &&
579                     %{$oldlink_targets->{$page}}) {
580                         foreach my $target (values %{$oldlink_targets->{$page}}) {
581                                 $backlinkchanged{$target}=1;
582                         }
583                         $linkchangers{lc($page)}=1;
584                 }
585
586                 # we currently assume that changing the type of a link doesn't
587                 # change backlinks
588                 if (!exists $linkchangers{lc($page)}) {
589                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
590                                 $linkchangers{lc($page)}=1;
591                         }
592                 }
593         }
594
595         return \%backlinkchanged, \%linkchangers;
596 }
597
598 sub render_dependent ($$$$$$$) {
599         my ($files, $new, $internal_new, $del, $internal_del,
600                 $internal_changed, $linkchangers)=@_;
601
602         my @changed=(keys %rendered, @$del);
603         my @exists_changed=(@$new, @$del);
604         
605         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
606         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
607
608         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
609                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
610                         foreach my $f (@$files) {
611                                 next if $rendered{$f};
612                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
613                         }
614                         return 0;
615                 }
616         }
617          
618         foreach my $f (@$files) {
619                 next if $rendered{$f};
620                 my $p=pagename($f);
621                 my $reason = undef;
622
623                 if (exists $depends_simple{$p} && ! defined $reason) {
624                         foreach my $d (keys %{$depends_simple{$p}}) {
625                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
626                                      $lc_changed{$d})
627                                     ||
628                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
629                                      $lc_exists_changed{$d})
630                                     ||
631                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
632                                      $linkchangers->{$d})
633                                 ) {
634                                         $reason = $d;
635                                         last;
636                                 }
637                         }
638                 }
639         
640                 if (exists $depends{$p} && ! defined $reason) {
641                         foreach my $dep (keys %{$depends{$p}}) {
642                                 my $sub=pagespec_translate($dep);
643                                 next unless defined $sub;
644
645                                 # only consider internal files
646                                 # if the page explicitly depends
647                                 # on such files
648                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
649
650                                 my $in=sub {
651                                         my $list=shift;
652                                         my $type=shift;
653                                         foreach my $file (@$list) {
654                                                 next if $file eq $f;
655                                                 my $page=pagename($file);
656                                                 if ($sub->($page, location => $p)) {
657                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
658                                                                 next unless $linkchangers->{lc($page)};
659                                                         }
660                                                         $reason=$page;
661                                                         return 1;
662                                                 }
663                                         }
664                                         return undef;
665                                 };
666
667                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
668                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
669                                         last if $internal_dep && (
670                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
671                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
672                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
673                                         );
674                                 }
675                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
676                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
677                                         last if $internal_dep && (
678                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
679                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
680                                         );
681                                 }
682                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
683                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
684                                         last if $internal_dep && (
685                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
686                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
687                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
688                                         );
689                                 }
690                         }
691                 }
692         
693                 if (defined $reason) {
694                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
695                         return 1;
696                 }
697         }
698
699         return 0;
700 }
701
702 sub render_backlinks ($) {
703         my $backlinkchanged=shift;
704         foreach my $link (keys %$backlinkchanged) {
705                 my $linkfile=$pagesources{$link};
706                 if (defined $linkfile) {
707                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
708                 }
709         }
710 }
711
712 sub gen_autofile ($$$) {
713         my $autofile=shift;
714         my $pages=shift;
715         my $del=shift;
716
717         if (file_pruned($autofile)) {
718                 return;
719         }
720
721         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
722         if (! defined $file) {
723                 return;
724         }
725
726         # Remember autofiles that were tried, and never try them again later.
727         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
728                 return;
729         }
730         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
731
732         if (srcfile($autofile, 1) || file_pruned($autofile)) {
733                 return;
734         }
735         
736         if (-l $file || -d _ || -e _) {
737                 return;
738         }
739
740         my $page = pagename($file);
741         if ($pages->{$page}) {
742                 return;
743         }
744
745         if (grep { $_ eq $autofile } @$del) {
746                 return;
747         }
748
749         $autofiles{$autofile}{generator}->();
750         $pages->{$page}=1;
751         return 1;
752 }
753
754
755 sub refresh () {
756         srcdir_check();
757         run_hooks(refresh => sub { shift->() });
758         my ($files, $pages)=find_src_files();
759         my ($new, $internal_new)=find_new_files($files);
760         my ($del, $internal_del)=find_del_files($pages);
761         my ($changed, $internal_changed)=find_changed($files);
762         run_hooks(needsbuild => sub { shift->($changed) });
763         my $oldlink_targets=calculate_old_links($changed, $del);
764
765         foreach my $file (@$changed) {
766                 scan($file);
767         }
768
769         foreach my $autofile (keys %autofiles) {
770                 if (gen_autofile($autofile, $pages, $del)) {
771                         push @{$files}, $autofile;
772                         push @{$new}, $autofile if find_new_files([$autofile]);
773                         push @{$changed}, $autofile if find_changed([$autofile]);
774                         
775                         scan($autofile);
776                 }
777         }
778
779         calculate_links();
780         
781         remove_del(@$del, @$internal_del);
782
783         foreach my $file (@$changed) {
784                 render($file, sprintf(gettext("building %s"), $file));
785         }
786         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
787                 derender_internal($file);
788         }
789
790         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
791                 $del, $oldlink_targets);
792
793         foreach my $file (@$new, @$del) {
794                 render_linkers($file);
795         }
796
797         if (@$changed || @$internal_changed ||
798             @$del || @$internal_del || @$internal_new) {
799                 1 while render_dependent($files, $new, $internal_new,
800                         $del, $internal_del, $internal_changed,
801                         $linkchangers);
802         }
803
804         render_backlinks($backlinkchanged);
805         remove_unrendered();
806
807         if (@$del || @$internal_del) {
808                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
809         }
810         if (%rendered) {
811                 run_hooks(change => sub { shift->(keys %rendered) });
812         }
813 }
814
815 sub clean_rendered {
816         lockwiki();
817         loadindex();
818         remove_unrendered();
819         foreach my $page (keys %oldrenderedfiles) {
820                 foreach my $file (@{$oldrenderedfiles{$page}}) {
821                         prune($config{destdir}."/".$file);
822                 }
823         }
824 }
825
826 sub commandline_render () {
827         lockwiki();
828         loadindex();
829         unlockwiki();
830
831         my $srcfile=possibly_foolish_untaint($config{render});
832         my $file=$srcfile;
833         $file=~s/\Q$config{srcdir}\E\/?//;
834
835         my $type=pagetype($file);
836         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
837         my $content=readfile($srcfile);
838         my $page=pagename($file);
839         $pagesources{$page}=$file;
840         $content=filter($page, $page, $content);
841         $content=preprocess($page, $page, $content);
842         $content=linkify($page, $page, $content);
843         $content=htmlize($page, $page, $type, $content);
844         $pagemtime{$page}=(stat($srcfile))[9];
845         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
846
847         print genpage($page, $content);
848         exit 0;
849 }
850
851 1