]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/po.pm
po plugin: po_slave_languages is a hash, rather than a hash of hashes
[git.ikiwiki.info.git] / IkiWiki / Plugin / po.pm
index d60ade4992b7afbc6db27f8d51deb3478fe91772..0c95f6c14ed2ae7b8d2c7768303ba816d73ac742 100644 (file)
@@ -10,6 +10,11 @@ use IkiWiki 2.00;
 use Encode;
 use Locale::Po4a::Chooser;
 use File::Temp;
+use Memoize;
+
+my %translations;
+memoize("istranslatable");
+memoize("_istranslation");
 
 sub import {
        hook(type => "getsetup", id => "po", call => \&getsetup);
@@ -40,9 +45,10 @@ sub getsetup () { #{{{
                },
                po_slave_languages => {
                        type => "string",
-                       example => {'fr' => { 'name' => 'Français' },
-                                   'es' => { 'name' => 'Castellano' },
-                                   'de' => { 'name' => 'Deutsch' },
+                       example => {
+                               'fr' => 'Français',
+                               'es' => 'Castellano',
+                               'de' => 'Deutsch'
                        },
                        description => "slave languages (PO files)",
                        safe => 1,
@@ -88,9 +94,10 @@ sub checkconfig () { #{{{
 sub scan (@) { #{{{
        my %params=@_;
        my $page=$params{page};
-
-       # FIXME: cache (or memoize) the list of translatable/translation pages,
-       # and/or istranslation/istranslated results
+       # let's build %translations, using istranslation's
+       # side-effect, so that we can consider it is complete at
+       # preprocess time
+       istranslation($page);
 } #}}}
 
 sub targetpage (@) { #{{{
@@ -140,16 +147,18 @@ sub tweakbestlink ($$) { #{{{
        return $link;
 } #}}}
 
+our %filtered;
 # We use filter to convert PO to the master page's type,
 # since other plugins 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}));
 
        # decide if this is a PO file that should be converted into a translated document,
        # and perform various sanity checks
-       if (! istranslation($page)) {
+       if (! istranslation($page) || $filtered{$page}{$destpage}) {
                return $content;
        }
 
@@ -174,6 +183,7 @@ sub filter (@) { #{{{
        my $tmpout = $tmpfh->filename;
        $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
        $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
+       $filtered{$page}{$destpage}=1;
        return $content;
 } #}}}
 
@@ -200,7 +210,7 @@ sub istranslatable ($) { #{{{
        return pagespec_match($page, $config{po_translatable_pages});
 } #}}}
 
-sub istranslation ($) { #{{{
+sub _istranslation ($) { #{{{
        my $page=shift;
        my $file=$pagesources{$page};
        if (! defined $file) {
@@ -225,6 +235,15 @@ sub istranslation ($) { #{{{
        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;
@@ -233,7 +252,7 @@ use IkiWiki 2.00;
 
 sub match_istranslation ($;@) { #{{{
        my $page=shift;
-       if (IkiWiki::Plugins::po::istranslation($page)) {
+       if (IkiWiki::Plugin::po::istranslation($page)) {
                return IkiWiki::SuccessReason->new("is a translation page");
        }
        else {
@@ -243,7 +262,7 @@ sub match_istranslation ($;@) { #{{{
 
 sub match_istranslatable ($;@) { #{{{
        my $page=shift;
-       if (IkiWiki::Plugins::po::istranslatable($page)) {
+       if (IkiWiki::Plugin::po::istranslatable($page)) {
                return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
        }
        else {