X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/646d7bf6a3c9d4f153f8129400a24ad147dcd67c..532ec56a5f2131809f8a2709f2f1d5c1e5d0a19c:/IkiWiki/Plugin/po.pm diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 7bc702eca..a37230b88 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -202,6 +202,15 @@ sub refreshpofiles ($@) { #{{{ } } #}}} +sub buildtranslationscache() { #{{{ + # use istranslation's side-effect + map istranslation($_), (keys %pagesources); +} #}}} + +sub resettranslationscache() { #{{{ + undef %translations; +} #}}} + sub needsbuild () { #{{{ my $needsbuild=shift; @@ -209,8 +218,7 @@ sub needsbuild () { #{{{ # a given master page was rendered because its source file was changed @origneedsbuild=(@$needsbuild); - # build %translations, using istranslation's side-effect - map istranslation($_), (keys %pagesources); + buildtranslationscache(); # make existing translations depend on the corresponding master page foreach my $master (keys %translations) { @@ -335,6 +343,31 @@ sub mybestlink ($$) { #{{{ return ""; } #}}} +sub alreadyfiltered($$) { #{{{ + my $page=shift; + my $destpage=shift; + + return ( exists $filtered{$page}{$destpage} + && $filtered{$page}{$destpage} eq 1 ); +} #}}} +sub setalreadyfiltered($$) { #{{{ + my $page=shift; + my $destpage=shift; + + $filtered{$page}{$destpage}=1; +} #}}} +sub unsetalreadyfiltered($$) { #{{{ + my $page=shift; + my $destpage=shift; + + if (exists $filtered{$page}{$destpage}) { + delete $filtered{$page}{$destpage}; + } +} #}}} +sub resetalreadyfiltered() { #{{{ + undef %filtered; +} #}}} + # We use filter to convert PO to the master page's format, # since the rest of ikiwiki should not work on PO files. sub filter (@) { #{{{ @@ -345,8 +378,7 @@ sub filter (@) { #{{{ my $content = decode_utf8(encode_utf8($params{content})); return $content if ( ! istranslation($page) - || ( exists $filtered{$page}{$destpage} - && $filtered{$page}{$destpage} eq 1 )); + || alreadyfiltered($page, $destpage) ); # CRLF line terminators make poor Locale::Po4a feel bad $content=~s/\r\n/\n/g; @@ -385,16 +417,16 @@ sub filter (@) { #{{{ 'file_in_name' => \@masters, 'file_in_charset' => 'utf-8', 'file_out_charset' => 'utf-8', - ) or error("[po/filter:$infile]: failed to translate"); - $doc->write($outfile) or error("[po/filter:$infile] could not write $outfile"); - $content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile"); + ) or error("[po/filter:$page]: failed to translate"); + $doc->write($outfile) or error("[po/filter:$page] could not write $outfile"); + $content = readfile($outfile) or error("[po/filter:$page] could not read $outfile"); # Unlinking should happen automatically, thanks to File::Temp, # but it does not work here, probably because of the way writefile() # and Locale::Po4a::write() work. unlink $infile, $outfile; - $filtered{$page}{$destpage}=1; + setalreadyfiltered($page, $destpage); return $content; } #}}} @@ -429,7 +461,7 @@ sub percenttranslated ($) { #{{{ 'file_in_name' => \@masters, 'file_in_charset' => 'utf-8', 'file_out_charset' => 'utf-8', - ) or error("[po/percenttranslated:$file]: failed to translate"); + ) or error("[po/percenttranslated:$page]: failed to translate"); my ($percent,$hit,$queries) = $doc->stats(); return $percent; } #}}} @@ -496,7 +528,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; } } } @@ -517,7 +549,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") { @@ -535,6 +569,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); @@ -564,8 +601,8 @@ sub change(@) { #{{{ IkiWiki::rcs_update(); } # Reinitialize module's private variables. - undef %filtered; - undef %translations; + resetalreadyfiltered(); + resettranslationscache(); # Trigger a wiki refresh. require IkiWiki::Render; IkiWiki::refresh(); @@ -577,9 +614,7 @@ sub editcontent () { #{{{ my %params=@_; # as we're previewing or saving a page, the content may have # changed, so tell the next filter() invocation it must not be lazy - if (exists $filtered{$params{page}}{$params{page}}) { - delete $filtered{$params{page}}{$params{page}}; - } + unsetalreadyfiltered($params{page}, $params{page}); return $params{content}; } #}}}