]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/po.pm
po: implemented linking/backlinks specification for po_link_to=negotiated
[git.ikiwiki.info.git] / IkiWiki / Plugin / po.pm
index c8ec37c228378d600ae526086b5fe97fb2ac503d..acc133042829a609f1af26ac5041770e41a2247e 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);
@@ -48,6 +50,8 @@ sub import { #{{{
        inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
        $origsubs{'targetpage'}=\&IkiWiki::targetpage;
        inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
+       $origsubs{'urlto'}=\&IkiWiki::urlto;
+       inject(name => "IkiWiki::urlto", call => \&myurlto);
 } #}}}
 
 sub getsetup () { #{{{
@@ -151,6 +155,11 @@ 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;
 
@@ -211,6 +220,42 @@ sub needsbuild () { #{{{
        }
 } #}}}
 
+sub scan (@) { #{{{
+       my %params=@_;
+       my $page=$params{page};
+       my $content=$params{content};
+
+       return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
+       return unless $config{'po_link_to'} eq 'negotiated';
+
+       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;
@@ -256,6 +301,22 @@ sub urlto_with_orig_beautiful_urlpath($$) { #{{{
        return $res;
 } #}}}
 
+sub myurlto ($$;$) { #{{{
+       my $to=shift;
+       my $from=shift;
+       my $absolute=shift;
+
+       # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
+       if (! length $to
+           && $config{po_link_to} eq "current"
+           && istranslation($from)
+           && istranslatable('index')) {
+               my ($masterpage, $curlang) = ($from =~ /(.*)[.]([a-z]{2})$/);
+               return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . $curlang . ".$config{htmlext}");
+       }
+       return $origsubs{'urlto'}->($to,$from,$absolute);
+} #}}}
+
 sub mybestlink ($$) { #{{{
        my $page=shift;
        my $link=shift;