]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
3.20120629.2
[git.ikiwiki.info.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use IkiWiki;
9 my (%backlinks, %rendered);
10 our %brokenlinks;
11 my $links_calculated=0;
13 sub calculate_links () {
14         return if $links_calculated;
15         %backlinks=%brokenlinks=();
16         foreach my $page (keys %links) {
17                 foreach my $link (@{$links{$page}}) {
18                         my $bestlink=bestlink($page, $link);
19                         if (length $bestlink) {
20                                 $backlinks{$bestlink}{$page}=1
21                                         if $bestlink ne $page;
22                         }
23                         else {
24                                 push @{$brokenlinks{$link}}, $page;
25                         }
26                 }
27         }
28         $links_calculated=1;
29 }
31 sub backlink_pages ($) {
32         my $page=shift;
34         calculate_links();
36         return keys %{$backlinks{$page}};
37 }
39 sub backlinks ($) {
40         my $page=shift;
42         my @links;
43         foreach my $p (backlink_pages($page)) {
44                 my $href=urlto($p, $page);
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 => pagetitle($p_trimmed) };
56         }
57         return @links;
58 }
60 sub genpage ($$) {
61         my $page=shift;
62         my $content=shift;
63         
64         run_hooks(indexhtml => sub {
65                 shift->(page => $page, destpage => $page, content => $content);
66         });
68         my $templatefile;
69         run_hooks(templatefile => sub {
70                 return if defined $templatefile;
71                 my $file=shift->(page => $page);
72                 if (defined $file && defined template_file($file)) {
73                         $templatefile=$file;
74                 }
75         });
76         my $template;
77         if (defined $templatefile) {
78                 $template=template_depends($templatefile, $page,
79                         blind_cache => 1);
80         }
81         else {
82                 # no explicit depends as special case
83                 $template=template('page.tmpl', 
84                         blind_cache => 1);
85         }
87         my $actions=0;
88         if (length $config{cgiurl}) {
89                 if (IkiWiki->can("cgi_editpage")) {
90                         $template->param(editurl => cgiurl(do => "edit", page => $page));
91                         $actions++;
92                 }
93         }
94         if (defined $config{historyurl} && length $config{historyurl}) {
95                 my $u=$config{historyurl};
96                 my $p=uri_escape_utf8($pagesources{$page}, '^A-Za-z0-9\-\._~/');
97                 $u=~s/\[\[file\]\]/$p/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);
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         }
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         );
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         });
152         return $content;
155 sub scan ($) {
156         my $file=shift;
158         debug(sprintf(gettext("scanning %s"), $file));
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);
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};
177                 # Preprocess in scan-only mode.
178                 preprocess($page, $page, $content, 1);
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         }
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;
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         }
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         }
265 sub prune ($;$) {
266         my $file=shift;
267         my $up_to=shift;
269         unlink($file);
270         my $dir=dirname($file);
271         while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) {
272                 $dir=dirname($dir);
273         }
276 sub srcdir_check () {
277         # security check, avoid following symlinks in the srcdir path by default
278         my $test=$config{srcdir};
279         while (length $test) {
280                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
281                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
282                 }
283                 unless ($test=~s/\/+$//) {
284                         $test=dirname($test);
285                 }
286         }
287         
290 sub find_src_files () {
291         my @files;
292         my %pages;
293         eval q{use File::Find};
294         error($@) if $@;
296         eval q{use Cwd};
297         die $@ if $@;
298         my $origdir=getcwd();
299         my $abssrcdir=Cwd::abs_path($config{srcdir});
301         my ($page, $underlay);
302         my $helper=sub {
303                 my $file=decode_utf8($_);
305                 return if -l $file || -d _;
306                 $file=~s/^\.\///;
307                 return if ! length $file;
308                 $page = pagename($file);
309                 if (! exists $pagesources{$page} &&
310                     file_pruned($file)) {
311                         $File::Find::prune=1;
312                         return;
313                 }
315                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
316                 if (! defined $f) {
317                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
318                         return;
319                 }
320         
321                 if ($underlay) {
322                         # avoid underlaydir override attacks; see security.mdwn
323                         if (! -l "$abssrcdir/$f" && ! -e _) {
324                                 if (! $pages{$page}) {
325                                         push @files, $f;
326                                         $pages{$page}=1;
327                                 }
328                         }
329                 }
330                 else {
331                         push @files, $f;
332                         if ($pages{$page}) {
333                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
334                         }
335                         $pages{$page}=1;
336                 }
337         };
339         chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
340         find({
341                 no_chdir => 1,
342                 wanted => $helper,
343         }, '.');
344         chdir($origdir) || die "chdir $origdir: $!";
346         $underlay=1;
347         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
348                 if (chdir($_)) {
349                         find({
350                                 no_chdir => 1,
351                                 wanted => $helper,
352                         }, '.');
353                         chdir($origdir) || die "chdir: $!";
354                 }
355         };
357         return \@files, \%pages;
360 sub find_new_files ($) {
361         my $files=shift;
362         my @new;
363         my @internal_new;
365         my $times_noted;
367         foreach my $file (@$files) {
368                 my $page=pagename($file);
370                 if ($config{rcs} && $config{gettime} &&
371                     -e "$config{srcdir}/$file") {
372                         if (! $times_noted) {
373                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
374                                 $times_noted=1;
375                         }
377                         eval {
378                                 my $ctime=rcs_getctime($file);
379                                 if ($ctime > 0) {
380                                         $pagectime{$page}=$ctime;
381                                 }
382                         };
383                         if ($@) {
384                                 print STDERR $@;
385                         }
386                         my $mtime;
387                         eval {
388                                 $mtime=rcs_getmtime($file);
389                         };
390                         if ($@) {
391                                 print STDERR $@;
392                         }
393                         elsif ($mtime > 0) {
394                                 utime($mtime, $mtime, "$config{srcdir}/$file");
395                         }
396                 }
398                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
399                         # the page has changed its type
400                         $forcerebuild{$page}=1;
401                 }
402                 $pagesources{$page}=$file;
403                 if (! $pagemtime{$page}) {
404                         if (isinternal($page)) {
405                                 push @internal_new, $file;
406                         }
407                         else {
408                                 push @new, $file;
409                         }
410                         $pagecase{lc $page}=$page;
411                         if (! exists $pagectime{$page}) {
412                                 $pagectime{$page}=(srcfile_stat($file))[10];
413                         }
414                 }
415         }
417         return \@new, \@internal_new;
420 sub find_del_files ($) {
421         my $pages=shift;
422         my @del;
423         my @internal_del;
425         foreach my $page (keys %pagesources) {
426                 if (! $pages->{$page}) {
427                         if (isinternal($page)) {
428                                 push @internal_del, $pagesources{$page};
429                         }
430                         else {
431                                 push @del, $pagesources{$page};
432                         }
433                         $links{$page}=[];
434                         delete $typedlinks{$page};
435                         $renderedfiles{$page}=[];
436                         $pagemtime{$page}=0;
437                 }
438         }
440         return \@del, \@internal_del;
443 sub remove_del (@) {
444         foreach my $file (@_) {
445                 my $page=pagename($file);
446                 if (! isinternal($page)) {
447                         debug(sprintf(gettext("removing obsolete %s"), $page));
448                 }
449         
450                 foreach my $old (@{$oldrenderedfiles{$page}}) {
451                         prune($config{destdir}."/".$old, $config{destdir});
452                 }
454                 foreach my $source (keys %destsources) {
455                         if ($destsources{$source} eq $page) {
456                                 delete $destsources{$source};
457                         }
458                 }
459         
460                 delete $pagecase{lc $page};
461                 $delpagesources{$page}=$pagesources{$page};
462                 delete $pagesources{$page};
463         }
466 sub find_changed ($) {
467         my $files=shift;
468         my @changed;
469         my @internal_changed;
470         foreach my $file (@$files) {
471                 my $page=pagename($file);
472                 my ($srcfile, @stat)=srcfile_stat($file);
473                 if (! exists $pagemtime{$page} ||
474                     $stat[9] > $pagemtime{$page} ||
475                     $forcerebuild{$page}) {
476                         $pagemtime{$page}=$stat[9];
478                         if (isinternal($page)) {
479                                 my $content = readfile($srcfile);
481                                 # Preprocess internal page in scan-only mode.
482                                 preprocess($page, $page, $content, 1);
484                                 run_hooks(scan => sub {
485                                         shift->(
486                                                 page => $page,
487                                                 content => $content,
488                                         );
489                                 });
491                                 push @internal_changed, $file;
492                         }
493                         else {
494                                 push @changed, $file;
495                         }
496                 }
497         }
498         return \@changed, \@internal_changed;
501 sub calculate_old_links ($$) {
502         my ($changed, $del)=@_;
503         my %oldlink_targets;
504         foreach my $file (@$changed, @$del) {
505                 my $page=pagename($file);
506                 if (exists $oldlinks{$page}) {
507                         foreach my $l (@{$oldlinks{$page}}) {
508                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
509                         }
510                 }
511         }
512         return \%oldlink_targets;
515 sub derender_internal ($) {
516         my $file=shift;
517         my $page=pagename($file);
518         delete $depends{$page};
519         delete $depends_simple{$page};
520         foreach my $old (@{$renderedfiles{$page}}) {
521                 delete $destsources{$old};
522         }
523         $renderedfiles{$page}=[];
526 sub render_linkers ($) {
527         my $f=shift;
528         my $p=pagename($f);
529         foreach my $page (keys %{$backlinks{$p}}) {
530                 my $file=$pagesources{$page};
531                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
532         }
535 sub remove_unrendered () {
536         foreach my $src (keys %rendered) {
537                 my $page=pagename($src);
538                 foreach my $file (@{$oldrenderedfiles{$page}}) {
539                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
540                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
541                                 prune($config{destdir}."/".$file, $config{destdir});
542                         }
543                 }
544         }
547 sub link_types_changed ($$) {
548         # each is of the form { type => { link => 1 } }
549         my $new = shift;
550         my $old = shift;
552         return 0 if !defined $new && !defined $old;
553         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
555         while (my ($type, $links) = each %$new) {
556                 foreach my $link (keys %$links) {
557                         return 1 unless exists $old->{$type}{$link};
558                 }
559         }
561         while (my ($type, $links) = each %$old) {
562                 foreach my $link (keys %$links) {
563                         return 1 unless exists $new->{$type}{$link};
564                 }
565         }
567         return 0;
570 sub calculate_changed_links ($$$) {
571         my ($changed, $del, $oldlink_targets)=@_;
573         my (%backlinkchanged, %linkchangers);
575         foreach my $file (@$changed, @$del) {
576                 my $page=pagename($file);
578                 if (exists $links{$page}) {
579                         foreach my $l (@{$links{$page}}) {
580                                 my $target=bestlink($page, $l);
581                                 if (! exists $oldlink_targets->{$page}{$l} ||
582                                     $target ne $oldlink_targets->{$page}{$l}) {
583                                         $backlinkchanged{$target}=1;
584                                         $linkchangers{lc($page)}=1;
585                                 }
586                                 delete $oldlink_targets->{$page}{$l};
587                         }
588                 }
589                 if (exists $oldlink_targets->{$page} &&
590                     %{$oldlink_targets->{$page}}) {
591                         foreach my $target (values %{$oldlink_targets->{$page}}) {
592                                 $backlinkchanged{$target}=1;
593                         }
594                         $linkchangers{lc($page)}=1;
595                 }
597                 # we currently assume that changing the type of a link doesn't
598                 # change backlinks
599                 if (!exists $linkchangers{lc($page)}) {
600                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
601                                 $linkchangers{lc($page)}=1;
602                         }
603                 }
604         }
606         return \%backlinkchanged, \%linkchangers;
609 sub render_dependent ($$$$$$$) {
610         my ($files, $new, $internal_new, $del, $internal_del,
611                 $internal_changed, $linkchangers)=@_;
613         my @changed=(keys %rendered, @$del);
614         my @exists_changed=(@$new, @$del);
615         
616         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
617         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
619         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
620                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
621                         foreach my $f (@$files) {
622                                 next if $rendered{$f};
623                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
624                         }
625                         return 0;
626                 }
627         }
628          
629         foreach my $f (@$files) {
630                 next if $rendered{$f};
631                 my $p=pagename($f);
632                 my $reason = undef;
634                 if (exists $depends_simple{$p} && ! defined $reason) {
635                         foreach my $d (keys %{$depends_simple{$p}}) {
636                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
637                                      $lc_changed{$d})
638                                     ||
639                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
640                                      $lc_exists_changed{$d})
641                                     ||
642                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
643                                      $linkchangers->{$d})
644                                 ) {
645                                         $reason = $d;
646                                         last;
647                                 }
648                         }
649                 }
650         
651                 if (exists $depends{$p} && ! defined $reason) {
652                         foreach my $dep (keys %{$depends{$p}}) {
653                                 my $sub=pagespec_translate($dep);
654                                 next unless defined $sub;
656                                 # only consider internal files
657                                 # if the page explicitly depends
658                                 # on such files
659                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
661                                 my $in=sub {
662                                         my $list=shift;
663                                         my $type=shift;
664                                         foreach my $file (@$list) {
665                                                 next if $file eq $f;
666                                                 my $page=pagename($file);
667                                                 if ($sub->($page, location => $p)) {
668                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
669                                                                 next unless $linkchangers->{lc($page)};
670                                                         }
671                                                         $reason=$page;
672                                                         return 1;
673                                                 }
674                                         }
675                                         return undef;
676                                 };
678                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
679                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
680                                         last if $internal_dep && (
681                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
682                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
683                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
684                                         );
685                                 }
686                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
687                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
688                                         last if $internal_dep && (
689                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
690                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
691                                         );
692                                 }
693                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
694                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
695                                         last if $internal_dep && (
696                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
697                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
698                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
699                                         );
700                                 }
701                         }
702                 }
703         
704                 if (defined $reason) {
705                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
706                         return 1;
707                 }
708         }
710         return 0;
713 sub render_backlinks ($) {
714         my $backlinkchanged=shift;
715         foreach my $link (keys %$backlinkchanged) {
716                 my $linkfile=$pagesources{$link};
717                 if (defined $linkfile) {
718                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
719                 }
720         }
723 sub gen_autofile ($$$) {
724         my $autofile=shift;
725         my $pages=shift;
726         my $del=shift;
728         if (file_pruned($autofile)) {
729                 return;
730         }
732         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
733         if (! defined $file) {
734                 return;
735         }
737         # Remember autofiles that were tried, and never try them again later.
738         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
739                 return;
740         }
741         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
743         if (srcfile($autofile, 1) || file_pruned($autofile)) {
744                 return;
745         }
746         
747         if (-l $file || -d _ || -e _) {
748                 return;
749         }
751         my $page = pagename($file);
752         if ($pages->{$page}) {
753                 return;
754         }
756         if (grep { $_ eq $autofile } @$del) {
757                 return;
758         }
760         $autofiles{$autofile}{generator}->();
761         $pages->{$page}=1;
762         return 1;
766 sub refresh () {
767         srcdir_check();
768         run_hooks(refresh => sub { shift->() });
769         my ($files, $pages)=find_src_files();
770         my ($new, $internal_new)=find_new_files($files);
771         my ($del, $internal_del)=find_del_files($pages);
772         my ($changed, $internal_changed)=find_changed($files);
773         run_hooks(needsbuild => sub {
774                 my $ret=shift->($changed, [@$del, @$internal_del]);
775                 $changed=$ret if ref $ret eq 'ARRAY';
776         });
777         my $oldlink_targets=calculate_old_links($changed, $del);
779         foreach my $file (@$changed) {
780                 scan($file);
781         }
783         foreach my $autofile (keys %autofiles) {
784                 if (gen_autofile($autofile, $pages, $del)) {
785                         push @{$files}, $autofile;
786                         push @{$new}, $autofile if find_new_files([$autofile]);
787                         push @{$changed}, $autofile if find_changed([$autofile]);
788                         
789                         scan($autofile);
790                 }
791         }
793         calculate_links();
794         
795         remove_del(@$del, @$internal_del);
797         foreach my $file (@$changed) {
798                 render($file, sprintf(gettext("building %s"), $file));
799         }
800         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
801                 derender_internal($file);
802         }
804         run_hooks(build_affected => sub {
805                 my %affected = shift->();
806                 while (my ($page, $message) = each %affected) {
807                         next unless exists $pagesources{$page};
808                         render($pagesources{$page}, $message);
809                 }
810         });
812         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
813                 $del, $oldlink_targets);
815         foreach my $file (@$new, @$del) {
816                 render_linkers($file);
817         }
819         if (@$changed || @$internal_changed ||
820             @$del || @$internal_del || @$internal_new) {
821                 1 while render_dependent($files, $new, $internal_new,
822                         $del, $internal_del, $internal_changed,
823                         $linkchangers);
824         }
826         render_backlinks($backlinkchanged);
827         remove_unrendered();
829         if (@$del || @$internal_del) {
830                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
831         }
832         if (%rendered) {
833                 run_hooks(rendered => sub { shift->(keys %rendered) });
834                 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
835         }
836         my %all_changed = map { $_ => 1 }
837                 @$new, @$changed, @$del,
838                 @$internal_new, @$internal_changed, @$internal_del;
839         run_hooks(changes => sub { shift->(keys %all_changed) });
842 sub clean_rendered {
843         lockwiki();
844         loadindex();
845         remove_unrendered();
846         foreach my $page (keys %oldrenderedfiles) {
847                 foreach my $file (@{$oldrenderedfiles{$page}}) {
848                         prune($config{destdir}."/".$file, $config{destdir});
849                 }
850         }
853 sub commandline_render () {
854         lockwiki();
855         loadindex();
856         unlockwiki();
858         my $srcfile=possibly_foolish_untaint($config{render});
859         my $file=$srcfile;
860         $file=~s/\Q$config{srcdir}\E\/?//;
862         my $type=pagetype($file);
863         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
864         my $content=readfile($srcfile);
865         my $page=pagename($file);
866         $pagesources{$page}=$file;
867         $content=filter($page, $page, $content);
868         $content=preprocess($page, $page, $content);
869         $content=linkify($page, $page, $content);
870         $content=htmlize($page, $page, $type, $content);
871         $pagemtime{$page}=(stat($srcfile))[9];
872         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
874         print genpage($page, $content);
875         exit 0;