]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
83242a1973000eb1f6ad05364d6d7661871b1688
[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=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
78         my $actions=0;
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         }
106         if ($actions) {
107                 $template->param(have_actions => 1);
108         }
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         }
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         );
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         });
147         return $content;
150 sub scan ($) {
151         my $file=shift;
153         debug(sprintf(gettext("scanning %s"), $file));
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);
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};
172                 run_hooks(scan => sub {
173                         shift->(
174                                 page => $page,
175                                 content => $content,
176                         );
177                 });
179                 # Preprocess in scan-only mode.
180                 preprocess($page, $page, $content, 1);
181         }
182         else {
183                 will_render($file, $file, 1);
184         }
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;
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         }
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         }
260 sub prune ($) {
261         my $file=shift;
263         unlink($file);
264         my $dir=dirname($file);
265         while (rmdir($dir)) {
266                 $dir=dirname($dir);
267         }
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         
284 sub verify_src_file ($$) {
285         my $file=decode_utf8(shift);
286         my $dir=shift;
288         return if -l $file || -d _;
289         $file=~s/^\Q$dir\E\/?//;
290         return if ! length $file;
291         my $page = pagename($file);
292         if (! exists $pagesources{$page} &&
293                 file_pruned($file)) {
294                 $File::Find::prune=1;
295                 return;
296         }
298         my ($file_untainted) = $file =~ /$config{wiki_file_regexp}/; # untaint
299         if (! defined $file_untainted) {
300                 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
301         }
302         return ($file_untainted, $page);
305 sub find_src_files () {
306         my @files;
307         my %pages;
308         eval q{use File::Find};
309         error($@) if $@;
310         find({
311                 no_chdir => 1,
312                 wanted => sub {
313                         my ($file, $page) = verify_src_file($_, $config{srcdir});
314                         if (defined $file) {
315                                 push @files, $file;
316                                 if ($pages{$page}) {
317                                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
318                                 }
319                                 $pages{$page}=1;
320                         }
321                 },
322         }, $config{srcdir});
323         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
324                 find({
325                         no_chdir => 1,
326                         wanted => sub {
327                                 my ($file, $page) = verify_src_file($_, $dir);
328                                 if (defined $file) {
329                                         # avoid underlaydir override
330                                         # attacks; see security.mdwn
331                                         if (! -l "$config{srcdir}/$file" &&
332                                             ! -e _) {
333                                                 if (! $pages{$page}) {
334                                                         push @files, $file;
335                                                         $pages{$page}=1;
336                                                 }
337                                         }
338                                 }
339                         },
340                 }, $dir);
341         };
342         return \@files, \%pages;
345 sub find_new_files ($) {
346         my $files=shift;
347         my @new;
348         my @internal_new;
350         my $times_noted;
352         foreach my $file (@$files) {
353                 my $page=pagename($file);
354                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
355                         # the page has changed its type
356                         $forcerebuild{$page}=1;
357                 }
358                 $pagesources{$page}=$file;
359                 if (! $pagemtime{$page}) {
360                         if (isinternal($page)) {
361                                 push @internal_new, $file;
362                         }
363                         elsif ($config{rcs}) {
364                                 if (! $times_noted) {
365                                         debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
366                                         $times_noted=1;
367                                 }
369                                 push @new, $file;
370                                 if ($config{gettime} && -e "$config{srcdir}/$file") {
371                                         eval {
372                                                 my $ctime=rcs_getctime("$config{srcdir}/$file");
373                                                 if ($ctime > 0) {
374                                                         $pagectime{$page}=$ctime;
375                                                 }
376                                         };
377                                         if ($@) {
378                                                 print STDERR $@;
379                                         }
380                                         my $mtime;
381                                         eval {
382                                                 $mtime=rcs_getmtime("$config{srcdir}/$file");
383                                         };
384                                         if ($@) {
385                                                 print STDERR $@;
386                                         }
387                                         elsif ($mtime > 0) {
388                                                 utime($mtime, $mtime, "$config{srcdir}/$file");
389                                         }
390                                 }
391                         }
392                         $pagecase{lc $page}=$page;
393                         if (! exists $pagectime{$page}) {
394                                 $pagectime{$page}=(srcfile_stat($file))[10];
395                         }
396                 }
397         }
399         return \@new, \@internal_new;
402 sub find_del_files ($) {
403         my $pages=shift;
404         my @del;
405         my @internal_del;
407         foreach my $page (keys %pagemtime) {
408                 if (! $pages->{$page}) {
409                         if (isinternal($page)) {
410                                 push @internal_del, $pagesources{$page};
411                         }
412                         else {
413                                 push @del, $pagesources{$page};
414                         }
415                         $links{$page}=[];
416                         delete $typedlinks{$page};
417                         $renderedfiles{$page}=[];
418                         $pagemtime{$page}=0;
419                 }
420         }
422         return \@del, \@internal_del;
425 sub remove_del (@) {
426         foreach my $file (@_) {
427                 my $page=pagename($file);
428                 if (! isinternal($page)) {
429                         debug(sprintf(gettext("removing old page %s"), $page));
430                 }
431         
432                 foreach my $old (@{$oldrenderedfiles{$page}}) {
433                         prune($config{destdir}."/".$old);
434                 }
436                 foreach my $source (keys %destsources) {
437                         if ($destsources{$source} eq $page) {
438                                 delete $destsources{$source};
439                         }
440                 }
441         
442                 delete $pagecase{lc $page};
443                 delete $pagesources{$page};
444         }
447 sub find_changed ($) {
448         my $files=shift;
449         my @changed;
450         my @internal_changed;
451         foreach my $file (@$files) {
452                 my $page=pagename($file);
453                 my ($srcfile, @stat)=srcfile_stat($file);
454                 if (! exists $pagemtime{$page} ||
455                     $stat[9] > $pagemtime{$page} ||
456                     $forcerebuild{$page}) {
457                         $pagemtime{$page}=$stat[9];
459                         if (isinternal($page)) {
460                                 # Preprocess internal page in scan-only mode.
461                                 preprocess($page, $page, readfile($srcfile), 1);
462                                 push @internal_changed, $file;
463                         }
464                         else {
465                                 push @changed, $file;
466                         }
467                 }
468         }
469         return \@changed, \@internal_changed;
472 sub calculate_old_links ($$) {
473         my ($changed, $del)=@_;
474         my %oldlink_targets;
475         foreach my $file (@$changed, @$del) {
476                 my $page=pagename($file);
477                 if (exists $oldlinks{$page}) {
478                         foreach my $l (@{$oldlinks{$page}}) {
479                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
480                         }
481                 }
482         }
483         return \%oldlink_targets;
486 sub derender_internal ($) {
487         my $file=shift;
488         my $page=pagename($file);
489         delete $depends{$page};
490         delete $depends_simple{$page};
491         foreach my $old (@{$renderedfiles{$page}}) {
492                 delete $destsources{$old};
493         }
494         $renderedfiles{$page}=[];
497 sub render_linkers ($) {
498         my $f=shift;
499         my $p=pagename($f);
500         foreach my $page (keys %{$backlinks{$p}}) {
501                 my $file=$pagesources{$page};
502                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
503         }
506 sub remove_unrendered () {
507         foreach my $src (keys %rendered) {
508                 my $page=pagename($src);
509                 foreach my $file (@{$oldrenderedfiles{$page}}) {
510                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
511                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
512                                 prune($config{destdir}."/".$file);
513                         }
514                 }
515         }
518 sub link_types_changed ($$) {
519         # each is of the form { type => { link => 1 } }
520         my $new = shift;
521         my $old = shift;
523         return 0 if !defined $new && !defined $old;
524         return 1 if !defined $new || !defined $old;
526         while (my ($type, $links) = each %$new) {
527                 foreach my $link (keys %$links) {
528                         return 1 unless exists $old->{$type}{$link};
529                 }
530         }
532         while (my ($type, $links) = each %$old) {
533                 foreach my $link (keys %$links) {
534                         return 1 unless exists $new->{$type}{$link};
535                 }
536         }
538         return 0;
541 sub calculate_changed_links ($$$) {
542         my ($changed, $del, $oldlink_targets)=@_;
544         my (%backlinkchanged, %linkchangers);
546         foreach my $file (@$changed, @$del) {
547                 my $page=pagename($file);
549                 if (exists $links{$page}) {
550                         foreach my $l (@{$links{$page}}) {
551                                 my $target=bestlink($page, $l);
552                                 if (! exists $oldlink_targets->{$page}{$l} ||
553                                     $target ne $oldlink_targets->{$page}{$l}) {
554                                         $backlinkchanged{$target}=1;
555                                         $linkchangers{lc($page)}=1;
556                                 }
557                                 delete $oldlink_targets->{$page}{$l};
558                         }
559                 }
560                 if (exists $oldlink_targets->{$page} &&
561                     %{$oldlink_targets->{$page}}) {
562                         foreach my $target (values %{$oldlink_targets->{$page}}) {
563                                 $backlinkchanged{$target}=1;
564                         }
565                         $linkchangers{lc($page)}=1;
566                 }
568                 # we currently assume that changing the type of a link doesn't
569                 # change backlinks
570                 if (!exists $linkchangers{lc($page)}) {
571                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
572                                 $linkchangers{lc($page)}=1;
573                         }
574                 }
575         }
577         return \%backlinkchanged, \%linkchangers;
580 sub render_dependent ($$$$$$$) {
581         my ($files, $new, $internal_new, $del, $internal_del,
582                 $internal_changed, $linkchangers)=@_;
584         my @changed=(keys %rendered, @$del);
585         my @exists_changed=(@$new, @$del);
586         
587         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
588         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
589          
590         foreach my $f (@$files) {
591                 next if $rendered{$f};
592                 my $p=pagename($f);
593                 my $reason = undef;
594         
595                 if (exists $depends_simple{$p}) {
596                         foreach my $d (keys %{$depends_simple{$p}}) {
597                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
598                                      $lc_changed{$d})
599                                     ||
600                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
601                                      $lc_exists_changed{$d})
602                                     ||
603                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
604                                      $linkchangers->{$d})
605                                 ) {
606                                         $reason = $d;
607                                         last;
608                                 }
609                         }
610                 }
611         
612                 if (exists $depends{$p} && ! defined $reason) {
613                         foreach my $dep (keys %{$depends{$p}}) {
614                                 my $sub=pagespec_translate($dep);
615                                 next unless defined $sub;
617                                 # only consider internal files
618                                 # if the page explicitly depends
619                                 # on such files
620                                 my $internal_dep=$dep =~ /internal\(/;
622                                 my $in=sub {
623                                         my $list=shift;
624                                         my $type=shift;
625                                         foreach my $file (@$list) {
626                                                 next if $file eq $f;
627                                                 my $page=pagename($file);
628                                                 if ($sub->($page, location => $p)) {
629                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
630                                                                 next unless $linkchangers->{lc($page)};
631                                                         }
632                                                         return $page;
633                                                 }
634                                         }
635                                         return undef;
636                                 };
638                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
639                                         last if $reason =
640                                                 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
641                                         last if $internal_dep && ($reason =
642                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
643                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
644                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
645                                 }
646                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
647                                         last if $reason = 
648                                                 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
649                                         last if $internal_dep && ($reason =
650                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
651                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
652                                 }
653                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
654                                         last if $reason =
655                                                 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
656                                         last if $internal_dep && ($reason =
657                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
658                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
659                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
660                                 }
661                         }
662                 }
663         
664                 if (defined $reason) {
665                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
666                         return 1;
667                 }
668         }
670         return 0;
673 sub render_backlinks ($) {
674         my $backlinkchanged=shift;
675         foreach my $link (keys %$backlinkchanged) {
676                 my $linkfile=$pagesources{$link};
677                 if (defined $linkfile) {
678                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
679                 }
680         }
683 sub gen_autofile ($$$) {
684         my $autofile=shift;
685         my $pages=shift;
686         my $del=shift;
687         
688         if (srcfile($autofile, 1)) {
689                 return 0;
690         }
692         my ($file, $page) = verify_src_file("$config{srcdir}/$autofile", $config{srcdir});
693         
694         if ((!defined $file) ||
695             (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile_deleted})) {
696                 return 0;
697         }
698         
699         if ($pages->{$page}) {
700                 return 0;
701         }
703         if (grep { $_ eq $file } @$del) {
704                 $wikistate{$autofiles{$autofile}{generator}}{autofile_deleted}=1;
705                 return 0;
706         }
708         $autofiles{$autofile}{generator}->();
709         $pages->{$page}=1;
710         return 1;
714 sub refresh () {
715         srcdir_check();
716         run_hooks(refresh => sub { shift->() });
717         my ($files, $pages)=find_src_files();
718         my ($new, $internal_new)=find_new_files($files);
719         my ($del, $internal_del)=find_del_files($pages);
720         my ($changed, $internal_changed)=find_changed($files);
721         run_hooks(needsbuild => sub { shift->($changed) });
722         my $oldlink_targets=calculate_old_links($changed, $del);
724         foreach my $file (@$changed) {
725                 scan($file);
726         }
728         foreach my $autofile (keys %autofiles) {
729                 if (gen_autofile($autofile, $pages, $del)) {
730                         push @{$files}, $autofile;
731                         push @{$new}, $autofile if find_new_files([$autofile]);
732                         push @{$changed}, $autofile if find_changed([$autofile]);
733                         
734                         scan($autofile);
735                 }
736         }
738         calculate_links();
739         
740         remove_del(@$del, @$internal_del);
742         foreach my $file (@$changed) {
743                 render($file, sprintf(gettext("building %s"), $file));
744         }
745         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
746                 derender_internal($file);
747         }
749         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
750                 $del, $oldlink_targets);
752         foreach my $file (@$new, @$del) {
753                 render_linkers($file);
754         }
755         
756         if (@$changed || @$internal_changed ||
757             @$del || @$internal_del || @$internal_new) {
758                 1 while render_dependent($files, $new, $internal_new,
759                         $del, $internal_del, $internal_changed,
760                         $linkchangers);
761         }
763         render_backlinks($backlinkchanged);
764         remove_unrendered();
766         if (@$del) {
767                 run_hooks(delete => sub { shift->(@$del) });
768         }
769         if (%rendered) {
770                 run_hooks(change => sub { shift->(keys %rendered) });
771         }
774 sub clean_rendered {
775         lockwiki();
776         loadindex();
777         remove_unrendered();
778         foreach my $page (keys %oldrenderedfiles) {
779                 foreach my $file (@{$oldrenderedfiles{$page}}) {
780                         prune($config{destdir}."/".$file);
781                 }
782         }
785 sub commandline_render () {
786         lockwiki();
787         loadindex();
788         unlockwiki();
790         my $srcfile=possibly_foolish_untaint($config{render});
791         my $file=$srcfile;
792         $file=~s/\Q$config{srcdir}\E\/?//;
794         my $type=pagetype($file);
795         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
796         my $content=readfile($srcfile);
797         my $page=pagename($file);
798         $pagesources{$page}=$file;
799         $content=filter($page, $page, $content);
800         $content=preprocess($page, $page, $content);
801         $content=linkify($page, $page, $content);
802         $content=htmlize($page, $page, $type, $content);
803         $pagemtime{$page}=(stat($srcfile))[9];
804         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
806         print genpage($page, $content);
807         exit 0;