po plugin(filter): redesign temp file management
... in a way compatible with various File::Temp versions. The result is far from being perfect (see comments in the code for details), but it does work. Signed-off-by: intrigeri <intrigeri@boum.org>master
parent
4097834357
commit
3e0c52c524
|
@ -303,10 +303,24 @@ sub filter (@) { #{{{
|
||||||
# CRLF line terminators make poor Locale::Po4a feel bad
|
# CRLF line terminators make poor Locale::Po4a feel bad
|
||||||
$content=~s/\r\n/\n/g;
|
$content=~s/\r\n/\n/g;
|
||||||
|
|
||||||
# Locale::Po4a needs an input file, and I'm too lazy to learn
|
# Implementation notes
|
||||||
# how to disguise a variable as a file
|
#
|
||||||
my $infile = File::Temp->new(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
|
# 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
|
||||||
TMPDIR => 1)->filename;
|
# to learn how to disguise a variable as a file.
|
||||||
|
# 2. There are incompatibilities between some File::Temp versions
|
||||||
|
# (including 0.18, bundled with Lenny's perl-modules package)
|
||||||
|
# and others (e.g. 0.20, previously present in the archive as
|
||||||
|
# a standalone package): under certain circumstances, some
|
||||||
|
# return a relative filename, whereas others return an absolute one;
|
||||||
|
# we here use this module in a way that is at least compatible
|
||||||
|
# with 0.18 and 0.20. Beware, hit'n'run refactorers!
|
||||||
|
my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
|
||||||
|
DIR => File::Spec->tmpdir,
|
||||||
|
UNLINK => 1)->filename;
|
||||||
|
my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
|
||||||
|
DIR => File::Spec->tmpdir,
|
||||||
|
UNLINK => 1)->filename;
|
||||||
|
|
||||||
writefile(basename($infile), File::Spec->tmpdir, $content);
|
writefile(basename($infile), File::Spec->tmpdir, $content);
|
||||||
|
|
||||||
my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
|
my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
|
||||||
|
@ -324,10 +338,14 @@ sub filter (@) { #{{{
|
||||||
'file_in_charset' => 'utf-8',
|
'file_in_charset' => 'utf-8',
|
||||||
'file_out_charset' => 'utf-8',
|
'file_out_charset' => 'utf-8',
|
||||||
) or error("[po/filter:$infile]: failed to translate");
|
) or error("[po/filter:$infile]: failed to translate");
|
||||||
my $tmpout = File::Temp->new(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
|
$doc->write($outfile) or error("[po/filter:$infile] could not write $outfile");
|
||||||
TMPDIR => 1)->filename;
|
$content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile");
|
||||||
$doc->write($tmpout) or error("[po/filter:$infile] could not write $tmpout");
|
|
||||||
$content = readfile($tmpout) or error("[po/filter:$infile] could not read $tmpout");
|
# Unlinking should happen automatically, thanks to File::Temp,
|
||||||
|
# but it does not work here, probably because of the way writefile()
|
||||||
|
# and Locale::Po4a::write() work.
|
||||||
|
unlink $infile, $outfile;
|
||||||
|
|
||||||
$filtered{$page}{$destpage}=1;
|
$filtered{$page}{$destpage}=1;
|
||||||
return $content;
|
return $content;
|
||||||
} #}}}
|
} #}}}
|
||||||
|
|
Loading…
Reference in New Issue