]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/po.pm
po: clarify code with some comments
[git.ikiwiki.info.git] / IkiWiki / Plugin / po.pm
index c85e881f154d7de030360a739d23eae10b47f0c4..9967e41586669b2860dfe8fbf2f3a0d88f8b5a9b 100644 (file)
@@ -17,6 +17,7 @@ use File::Copy;
 use File::Spec;
 use File::Temp;
 use Memoize;
+use UNIVERSAL;
 
 my %translations;
 my @origneedsbuild;
@@ -36,6 +37,7 @@ sub import { #{{{
        hook(type => "getsetup", id => "po", call => \&getsetup);
        hook(type => "checkconfig", id => "po", call => \&checkconfig);
        hook(type => "needsbuild", id => "po", call => \&needsbuild);
+       hook(type => "scan", id => "po", call => \&scan, last =>1);
        hook(type => "filter", id => "po", call => \&filter);
        hook(type => "htmlize", id => "po", call => \&htmlize);
        hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
@@ -153,16 +155,21 @@ sub pofile ($$) { #{{{
        return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
 } #}}}
 
+sub pofiles ($) { #{{{
+       my $masterfile=shift;
+       return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
+} #}}}
+
 sub refreshpot ($) { #{{{
        my $masterfile=shift;
 
        my $potfile=potfile($masterfile);
        my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
        my $doc=Locale::Po4a::Chooser::new('text',%options);
-       $doc->read($masterfile);
        $doc->{TT}{utf_mode} = 1;
        $doc->{TT}{file_in_charset} = 'utf-8';
        $doc->{TT}{file_out_charset} = 'utf-8';
+       $doc->read($masterfile);
        # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
        # this is undocument use of internal Locale::Po4a::TransTractor's data,
        # compulsory since this module prevents us from using the porefs option.
@@ -213,6 +220,41 @@ sub needsbuild () { #{{{
        }
 } #}}}
 
+sub scan (@) { #{{{
+       my %params=@_;
+       my $page=$params{page};
+       my $content=$params{content};
+
+       return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
+
+       if (istranslation($page)) {
+               my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+               foreach my $destpage (@{$links{$page}}) {
+                       if (istranslatable($destpage)) {
+                               # replace one occurence of $destpage in $links{$page}
+                               # (we only want to replace the one that was added by
+                               # IkiWiki::Plugin::link::scan, other occurences may be
+                               # there for other reasons)
+                               for (my $i=0; $i<@{$links{$page}}; $i++) {
+                                       if (@{$links{$page}}[$i] eq $destpage) {
+                                               @{$links{$page}}[$i] = $destpage . '.' . $curlang;
+                                               last;
+                                       }
+                               }
+                       }
+               }
+       }
+       elsif (! istranslatable($page) && ! istranslation($page)) {
+               foreach my $destpage (@{$links{$page}}) {
+                       if (istranslatable($destpage)) {
+                               map {
+                                       push @{$links{$page}}, $destpage . '.' . $_;
+                               } (keys %{$config{po_slave_languages}});
+                       }
+               }
+       }
+} #}}}
+
 sub mytargetpage ($$) { #{{{
        my $page=shift;
        my $ext=shift;
@@ -454,7 +496,7 @@ sub pagetemplate (@) { #{{{
                elsif (istranslation($page)) {
                        add_depends($page, $masterpage);
                        foreach my $translation (values %{$translations{$masterpage}}) {
-                               add_depends($page, $translation);
+                               add_depends($page, $translation) unless $page eq $translation;
                        }
                }
        }
@@ -475,7 +517,9 @@ sub pagetemplate (@) { #{{{
                                                        linktext => gettext("Discussion"),
                                                        ));
        }
-       # remove broken parentlink to ./index.html on home page's translations
+       # Remove broken parentlink to ./index.html on home page's translations.
+       # It works because this hook has the "last" parameter set, to ensure it
+       # runs after parentlinks' own pagetemplate hook.
        if ($template->param('parentlinks')
            && istranslation($page)
            && $masterpage eq "index") {
@@ -493,6 +537,9 @@ sub change(@) { #{{{
                next unless istranslatable($page);
                my $file=srcfile($pagesources{$page});
                my $updated_pot_file=0;
+               # Only refresh Pot file if it does not exist, or if
+               # $pagesources{$page} was changed: don't if only the HTML was
+               # refreshed, e.g. because of a dependency.
                if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
                    || ! -e potfile($file)) {
                        refreshpot($file);