+ my $page = $params{page};
+ my $content = $params{content};
+ my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ my $masterfile = srcfile($pagesources{$masterpage});
+
+ # 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 percenttranslated ($) { #{{{
+ my $page=shift;
+ return "N/A" unless (istranslation($page));
+ my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ my $file=srcfile($pagesources{$page});
+ my $masterfile = srcfile($pagesources{$masterpage});
+ my (@pos,@masters);
+ push @pos,$file;
+ 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/percenttranslated:$file]: failed to translate");
+ my ($percent,$hit,$queries) = $doc->stats();
+ return $percent;
+} #}}}
+
+sub otherlanguages ($) { #{{{
+ my $page=shift;
+ my @ret;
+ if (istranslatable($page)) {
+ foreach my $lang (sort keys %{$translations{$page}}) {
+ my $translation = $translations{$page}{$lang};
+ push @ret, {
+ url => urlto($translation, $page),
+ code => $lang,
+ language => $config{po_slave_languages}{$lang},
+ percent => percenttranslated($translation),
+ };
+ }
+ }
+ elsif (istranslation($page)) {
+ my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ push @ret, {
+ url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
+ code => $config{po_master_language}{code},
+ language => $config{po_master_language}{name},
+ master => 1,
+ };
+ foreach my $lang (sort keys %{$translations{$masterpage}}) {
+ push @ret, {
+ url => urlto($translations{$masterpage}{$lang}, $page),
+ code => $lang,
+ language => $config{po_slave_languages}{$lang},
+ percent => percenttranslated($translations{$masterpage}{$lang}),
+ } unless ($lang eq $curlang);
+ }
+ }
+ return @ret;
+} #}}}
+
+sub pagetemplate (@) { #{{{
+ my %params=@_;
+ my $page=$params{page};
+ my $destpage=$params{destpage};
+ my $template=$params{template};
+
+ if (istranslation($page) && $template->query(name => "percenttranslated")) {
+ $template->param(percenttranslated => percenttranslated($page));
+ }
+ if ($template->query(name => "istranslation")) {
+ $template->param(istranslation => istranslation($page));
+ }
+ if ($template->query(name => "istranslatable")) {
+ $template->param(istranslatable => istranslatable($page));
+ }
+ if ($template->query(name => "otherlanguages")) {
+ $template->param(otherlanguages => [otherlanguages($page)]);
+ if (istranslatable($page)) {
+ foreach my $translation (values %{$translations{$page}}) {
+ add_depends($page, $translation);
+ }
+ }
+ elsif (istranslation($page)) {
+ my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ add_depends($page, $masterpage);
+ foreach my $translation (values %{$translations{$masterpage}}) {
+ add_depends($page, $translation);
+ }
+ }
+ }
+ # Rely on IkiWiki::Render's genpage() to decide wether
+ # a discussion link should appear on $page; this is not
+ # totally accurate, though: some broken links may be generated
+ # when cgiurl is disabled.
+ # This compromise avoids some code duplication, and will probably
+ # prevent future breakage when ikiwiki internals change.
+ # Known limitations are preferred to future random bugs.
+ if ($template->param('discussionlink') && istranslation($page)) {
+ my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ $template->param('discussionlink' => htmllink(
+ $page,
+ $destpage,
+ $masterpage . '/' . gettext("Discussion"),
+ noimageinline => 1,
+ forcesubpage => 0,
+ linktext => gettext("Discussion"),
+ ));
+ }
+} # }}}
+
+sub istranslatable ($) { #{{{
+ my $page=shift;
+ my $file=$pagesources{$page};
+
+ if (! defined $file
+ || (defined pagetype($file) && pagetype($file) eq 'po')
+ || $file =~ /\.pot$/) {
+ return 0;
+ }
+ return pagespec_match($page, $config{po_translatable_pages});
+} #}}}
+
+sub _istranslation ($) { #{{{
+ my $page=shift;
+ my $file=$pagesources{$page};
+ if (! defined $file) {
+ return IkiWiki::FailReason->new("no file specified");
+ }
+
+ if (! defined $file
+ || ! defined pagetype($file)
+ || ! pagetype($file) eq 'po'
+ || $file =~ /\.pot$/) {
+ return 0;
+ }
+
+ my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ if (! defined $masterpage || ! defined $lang
+ || ! (length($masterpage) > 0) || ! (length($lang) > 0)
+ || ! defined $pagesources{$masterpage}
+ || ! defined $config{po_slave_languages}{$lang}) {
+ return 0;
+ }
+
+ return istranslatable($masterpage);
+} #}}}
+
+sub istranslation ($) { #{{{
+ my $page=shift;
+ if (_istranslation($page)) {
+ my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
+ return 1;
+ }
+ return 0;
+} #}}}
+
+package IkiWiki::PageSpec;
+use warnings;
+use strict;
+use IkiWiki 2.00;
+
+sub match_istranslation ($;@) { #{{{
+ my $page=shift;
+ if (IkiWiki::Plugin::po::istranslation($page)) {
+ return IkiWiki::SuccessReason->new("is a translation page");
+ }
+ else {
+ return IkiWiki::FailReason->new("is not a translation page");
+ }
+} #}}}
+
+sub match_istranslatable ($;@) { #{{{
+ my $page=shift;
+ if (IkiWiki::Plugin::po::istranslatable($page)) {
+ return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
+ }
+ else {
+ return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
+ }
+} #}}}
+
+sub match_lang ($$;@) { #{{{
+ my $page=shift;
+ my $wanted=shift;
+ my $regexp=IkiWiki::glob2re($wanted);
+ my $lang;
+ my $masterpage;
+
+ if (IkiWiki::Plugin::po::istranslation($page)) {
+ ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ }
+ else {
+ $lang = $config{po_master_language}{code};
+ }
+
+ if ($lang!~/^$regexp$/i) {
+ return IkiWiki::FailReason->new("file language is $lang, not $wanted");
+ }
+ else {
+ return IkiWiki::SuccessReason->new("file language is $wanted");
+ }
+} #}}}
+
+sub match_currentlang ($$;@) { #{{{
+ my $page=shift;
+ shift;
+ my %params=@_;
+ my ($currentmasterpage, $currentlang, $masterpage, $lang);
+
+ return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
+
+ if (IkiWiki::Plugin::po::istranslation($params{location})) {
+ ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
+ }
+ else {
+ $currentlang = $config{po_master_language}{code};
+ }
+
+ if (IkiWiki::Plugin::po::istranslation($page)) {
+ ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+ }
+ else {
+ $lang = $config{po_master_language}{code};
+ }
+
+ if ($lang eq $currentlang) {
+ return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
+ }
+ else {
+ return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
+ }