+# We use filter to convert PO to the master page's format,
+# since the rest of ikiwiki should not work on PO files.
+sub filter (@) { #{{{
+ my %params = @_;
+
+ my $page = $params{page};
+ my $destpage = $params{destpage};
+ my $content = decode_utf8(encode_utf8($params{content}));
+
+ return $content if ( ! istranslation($page)
+ || alreadyfiltered($page, $destpage) );
+
+ # CRLF line terminators make poor Locale::Po4a feel bad
+ $content=~s/\r\n/\n/g;
+
+ # Implementation notes
+ #
+ # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
+ # 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);
+
+ my $masterfile = srcfile($pagesources{masterpage($page)});
+ my (@pos,@masters);
+ push @pos,$infile;
+ push @masters,$masterfile;
+ my %options = (
+ "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
+ );
+ my $doc=Locale::Po4a::Chooser::new('text',%options);
+ $doc->process(
+ 'po_in_name' => \@pos,
+ 'file_in_name' => \@masters,
+ 'file_in_charset' => 'utf-8',
+ 'file_out_charset' => 'utf-8',
+ ) or error("[po/filter:$page]: failed to translate");
+ $doc->write($outfile) or error("[po/filter:$page] could not write $outfile");
+ $content = readfile($outfile) or error("[po/filter:$page] could not read $outfile");
+
+ # 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;
+
+ setalreadyfiltered($page, $destpage);
+ return $content;
+} #}}}
+
+sub htmlize (@) { #{{{
+ my %params=@_;
+
+ my $page = $params{page};
+ my $content = $params{content};
+ my $masterfile = srcfile($pagesources{masterpage($page)});
+
+ # force content to be htmlize'd as if it was the same type as the master page
+ return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
+} #}}}
+
+sub pagetemplate (@) { #{{{
+ my %params=@_;
+ my $page=$params{page};
+ my $destpage=$params{destpage};
+ my $template=$params{template};
+
+ my ($masterpage, $lang) = istranslation($page);