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