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