]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Merge remote-tracking branch 'smcv/comments-metadata'
[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;
268         unlink($file);
269         my $dir=dirname($file);
270         while (rmdir($dir)) {
271                 $dir=dirname($dir);
272         }
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         
289 sub find_src_files () {
290         my @files;
291         my %pages;
292         eval q{use File::Find};
293         error($@) if $@;
295         eval q{use Cwd};
296         die $@ if $@;
297         my $origdir=getcwd();
298         my $abssrcdir=Cwd::abs_path($config{srcdir});
300         my ($page, $underlay);
301         my $helper=sub {
302                 my $file=decode_utf8($_);
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                 }
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         };
338         chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
339         find({
340                 no_chdir => 1,
341                 wanted => $helper,
342         }, '.');
343         chdir($origdir) || die "chdir $origdir: $!";
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         };
356         return \@files, \%pages;
359 sub find_new_files ($) {
360         my $files=shift;
361         my @new;
362         my @internal_new;
364         my $times_noted;
366         foreach my $file (@$files) {
367                 my $page=pagename($file);
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                         }
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                 }
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         }
416         return \@new, \@internal_new;
419 sub find_del_files ($) {
420         my $pages=shift;
421         my @del;
422         my @internal_del;
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         }
439         return \@del, \@internal_del;
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                 }
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         }
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];
477                         if (isinternal($page)) {
478                                 my $content = readfile($srcfile);
480                                 # Preprocess internal page in scan-only mode.
481                                 preprocess($page, $page, $content, 1);
483                                 run_hooks(scan => sub {
484                                         shift->(
485                                                 page => $page,
486                                                 content => $content,
487                                         );
488                                 });
490                                 push @internal_changed, $file;
491                         }
492                         else {
493                                 push @changed, $file;
494                         }
495                 }
496         }
497         return \@changed, \@internal_changed;
500 sub calculate_old_links ($$) {
501         my ($changed, $del)=@_;
502         my %oldlink_targets;
503         foreach my $file (@$changed, @$del) {
504                 my $page=pagename($file);
505                 if (exists $oldlinks{$page}) {
506                         foreach my $l (@{$oldlinks{$page}}) {
507                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
508                         }
509                 }
510         }
511         return \%oldlink_targets;
514 sub derender_internal ($) {
515         my $file=shift;
516         my $page=pagename($file);
517         delete $depends{$page};
518         delete $depends_simple{$page};
519         foreach my $old (@{$renderedfiles{$page}}) {
520                 delete $destsources{$old};
521         }
522         $renderedfiles{$page}=[];
525 sub render_linkers ($) {
526         my $f=shift;
527         my $p=pagename($f);
528         foreach my $page (keys %{$backlinks{$p}}) {
529                 my $file=$pagesources{$page};
530                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
531         }
534 sub remove_unrendered () {
535         foreach my $src (keys %rendered) {
536                 my $page=pagename($src);
537                 foreach my $file (@{$oldrenderedfiles{$page}}) {
538                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
539                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
540                                 prune($config{destdir}."/".$file);
541                         }
542                 }
543         }
546 sub link_types_changed ($$) {
547         # each is of the form { type => { link => 1 } }
548         my $new = shift;
549         my $old = shift;
551         return 0 if !defined $new && !defined $old;
552         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
554         while (my ($type, $links) = each %$new) {
555                 foreach my $link (keys %$links) {
556                         return 1 unless exists $old->{$type}{$link};
557                 }
558         }
560         while (my ($type, $links) = each %$old) {
561                 foreach my $link (keys %$links) {
562                         return 1 unless exists $new->{$type}{$link};
563                 }
564         }
566         return 0;
569 sub calculate_changed_links ($$$) {
570         my ($changed, $del, $oldlink_targets)=@_;
572         my (%backlinkchanged, %linkchangers);
574         foreach my $file (@$changed, @$del) {
575                 my $page=pagename($file);
577                 if (exists $links{$page}) {
578                         foreach my $l (@{$links{$page}}) {
579                                 my $target=bestlink($page, $l);
580                                 if (! exists $oldlink_targets->{$page}{$l} ||
581                                     $target ne $oldlink_targets->{$page}{$l}) {
582                                         $backlinkchanged{$target}=1;
583                                         $linkchangers{lc($page)}=1;
584                                 }
585                                 delete $oldlink_targets->{$page}{$l};
586                         }
587                 }
588                 if (exists $oldlink_targets->{$page} &&
589                     %{$oldlink_targets->{$page}}) {
590                         foreach my $target (values %{$oldlink_targets->{$page}}) {
591                                 $backlinkchanged{$target}=1;
592                         }
593                         $linkchangers{lc($page)}=1;
594                 }
596                 # we currently assume that changing the type of a link doesn't
597                 # change backlinks
598                 if (!exists $linkchangers{lc($page)}) {
599                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
600                                 $linkchangers{lc($page)}=1;
601                         }
602                 }
603         }
605         return \%backlinkchanged, \%linkchangers;
608 sub render_dependent ($$$$$$$) {
609         my ($files, $new, $internal_new, $del, $internal_del,
610                 $internal_changed, $linkchangers)=@_;
612         my @changed=(keys %rendered, @$del);
613         my @exists_changed=(@$new, @$del);
614         
615         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
616         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
618         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
619                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
620                         foreach my $f (@$files) {
621                                 next if $rendered{$f};
622                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
623                         }
624                         return 0;
625                 }
626         }
627          
628         foreach my $f (@$files) {
629                 next if $rendered{$f};
630                 my $p=pagename($f);
631                 my $reason = undef;
633                 if (exists $depends_simple{$p} && ! defined $reason) {
634                         foreach my $d (keys %{$depends_simple{$p}}) {
635                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
636                                      $lc_changed{$d})
637                                     ||
638                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
639                                      $lc_exists_changed{$d})
640                                     ||
641                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
642                                      $linkchangers->{$d})
643                                 ) {
644                                         $reason = $d;
645                                         last;
646                                 }
647                         }
648                 }
649         
650                 if (exists $depends{$p} && ! defined $reason) {
651                         foreach my $dep (keys %{$depends{$p}}) {
652                                 my $sub=pagespec_translate($dep);
653                                 next unless defined $sub;
655                                 # only consider internal files
656                                 # if the page explicitly depends
657                                 # on such files
658                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
660                                 my $in=sub {
661                                         my $list=shift;
662                                         my $type=shift;
663                                         foreach my $file (@$list) {
664                                                 next if $file eq $f;
665                                                 my $page=pagename($file);
666                                                 if ($sub->($page, location => $p)) {
667                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
668                                                                 next unless $linkchangers->{lc($page)};
669                                                         }
670                                                         $reason=$page;
671                                                         return 1;
672                                                 }
673                                         }
674                                         return undef;
675                                 };
677                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
678                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
679                                         last if $internal_dep && (
680                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
681                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
682                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
683                                         );
684                                 }
685                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
686                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
687                                         last if $internal_dep && (
688                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
689                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
690                                         );
691                                 }
692                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
693                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
694                                         last if $internal_dep && (
695                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
696                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
697                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
698                                         );
699                                 }
700                         }
701                 }
702         
703                 if (defined $reason) {
704                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
705                         return 1;
706                 }
707         }
709         return 0;
712 sub render_backlinks ($) {
713         my $backlinkchanged=shift;
714         foreach my $link (keys %$backlinkchanged) {
715                 my $linkfile=$pagesources{$link};
716                 if (defined $linkfile) {
717                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
718                 }
719         }
722 sub gen_autofile ($$$) {
723         my $autofile=shift;
724         my $pages=shift;
725         my $del=shift;
727         if (file_pruned($autofile)) {
728                 return;
729         }
731         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
732         if (! defined $file) {
733                 return;
734         }
736         # Remember autofiles that were tried, and never try them again later.
737         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
738                 return;
739         }
740         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
742         if (srcfile($autofile, 1) || file_pruned($autofile)) {
743                 return;
744         }
745         
746         if (-l $file || -d _ || -e _) {
747                 return;
748         }
750         my $page = pagename($file);
751         if ($pages->{$page}) {
752                 return;
753         }
755         if (grep { $_ eq $autofile } @$del) {
756                 return;
757         }
759         $autofiles{$autofile}{generator}->();
760         $pages->{$page}=1;
761         return 1;
765 sub refresh () {
766         srcdir_check();
767         run_hooks(refresh => sub { shift->() });
768         my ($files, $pages)=find_src_files();
769         my ($new, $internal_new)=find_new_files($files);
770         my ($del, $internal_del)=find_del_files($pages);
771         my ($changed, $internal_changed)=find_changed($files);
772         run_hooks(needsbuild => sub {
773                 my $ret=shift->($changed, [@$del, @$internal_del]);
774                 $changed=$ret if ref $ret eq 'ARRAY';
775         });
776         my $oldlink_targets=calculate_old_links($changed, $del);
778         foreach my $file (@$changed) {
779                 scan($file);
780         }
782         foreach my $autofile (keys %autofiles) {
783                 if (gen_autofile($autofile, $pages, $del)) {
784                         push @{$files}, $autofile;
785                         push @{$new}, $autofile if find_new_files([$autofile]);
786                         push @{$changed}, $autofile if find_changed([$autofile]);
787                         
788                         scan($autofile);
789                 }
790         }
792         calculate_links();
793         
794         remove_del(@$del, @$internal_del);
796         foreach my $file (@$changed) {
797                 render($file, sprintf(gettext("building %s"), $file));
798         }
799         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
800                 derender_internal($file);
801         }
803         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
804                 $del, $oldlink_targets);
806         foreach my $file (@$new, @$del) {
807                 render_linkers($file);
808         }
810         if (@$changed || @$internal_changed ||
811             @$del || @$internal_del || @$internal_new) {
812                 1 while render_dependent($files, $new, $internal_new,
813                         $del, $internal_del, $internal_changed,
814                         $linkchangers);
815         }
817         render_backlinks($backlinkchanged);
818         remove_unrendered();
820         if (@$del || @$internal_del) {
821                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
822         }
823         if (%rendered) {
824                 run_hooks(change => sub { shift->(keys %rendered) });
825         }
828 sub clean_rendered {
829         lockwiki();
830         loadindex();
831         remove_unrendered();
832         foreach my $page (keys %oldrenderedfiles) {
833                 foreach my $file (@{$oldrenderedfiles{$page}}) {
834                         prune($config{destdir}."/".$file);
835                 }
836         }
839 sub commandline_render () {
840         lockwiki();
841         loadindex();
842         unlockwiki();
844         my $srcfile=possibly_foolish_untaint($config{render});
845         my $file=$srcfile;
846         $file=~s/\Q$config{srcdir}\E\/?//;
848         my $type=pagetype($file);
849         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
850         my $content=readfile($srcfile);
851         my $page=pagename($file);
852         $pagesources{$page}=$file;
853         $content=filter($page, $page, $content);
854         $content=preprocess($page, $page, $content);
855         $content=linkify($page, $page, $content);
856         $content=htmlize($page, $page, $type, $content);
857         $pagemtime{$page}=(stat($srcfile))[9];
858         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
860         print genpage($page, $content);
861         exit 0;