]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
add ISPAGE variable to page.tmpl
[git.ikiwiki.info.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
10 my (%backlinks, %rendered);
11 our %brokenlinks;
12 my $links_calculated=0;
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 }
32 sub backlink_pages ($) {
33         my $page=shift;
35         calculate_links();
37         return keys %{$backlinks{$page}};
38 }
40 sub backlinks ($) {
41         my $page=shift;
43         my @links;
44         foreach my $p (backlink_pages($page)) {
45                 my $href=urlto($p, $page);
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 }
61 sub genpage ($$) {
62         my $page=shift;
63         my $content=shift;
64         
65         run_hooks(postscan => sub {
66                 shift->(page => $page, content => $content);
67         });
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;
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         }
115         my @actions;
116         run_hooks(pageactions => sub {
117                 push @actions, map { { action => $_ } } 
118                         grep { defined } shift->(page => $page);
119         });
120         $template->param(actions => \@actions);
122         if ($actions || @actions) {
123                 $template->param(have_actions => 1);
124         }
126         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
127         my ($backlinks, $more_backlinks);
128         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
129                 $backlinks=\@backlinks;
130                 $more_backlinks=[];
131         }
132         else {
133                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
134                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
135         }
137         $template->param(
138                 title => $page eq 'index' 
139                         ? $config{wikiname} 
140                         : pagetitle(basename($page)),
141                 ispage => 1,
142                 wikiname => $config{wikiname},
143                 content => $content,
144                 backlinks => $backlinks,
145                 more_backlinks => $more_backlinks,
146                 mtime => displaytime($pagemtime{$page}),
147                 ctime => displaytime($pagectime{$page}, undef, 1),
148                 baseurl => baseurl($page),
149                 have_footer => 1,
150                 html5 => $config{html5},
151         );
153         run_hooks(pagetemplate => sub {
154                 shift->(page => $page, destpage => $page, template => $template);
155         });
156         
157         $content=$template->output;
158         
159         run_hooks(format => sub {
160                 $content=shift->(
161                         page => $page,
162                         content => $content,
163                 );
164         });
166         return $content;
169 sub scan ($) {
170         my $file=shift;
172         debug(sprintf(gettext("scanning %s"), $file));
174         my $type=pagetype($file);
175         if (defined $type) {
176                 my $srcfile=srcfile($file);
177                 my $content=readfile($srcfile);
178                 my $page=pagename($file);
179                 will_render($page, htmlpage($page), 1);
181                 if ($config{discussion}) {
182                         # Discussion links are a special case since they're
183                         # not in the text of the page, but on its template.
184                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
185                 }
186                 else {
187                         $links{$page}=[];
188                 }
189                 delete $typedlinks{$page};
191                 run_hooks(scan => sub {
192                         shift->(
193                                 page => $page,
194                                 content => $content,
195                         );
196                 });
198                 # Preprocess in scan-only mode.
199                 preprocess($page, $page, $content, 1);
200         }
201         else {
202                 will_render($file, $file, 1);
203         }
206 sub fast_file_copy (@) {
207         my $srcfile=shift;
208         my $destfile=shift;
209         my $srcfd=shift;
210         my $destfd=shift;
211         my $cleanup=shift;
213         my $blksize = 16384;
214         my ($len, $buf, $written);
215         while ($len = sysread $srcfd, $buf, $blksize) {
216                 if (! defined $len) {
217                         next if $! =~ /^Interrupted/;
218                         error("failed to read $srcfile: $!", $cleanup);
219                 }
220                 my $offset = 0;
221                 while ($len) {
222                         defined($written = syswrite $destfd, $buf, $len, $offset)
223                                 or error("failed to write $destfile: $!", $cleanup);
224                         $len -= $written;
225                         $offset += $written;
226                 }
227         }
230 sub render ($$) {
231         my $file=shift;
232         return if $rendered{$file};
233         debug(shift);
234         $rendered{$file}=1;
235         
236         my $type=pagetype($file);
237         my $srcfile=srcfile($file);
238         if (defined $type) {
239                 my $page=pagename($file);
240                 delete $depends{$page};
241                 delete $depends_simple{$page};
242                 will_render($page, htmlpage($page), 1);
243                 return if $type=~/^_/;
244                 
245                 my $content=htmlize($page, $page, $type,
246                         linkify($page, $page,
247                         preprocess($page, $page,
248                         filter($page, $page,
249                         readfile($srcfile)))));
250                 
251                 my $output=htmlpage($page);
252                 writefile($output, $config{destdir}, genpage($page, $content));
253         }
254         else {
255                 delete $depends{$file};
256                 delete $depends_simple{$file};
257                 will_render($file, $file, 1);
258                 
259                 if ($config{hardlink}) {
260                         # only hardlink if owned by same user
261                         my @stat=stat($srcfile);
262                         if ($stat[4] == $>) {
263                                 prep_writefile($file, $config{destdir});
264                                 unlink($config{destdir}."/".$file);
265                                 if (link($srcfile, $config{destdir}."/".$file)) {
266                                         return;
267                                 }
268                         }
269                         # if hardlink fails, fall back to copying
270                 }
271                 
272                 my $srcfd=readfile($srcfile, 1, 1);
273                 writefile($file, $config{destdir}, undef, 1, sub {
274                         fast_file_copy($srcfile, $file, $srcfd, @_);
275                 });
276         }
279 sub prune ($) {
280         my $file=shift;
282         unlink($file);
283         my $dir=dirname($file);
284         while (rmdir($dir)) {
285                 $dir=dirname($dir);
286         }
289 sub srcdir_check () {
290         # security check, avoid following symlinks in the srcdir path by default
291         my $test=$config{srcdir};
292         while (length $test) {
293                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
294                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
295                 }
296                 unless ($test=~s/\/+$//) {
297                         $test=dirname($test);
298                 }
299         }
300         
303 sub find_src_files () {
304         my @files;
305         my %pages;
306         eval q{use File::Find};
307         error($@) if $@;
309         my ($page, $dir, $underlay);
310         my $helper=sub {
311                 my $file=decode_utf8($_);
313                 return if -l $file || -d _;
314                 $file=~s/^\Q$dir\E\/?//;
315                 return if ! length $file;
316                 $page = pagename($file);
317                 if (! exists $pagesources{$page} &&
318                     file_pruned($file)) {
319                         $File::Find::prune=1;
320                         return;
321                 }
323                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
324                 if (! defined $f) {
325                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
326                         return;
327                 }
328         
329                 if ($underlay) {
330                         # avoid underlaydir override attacks; see security.mdwn
331                         if (! -l "$config{srcdir}/$f" && ! -e _) {
332                                 if (! $pages{$page}) {
333                                         push @files, $f;
334                                         $pages{$page}=1;
335                                 }
336                         }
337                 }
338                 else {
339                         push @files, $f;
340                         if ($pages{$page}) {
341                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
342                         }
343                         $pages{$page}=1;
344                 }
345         };
347         find({
348                 no_chdir => 1,
349                 wanted => $helper,
350         }, $dir=$config{srcdir});
351         $underlay=1;
352         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
353                 find({
354                         no_chdir => 1,
355                         wanted => $helper,
356                 }, $dir=$_);
357         };
358         return \@files, \%pages;
361 sub find_new_files ($) {
362         my $files=shift;
363         my @new;
364         my @internal_new;
366         my $times_noted;
368         foreach my $file (@$files) {
369                 my $page=pagename($file);
370                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
371                         # the page has changed its type
372                         $forcerebuild{$page}=1;
373                 }
374                 $pagesources{$page}=$file;
375                 if (! $pagemtime{$page}) {
376                         if (isinternal($page)) {
377                                 push @internal_new, $file;
378                         }
379                         elsif ($config{rcs}) {
380                                 push @new, $file;
381                                 if ($config{gettime} && -e "$config{srcdir}/$file") {
382                                         if (! $times_noted) {
383                                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
384                                                 $times_noted=1;
385                                         }
387                                         eval {
388                                                 my $ctime=rcs_getctime("$config{srcdir}/$file");
389                                                 if ($ctime > 0) {
390                                                         $pagectime{$page}=$ctime;
391                                                 }
392                                         };
393                                         if ($@) {
394                                                 print STDERR $@;
395                                         }
396                                         my $mtime;
397                                         eval {
398                                                 $mtime=rcs_getmtime("$config{srcdir}/$file");
399                                         };
400                                         if ($@) {
401                                                 print STDERR $@;
402                                         }
403                                         elsif ($mtime > 0) {
404                                                 utime($mtime, $mtime, "$config{srcdir}/$file");
405                                         }
406                                 }
407                         }
408                         $pagecase{lc $page}=$page;
409                         if (! exists $pagectime{$page}) {
410                                 $pagectime{$page}=(srcfile_stat($file))[10];
411                         }
412                 }
413         }
415         return \@new, \@internal_new;
418 sub find_del_files ($) {
419         my $pages=shift;
420         my @del;
421         my @internal_del;
423         foreach my $page (keys %pagesources) {
424                 if (! $pages->{$page}) {
425                         if (isinternal($page)) {
426                                 push @internal_del, $pagesources{$page};
427                         }
428                         else {
429                                 push @del, $pagesources{$page};
430                         }
431                         $links{$page}=[];
432                         delete $typedlinks{$page};
433                         $renderedfiles{$page}=[];
434                         $pagemtime{$page}=0;
435                 }
436         }
438         return \@del, \@internal_del;
441 sub remove_del (@) {
442         foreach my $file (@_) {
443                 my $page=pagename($file);
444                 if (! isinternal($page)) {
445                         debug(sprintf(gettext("removing obsolete %s"), $page));
446                 }
447         
448                 foreach my $old (@{$oldrenderedfiles{$page}}) {
449                         prune($config{destdir}."/".$old);
450                 }
452                 foreach my $source (keys %destsources) {
453                         if ($destsources{$source} eq $page) {
454                                 delete $destsources{$source};
455                         }
456                 }
457         
458                 delete $pagecase{lc $page};
459                 delete $pagesources{$page};
460         }
463 sub find_changed ($) {
464         my $files=shift;
465         my @changed;
466         my @internal_changed;
467         foreach my $file (@$files) {
468                 my $page=pagename($file);
469                 my ($srcfile, @stat)=srcfile_stat($file);
470                 if (! exists $pagemtime{$page} ||
471                     $stat[9] > $pagemtime{$page} ||
472                     $forcerebuild{$page}) {
473                         $pagemtime{$page}=$stat[9];
475                         if (isinternal($page)) {
476                                 # Preprocess internal page in scan-only mode.
477                                 preprocess($page, $page, readfile($srcfile), 1);
478                                 push @internal_changed, $file;
479                         }
480                         else {
481                                 push @changed, $file;
482                         }
483                 }
484         }
485         return \@changed, \@internal_changed;
488 sub calculate_old_links ($$) {
489         my ($changed, $del)=@_;
490         my %oldlink_targets;
491         foreach my $file (@$changed, @$del) {
492                 my $page=pagename($file);
493                 if (exists $oldlinks{$page}) {
494                         foreach my $l (@{$oldlinks{$page}}) {
495                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
496                         }
497                 }
498         }
499         return \%oldlink_targets;
502 sub derender_internal ($) {
503         my $file=shift;
504         my $page=pagename($file);
505         delete $depends{$page};
506         delete $depends_simple{$page};
507         foreach my $old (@{$renderedfiles{$page}}) {
508                 delete $destsources{$old};
509         }
510         $renderedfiles{$page}=[];
513 sub render_linkers ($) {
514         my $f=shift;
515         my $p=pagename($f);
516         foreach my $page (keys %{$backlinks{$p}}) {
517                 my $file=$pagesources{$page};
518                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
519         }
522 sub remove_unrendered () {
523         foreach my $src (keys %rendered) {
524                 my $page=pagename($src);
525                 foreach my $file (@{$oldrenderedfiles{$page}}) {
526                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
527                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
528                                 prune($config{destdir}."/".$file);
529                         }
530                 }
531         }
534 sub link_types_changed ($$) {
535         # each is of the form { type => { link => 1 } }
536         my $new = shift;
537         my $old = shift;
539         return 0 if !defined $new && !defined $old;
540         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
542         while (my ($type, $links) = each %$new) {
543                 foreach my $link (keys %$links) {
544                         return 1 unless exists $old->{$type}{$link};
545                 }
546         }
548         while (my ($type, $links) = each %$old) {
549                 foreach my $link (keys %$links) {
550                         return 1 unless exists $new->{$type}{$link};
551                 }
552         }
554         return 0;
557 sub calculate_changed_links ($$$) {
558         my ($changed, $del, $oldlink_targets)=@_;
560         my (%backlinkchanged, %linkchangers);
562         foreach my $file (@$changed, @$del) {
563                 my $page=pagename($file);
565                 if (exists $links{$page}) {
566                         foreach my $l (@{$links{$page}}) {
567                                 my $target=bestlink($page, $l);
568                                 if (! exists $oldlink_targets->{$page}{$l} ||
569                                     $target ne $oldlink_targets->{$page}{$l}) {
570                                         $backlinkchanged{$target}=1;
571                                         $linkchangers{lc($page)}=1;
572                                 }
573                                 delete $oldlink_targets->{$page}{$l};
574                         }
575                 }
576                 if (exists $oldlink_targets->{$page} &&
577                     %{$oldlink_targets->{$page}}) {
578                         foreach my $target (values %{$oldlink_targets->{$page}}) {
579                                 $backlinkchanged{$target}=1;
580                         }
581                         $linkchangers{lc($page)}=1;
582                 }
584                 # we currently assume that changing the type of a link doesn't
585                 # change backlinks
586                 if (!exists $linkchangers{lc($page)}) {
587                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
588                                 $linkchangers{lc($page)}=1;
589                         }
590                 }
591         }
593         return \%backlinkchanged, \%linkchangers;
596 sub render_dependent ($$$$$$$) {
597         my ($files, $new, $internal_new, $del, $internal_del,
598                 $internal_changed, $linkchangers)=@_;
600         my @changed=(keys %rendered, @$del);
601         my @exists_changed=(@$new, @$del);
602         
603         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
604         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
606         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
607                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
608                         foreach my $f (@$files) {
609                                 next if $rendered{$f};
610                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
611                         }
612                         return 0;
613                 }
614         }
615          
616         foreach my $f (@$files) {
617                 next if $rendered{$f};
618                 my $p=pagename($f);
619                 my $reason = undef;
621                 if (exists $depends_simple{$p} && ! defined $reason) {
622                         foreach my $d (keys %{$depends_simple{$p}}) {
623                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
624                                      $lc_changed{$d})
625                                     ||
626                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
627                                      $lc_exists_changed{$d})
628                                     ||
629                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
630                                      $linkchangers->{$d})
631                                 ) {
632                                         $reason = $d;
633                                         last;
634                                 }
635                         }
636                 }
637         
638                 if (exists $depends{$p} && ! defined $reason) {
639                         foreach my $dep (keys %{$depends{$p}}) {
640                                 my $sub=pagespec_translate($dep);
641                                 next unless defined $sub;
643                                 # only consider internal files
644                                 # if the page explicitly depends
645                                 # on such files
646                                 my $internal_dep=$dep =~ /internal\(/;
648                                 my $in=sub {
649                                         my $list=shift;
650                                         my $type=shift;
651                                         foreach my $file (@$list) {
652                                                 next if $file eq $f;
653                                                 my $page=pagename($file);
654                                                 if ($sub->($page, location => $p)) {
655                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
656                                                                 next unless $linkchangers->{lc($page)};
657                                                         }
658                                                         $reason=$page;
659                                                         return 1;
660                                                 }
661                                         }
662                                         return undef;
663                                 };
665                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
666                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
667                                         last if $internal_dep && (
668                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
669                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
670                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
671                                         );
672                                 }
673                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
674                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
675                                         last if $internal_dep && (
676                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
677                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
678                                         );
679                                 }
680                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
681                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
682                                         last if $internal_dep && (
683                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
684                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
685                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
686                                         );
687                                 }
688                         }
689                 }
690         
691                 if (defined $reason) {
692                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
693                         return 1;
694                 }
695         }
697         return 0;
700 sub render_backlinks ($) {
701         my $backlinkchanged=shift;
702         foreach my $link (keys %$backlinkchanged) {
703                 my $linkfile=$pagesources{$link};
704                 if (defined $linkfile) {
705                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
706                 }
707         }
710 sub gen_autofile ($$$) {
711         my $autofile=shift;
712         my $pages=shift;
713         my $del=shift;
715         if (file_pruned($autofile)) {
716                 return;
717         }
719         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
720         if (! defined $file) {
721                 return;
722         }
724         # Remember autofiles that were tried, and never try them again later.
725         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
726                 return;
727         }
728         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
730         if (srcfile($autofile, 1) || file_pruned($autofile)) {
731                 return;
732         }
733         
734         if (-l $file || -d _ || -e _) {
735                 return;
736         }
738         my $page = pagename($file);
739         if ($pages->{$page}) {
740                 return;
741         }
743         if (grep { $_ eq $autofile } @$del) {
744                 return;
745         }
747         $autofiles{$autofile}{generator}->();
748         $pages->{$page}=1;
749         return 1;
753 sub refresh () {
754         srcdir_check();
755         run_hooks(refresh => sub { shift->() });
756         my ($files, $pages)=find_src_files();
757         my ($new, $internal_new)=find_new_files($files);
758         my ($del, $internal_del)=find_del_files($pages);
759         my ($changed, $internal_changed)=find_changed($files);
760         run_hooks(needsbuild => sub { shift->($changed) });
761         my $oldlink_targets=calculate_old_links($changed, $del);
763         foreach my $file (@$changed) {
764                 scan($file);
765         }
767         foreach my $autofile (keys %autofiles) {
768                 if (gen_autofile($autofile, $pages, $del)) {
769                         push @{$files}, $autofile;
770                         push @{$new}, $autofile if find_new_files([$autofile]);
771                         push @{$changed}, $autofile if find_changed([$autofile]);
772                         
773                         scan($autofile);
774                 }
775         }
777         calculate_links();
778         
779         remove_del(@$del, @$internal_del);
781         foreach my $file (@$changed) {
782                 render($file, sprintf(gettext("building %s"), $file));
783         }
784         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
785                 derender_internal($file);
786         }
788         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
789                 $del, $oldlink_targets);
791         foreach my $file (@$new, @$del) {
792                 render_linkers($file);
793         }
795         if (@$changed || @$internal_changed ||
796             @$del || @$internal_del || @$internal_new) {
797                 1 while render_dependent($files, $new, $internal_new,
798                         $del, $internal_del, $internal_changed,
799                         $linkchangers);
800         }
802         render_backlinks($backlinkchanged);
803         remove_unrendered();
805         if (@$del) {
806                 run_hooks(delete => sub { shift->(@$del) });
807         }
808         if (%rendered) {
809                 run_hooks(change => sub { shift->(keys %rendered) });
810         }
813 sub clean_rendered {
814         lockwiki();
815         loadindex();
816         remove_unrendered();
817         foreach my $page (keys %oldrenderedfiles) {
818                 foreach my $file (@{$oldrenderedfiles{$page}}) {
819                         prune($config{destdir}."/".$file);
820                 }
821         }
824 sub commandline_render () {
825         lockwiki();
826         loadindex();
827         unlockwiki();
829         my $srcfile=possibly_foolish_untaint($config{render});
830         my $file=$srcfile;
831         $file=~s/\Q$config{srcdir}\E\/?//;
833         my $type=pagetype($file);
834         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
835         my $content=readfile($srcfile);
836         my $page=pagename($file);
837         $pagesources{$page}=$file;
838         $content=filter($page, $page, $content);
839         $content=preprocess($page, $page, $content);
840         $content=linkify($page, $page, $content);
841         $content=htmlize($page, $page, $type, $content);
842         $pagemtime{$page}=(stat($srcfile))[9];
843         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
845         print genpage($page, $content);
846         exit 0;