X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/22d982243716bb04f56e4e78e573e3ec49077309..2ff705367b44447c2a9159d14f80880c38556ce4:/IkiWiki/Plugin/po.pm diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index a2010e1be..f3530faf3 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -39,7 +39,6 @@ sub import { 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 => "rescan", id => "po", call => \&rescan); hook(type => "filter", id => "po", call => \&filter); hook(type => "htmlize", id => "po", call => \&htmlize); hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1); @@ -224,38 +223,61 @@ sub needsbuild () { } } -# Massage the recorded state of internal links so that: -# - it matches the actually generated links, rather than the links as written -# in the pages' source -# - backlinks are consistent in all cases sub scan (@) { my %params=@_; my $page=$params{page}; my $content=$params{content}; - - if (istranslation($page)) { - foreach my $destpage (@{$links{$page}}) { - if (istranslatable($destpage)) { - # replace the occurence of $destpage in $links{$page} - for (my $i=0; $i<@{$links{$page}}; $i++) { - if (@{$links{$page}}[$i] eq $destpage) { - @{$links{$page}}[$i] = $destpage . '.' . lang($page); - last; - } - } + my $run_by_po=$params{run_by_po}; + + # Massage the recorded state of internal links so that: + # - it matches the actually generated links, rather than the links as + # written in the pages' source + # - backlinks are consistent in all cases + + # A second scan pass is made over translation pages, so as an + # optimization, we only do so on the second pass in this case, + # i.e. when this hook is called by itself. + if ($run_by_po && istranslation($page)) { + # replace the occurence of $destpage in $links{$page} + my @orig_links = @{$links{$page}}; + $links{$page} = []; + foreach my $destpage (@orig_links) { + if (istranslatedto($destpage, lang($page))) { + add_link($page, $destpage . '.' . lang($page)); + } + else { + add_link($page, $destpage); } } } - elsif (! istranslatable($page) && ! istranslation($page)) { + # No second scan pass is done for a non-translation page, so + # links massaging must happen on first pass in this case. + elsif (! $run_by_po && ! istranslatable($page) && ! istranslation($page)) { foreach my $destpage (@{$links{$page}}) { if (istranslatable($destpage)) { # make sure any destpage's translations has # $page in its backlinks - push @{$links{$page}}, - values %{otherlanguages_pages($destpage)}; + foreach my $link (values %{otherlanguages_pages($destpage)}) { + add_link($page, $link); + } } } } + + # Re-run the preprocess hooks in scan mode, then the scan hooks, + # over the po-to-markup converted content + return if $run_by_po; # avoid looping endlessly + return unless istranslation($page); + $content = po_to_markup($page, $content); + require IkiWiki; + IkiWiki::preprocess($page, $page, $content, 1); + IkiWiki::run_hooks(scan => sub { + shift->( + page => $page, + content => $content, + run_by_po => 1, + ); + }); } # We use filter to convert PO to the master page's format, @@ -273,26 +295,6 @@ sub filter (@) { return $content; } -# re-run the scan hooks and run the preprocess ones in scan -# mode on the po-to-markup converted content -sub rescan (@) { - my %params=@_; - my $page=$params{page}; - my $content=$params{content}; - - return unless istranslation($page); - - $content = po_to_markup($page, $content); - require IkiWiki; - IkiWiki::run_hooks(scan => sub { - shift->( - page => $page, - content => $content, - ); - }); - IkiWiki::preprocess($page, $page, $content, 1); -} - sub htmlize (@) { my %params=@_; @@ -583,7 +585,7 @@ sub mybestlink ($$) { my $res=$origsubs{'bestlink'}->(masterpage($page), $link); my @caller = caller(1); if (length $res - && istranslatable($res) + && istranslatedto($res, lang($page)) && istranslation($page) && !(exists $caller[3] && defined $caller[3] && ($caller[3] eq "IkiWiki::PageSpec::match_link"))) { @@ -764,6 +766,15 @@ sub istranslatable ($) { return; } +sub istranslatedto ($$) { + my $page=shift; + my $destlang = shift; + + $page=~s#^/##; + return 0 unless istranslatable($page); + exists $pagesources{otherlanguage_page($page, $destlang)}; +} + sub _istranslation ($) { my $page=shift; @@ -840,7 +851,10 @@ sub otherlanguages_codes ($) { foreach my $lang ($config{po_master_language}{code}, @slavelanguages) { next if $lang eq $curlang; - push @ret, $lang; + if ($lang eq $config{po_master_language}{code} || + istranslatedto(masterpage($page), $lang)) { + push @ret, $lang; + } } return \@ret; }