+ my $cleanup = sub { unlink($newfile) };
+ open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
+ binmode($out) if ($binary);
+ if ($writer) {
+ $writer->(\*$out, $cleanup);
+ }
+ else {
+ print $out $content or error("failed writing to $newfile: $!", $cleanup);
+ }
+ close $out || error("failed saving $newfile: $!", $cleanup);
+ rename($newfile, "$destdir/$file") ||
+ error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
+
+ return 1;
+} #}}}
+
+my %cleared;
+sub will_render ($$;$) { #{{{
+ my $page=shift;
+ my $dest=shift;
+ my $clear=shift;
+
+ # Important security check.
+ if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
+ ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
+ error("$config{destdir}/$dest independently created, not overwriting with version from $page");
+ }
+
+ if (! $clear || $cleared{$page}) {
+ $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
+ }
+ else {
+ foreach my $old (@{$renderedfiles{$page}}) {
+ delete $destsources{$old};
+ }
+ $renderedfiles{$page}=[$dest];
+ $cleared{$page}=1;
+ }
+ $destsources{$dest}=$page;
+
+ return 1;