From daa04ae43612f2c48c6ec3e1c5dfa0ee6fda6653 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Thu, 6 Nov 2008 17:28:04 +0100 Subject: po: fix link to homepage in special case ... by wrapping IkiWiki::urlto in order to workaround hard-coded /index.$config{htmlext}, which is wrong when usedirs=0 and po_link_to=current and translatable homepage Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index c8ec37c22..7902fa745 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -48,6 +48,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 () { #{{{ @@ -256,6 +258,21 @@ sub urlto_with_orig_beautiful_urlpath($$) { #{{{ return $res; } #}}} +sub myurlto ($$;$) { #{{{ + my $to=shift; + my $from=shift; + my $absolute=shift; + + 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; -- cgit v1.2.3 From 4879a7f033758c5ae79ff5bf2022245711699c93 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Fri, 7 Nov 2008 16:33:10 +0100 Subject: po: add comments Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 1 + 1 file changed, 1 insertion(+) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 7902fa745..c85e881f1 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -263,6 +263,7 @@ sub myurlto ($$;$) { #{{{ 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) -- cgit v1.2.3 From 3e341e64c2bcb0c6ae4deba8b4b27a24e90c9db6 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Fri, 7 Nov 2008 21:23:23 +0100 Subject: po: added pofiles() function, will use this soon Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index c85e881f1..643621a91 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -153,6 +153,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; -- cgit v1.2.3 From 34ab884242fd23139b7d4ccc9ab368468d501186 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Fri, 7 Nov 2008 21:27:00 +0100 Subject: po: implemented linking/backlinks specification for po_link_to=negotiated Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 38 ++++++++++++++++++++++++++++++++++++++ doc/plugins/po.mdwn | 7 +------ 2 files changed, 39 insertions(+), 6 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 643621a91..acc133042 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -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); @@ -218,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; diff --git a/doc/plugins/po.mdwn b/doc/plugins/po.mdwn index 4f6212ff4..a010dbb7e 100644 --- a/doc/plugins/po.mdwn +++ b/doc/plugins/po.mdwn @@ -249,12 +249,7 @@ be fixed by something like [[todo/using_meta_titles_for_parentlinks]]. #### `po_link_to = negotiated` -If a given translatable `sourcepage.mdwn` links to \[[destpage]], -`sourcepage.LL.po` also link to \[[destpage]], and the latter has the -master page *and* all its translations listed in the backlinks. On the -other hand, translations of `destpage` get none of these backlinks. -What would be nice is: - +This is now implemented: - if a translatable page links to another translatable page: the master destpage gets a backlink to the master sourcepage, and every slave destpage gets a backlink to the corresponding slave sourcepage -- cgit v1.2.3 From a0ac34607919eab8f25e5312366c9c06bcfb791b Mon Sep 17 00:00:00 2001 From: intrigeri Date: Fri, 7 Nov 2008 22:17:54 +0100 Subject: po: finished backlinks implementation Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 1 - doc/plugins/po.mdwn | 24 --------------------- t/po.t | 59 +++++++++++++++++++++++++++++++++++----------------- 3 files changed, 40 insertions(+), 44 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index acc133042..89bd99470 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -226,7 +226,6 @@ sub scan (@) { #{{{ 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})$/); diff --git a/doc/plugins/po.mdwn b/doc/plugins/po.mdwn index a010dbb7e..ddd0f5870 100644 --- a/doc/plugins/po.mdwn +++ b/doc/plugins/po.mdwn @@ -245,30 +245,6 @@ does. This is actually a duplicate for [[bugs/pagetitle_function_does_not_respect_meta_titles]], which might be fixed by something like [[todo/using_meta_titles_for_parentlinks]]. -### backlinks - -#### `po_link_to = negotiated` - -This is now implemented: -- if a translatable page links to another translatable page: the - master destpage gets a backlink to the master sourcepage, and every - slave destpage gets a backlink to the corresponding slave sourcepage -- if a translatable page links to a non-translatable page: the - destpage gets a backlink to the master sourcepage only -- if a non-translatable page links to a translatable page: every - master or slave destpage gets a backlink to the sourcepage -- if a non-translatable page links to another non-translatable page: - the default behavious is nice, don't change it - -#### `po_link_to = current` - -At first glance, backlinks seem to work nicely, but a more thorough -look is needed. - -#### `po_link_to = default` - -FIXME - Translation quality assurance ----------------------------- diff --git a/t/po.t b/t/po.t index e9d5ac35f..3074372d3 100755 --- a/t/po.t +++ b/t/po.t @@ -17,7 +17,7 @@ BEGIN { } } -use Test::More tests => 45; +use Test::More tests => 52; BEGIN { use_ok("IkiWiki"); } @@ -89,27 +89,48 @@ ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation"); ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation"); ### links +require IkiWiki::Render; + +sub refresh_n_scan(@) { + my @masterfiles_rel=@_; + foreach my $masterfile_rel (@masterfiles_rel) { + my $masterfile=srcfile($masterfile_rel); + IkiWiki::scan($masterfile_rel); + next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel)); + my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile); + IkiWiki::Plugin::po::refreshpot($masterfile); + IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles); + map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles; + } +} + writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]'); writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]'); writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]'); -map IkiWiki::Plugin::po::refreshpot(srcfile($_)), ('index.mdwn', 'translatable.mdwn'); -require IkiWiki::Render; -foreach my $masterfile_rel ('index.mdwn', 'translatable.mdwn') { - my $masterfile=srcfile($masterfile_rel); - my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile); - IkiWiki::Plugin::po::refreshpot($masterfile); - IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles); - IkiWiki::scan($masterfile_rel); - map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles; -} -IkiWiki::scan('nontranslatable.mdwn'); -is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], 'index'); -is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], 'index.es'); -is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], 'index.fr'); -is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], 'translatable'); -is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], 'translatable.es'); -is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], 'translatable.fr'); -is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], 'nontranslatable'); + +$config{po_link_to}='negotiated'; +$msgprefix="links (po_link_to=negotiated)"; +refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn'); +is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index"); +is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es"); +is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr"); +is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable"); +is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es"); +is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr"); +is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable"); + +$config{po_link_to}='current'; +$msgprefix="links (po_link_to=current)"; +refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn'); +use Data::Dumper; +print Dumper(%links); +is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index"); +is_deeply(\@{$links{'index.es'}}, [ map bestlink('index.es', $_), ('translatable.es', 'nontranslatable')], "$msgprefix index.es"); +is_deeply(\@{$links{'index.fr'}}, [ map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable')], "$msgprefix index.fr"); +is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable"); +is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es"); +is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr"); +is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable"); ### targetpage $config{usedirs}=0; -- cgit v1.2.3 From 646d7bf6a3c9d4f153f8129400a24ad147dcd67c Mon Sep 17 00:00:00 2001 From: intrigeri Date: Sat, 8 Nov 2008 21:32:04 +0100 Subject: po(refreshpot): define the input charset before read()'ing ... to prevent the use of Encode::Guess::guess_encoding() in Locale::Po4a::Transtractor (just a minor security measure, dependent on po4a internals, but we have no reason to think Encode::Guess is not safe). Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 89bd99470..7bc702eca 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -166,10 +166,10 @@ sub refreshpot ($) { #{{{ 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. -- cgit v1.2.3 From aef1b0f5035a1b6b6d35d831dc026118853d99d2 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 12:48:27 +0100 Subject: po: prevent a slave page to depend on itself It has never caused harm yet, but it might in the future. Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 7bc702eca..d2e6bbdbc 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -496,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; } } } -- cgit v1.2.3 From 6fc898df75223b93eba827847fd67934a65d7bee Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 13:00:05 +0100 Subject: po: clarify code with some comments Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index d2e6bbdbc..9967e4158 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -517,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") { @@ -535,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); -- cgit v1.2.3 From 00732f19b9806730ea90de90a3cef59dc858c625 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 13:22:50 +0100 Subject: po: clarify error messages Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 9967e4158..d475158c1 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -385,9 +385,9 @@ 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() @@ -429,7 +429,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; } #}}} -- cgit v1.2.3 From 3174b4c6f3451dd21bf91e2ae98173451a5c403f Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 13:49:14 +0100 Subject: po: abstract %filtered implementation details Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index d475158c1..5336de813 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -335,6 +335,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 +370,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; @@ -394,7 +418,7 @@ sub filter (@) { #{{{ # and Locale::Po4a::write() work. unlink $infile, $outfile; - $filtered{$page}{$destpage}=1; + setalreadyfiltered($page, $destpage); return $content; } #}}} @@ -569,7 +593,7 @@ sub change(@) { #{{{ IkiWiki::rcs_update(); } # Reinitialize module's private variables. - undef %filtered; + resetalreadyfiltered(); undef %translations; # Trigger a wiki refresh. require IkiWiki::Render; @@ -582,9 +606,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}; } #}}} -- cgit v1.2.3 From 532ec56a5f2131809f8a2709f2f1d5c1e5d0a19c Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 13:56:46 +0100 Subject: po: abstract implementation details for %translations building and resetting Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 5336de813..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) { @@ -594,7 +602,7 @@ sub change(@) { #{{{ } # Reinitialize module's private variables. resetalreadyfiltered(); - undef %translations; + resettranslationscache(); # Trigger a wiki refresh. require IkiWiki::Render; IkiWiki::refresh(); -- cgit v1.2.3 From 5575786803b163bf10f8f6d22f958568fad40d49 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 14:49:58 +0100 Subject: po: abstract "doing something for the same page in every other language" Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index a37230b88..e7f36e968 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -138,6 +138,24 @@ sub checkconfig () { #{{{ push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/; } #}}} +sub otherlanguages($) { #{{{ + my $page=shift; + + my %ret; + if (istranslatable($page)) { + %ret = %{$translations{$page}}; + } + elsif (istranslation($page)) { + my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/); + $ret{$config{po_master_language}{code}} = $masterpage; + foreach my $lang (sort keys %{$translations{$masterpage}}) { + next if $lang eq $curlang; + $ret{$lang} = $translations{$masterpage}{$lang}; + } + } + return \%ret; +} #}}} + sub potfile ($) { #{{{ my $masterfile=shift; @@ -222,9 +240,7 @@ sub needsbuild () { #{{{ # make existing translations depend on the corresponding master page foreach my $master (keys %translations) { - foreach my $slave (values %{$translations{$master}}) { - add_depends($slave, $master); - } + map add_depends($_, $master), values %{otherlanguages($master)}; } } #}}} @@ -466,7 +482,7 @@ sub percenttranslated ($) { #{{{ return $percent; } #}}} -sub otherlanguages ($) { #{{{ +sub otherlanguagesloop ($) { #{{{ my $page=shift; my @ret; @@ -519,18 +535,8 @@ sub pagetemplate (@) { #{{{ $template->param(istranslatable => istranslatable($page)); } if ($template->query(name => "otherlanguages")) { - $template->param(otherlanguages => [otherlanguages($page)]); - if (istranslatable($page)) { - foreach my $translation (values %{$translations{$page}}) { - add_depends($page, $translation); - } - } - elsif (istranslation($page)) { - add_depends($page, $masterpage); - foreach my $translation (values %{$translations{$masterpage}}) { - add_depends($page, $translation) unless $page eq $translation; - } - } + $template->param(otherlanguages => [otherlanguagesloop($page)]); + map add_depends($page, $_), (values %{otherlanguages($page)}); } # Rely on IkiWiki::Render's genpage() to decide wether # a discussion link should appear on $page; this is not -- cgit v1.2.3 From 38cdda66d7d318918aab13b5e3b8dbfebc9ffd1c Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 15:09:56 +0100 Subject: po: actually use pofiles() function, to enhance code clarity Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index e7f36e968..8ea852d04 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -584,12 +584,9 @@ sub change(@) { #{{{ $updated_pot_file=1; } my @pofiles; - foreach my $lang (keys %{$config{po_slave_languages}}) { - my $pofile=pofile($file, $lang); - if ($updated_pot_file || ! -e $pofile) { - push @pofiles, $pofile; - } - } + map { + push @pofiles, $_ if ($updated_pot_file || ! -e $_); + } (pofiles($file)); if (@pofiles) { refreshpofiles($file, @pofiles); map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs}); -- cgit v1.2.3 From 1255acc02c7e0273e988c1cbbd1c1accc134c2bc Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 15:30:49 +0100 Subject: po: finish abstracting %filtered by putting it into a blackbox i.e. it is invisible for everyone but its accessors/mutators Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 8ea852d04..0666142cb 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -21,7 +21,6 @@ use UNIVERSAL; my %translations; my @origneedsbuild; -our %filtered; memoize("_istranslation"); memoize("percenttranslated"); @@ -359,30 +358,38 @@ sub mybestlink ($$) { #{{{ return ""; } #}}} -sub alreadyfiltered($$) { #{{{ - my $page=shift; - my $destpage=shift; +# blackbox for %filtered +{ + my %filtered; - return ( exists $filtered{$page}{$destpage} - && $filtered{$page}{$destpage} eq 1 ); -} #}}} -sub setalreadyfiltered($$) { #{{{ - my $page=shift; - my $destpage=shift; + sub alreadyfiltered($$) { #{{{ + my $page=shift; + my $destpage=shift; - $filtered{$page}{$destpage}=1; -} #}}} -sub unsetalreadyfiltered($$) { #{{{ - my $page=shift; - my $destpage=shift; + return ( exists $filtered{$page}{$destpage} + && $filtered{$page}{$destpage} eq 1 ); + } #}}} - if (exists $filtered{$page}{$destpage}) { - delete $filtered{$page}{$destpage}; - } -} #}}} -sub resetalreadyfiltered() { #{{{ - undef %filtered; -} #}}} + 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. -- cgit v1.2.3 From 864bc2d9d0507a3b3f67ab785e347e37a8eaf9fa Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 18:38:25 +0100 Subject: po(otherlanguagesloop): start factorizing Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 0666142cb..5383027b8 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -494,8 +494,8 @@ sub otherlanguagesloop ($) { #{{{ my @ret; if (istranslatable($page)) { - foreach my $lang (sort keys %{$translations{$page}}) { - my $translation = $translations{$page}{$lang}; + my %otherpages=%{otherlanguages($page)}; + while (my ($lang, $translation) = each %otherpages) { push @ret, { url => urlto($translation, $page), code => $lang, -- cgit v1.2.3 From c5513548c9d43923829c9bc2080a3e1bb9e2cac0 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 20:35:53 +0100 Subject: po: factorize extraction of a slave page's masterpage and lang The very same code was repeated at dozens of places. NB: the real work is now done is _istranslation(), which is memoized, so the additional function calls overhead should be compensated. Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 59 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 24 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 5383027b8..eabc96977 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -145,10 +145,10 @@ sub otherlanguages($) { #{{{ %ret = %{$translations{$page}}; } elsif (istranslation($page)) { - my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/); + my $masterpage = masterpage($page); $ret{$config{po_master_language}{code}} = $masterpage; foreach my $lang (sort keys %{$translations{$masterpage}}) { - next if $lang eq $curlang; + next if $lang eq lang($page); $ret{$lang} = $translations{$masterpage}{$lang}; } } @@ -251,7 +251,6 @@ sub scan (@) { #{{{ 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} @@ -260,7 +259,7 @@ sub scan (@) { #{{{ # there for other reasons) for (my $i=0; $i<@{$links{$page}}; $i++) { if (@{$links{$page}}[$i] eq $destpage) { - @{$links{$page}}[$i] = $destpage . '.' . $curlang; + @{$links{$page}}[$i] = $destpage . '.' . lang($page); last; } } @@ -283,7 +282,7 @@ sub mytargetpage ($$) { #{{{ my $ext=shift; if (istranslation($page)) { - my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); + my ($masterpage, $lang) = (masterpage($page), lang($page)); if (! $config{usedirs} || $masterpage eq 'index') { return $masterpage . "." . $lang . "." . $ext; } @@ -333,8 +332,7 @@ sub myurlto ($$;$) { #{{{ && $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 IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}"); } return $origsubs{'urlto'}->($to,$from,$absolute); } #}}} @@ -348,8 +346,7 @@ sub mybestlink ($$) { #{{{ if ($config{po_link_to} eq "current" && istranslatable($res) && istranslation($page)) { - my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/); - return $res . "." . $curlang; + return $res . "." . lang($page); } else { return $res; @@ -426,8 +423,7 @@ sub filter (@) { #{{{ writefile(basename($infile), File::Spec->tmpdir, $content); - my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); - my $masterfile = srcfile($pagesources{$masterpage}); + my $masterfile = srcfile($pagesources{masterpage($page)}); my (@pos,@masters); push @pos,$infile; push @masters,$masterfile; @@ -458,8 +454,7 @@ sub htmlize (@) { #{{{ my $page = $params{page}; my $content = $params{content}; - my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); - my $masterfile = srcfile($pagesources{$masterpage}); + my $masterfile = srcfile($pagesources{masterpage($page)}); # force content to be htmlize'd as if it was the same type as the master page return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content); @@ -468,10 +463,9 @@ sub htmlize (@) { #{{{ sub percenttranslated ($) { #{{{ my $page=shift; - return gettext("N/A") unless (istranslation($page)); - my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); + return gettext("N/A") unless istranslation($page); my $file=srcfile($pagesources{$page}); - my $masterfile = srcfile($pagesources{$masterpage}); + my $masterfile = srcfile($pagesources{masterpage($page)}); my (@pos,@masters); push @pos,$file; push @masters,$masterfile; @@ -505,7 +499,7 @@ sub otherlanguagesloop ($) { #{{{ } } elsif (istranslation($page)) { - my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/); + my ($masterpage, $curlang) = (masterpage($page), lang($page)); push @ret, { url => urlto_with_orig_beautiful_urlpath($masterpage, $page), code => $config{po_master_language}{code}, @@ -530,13 +524,13 @@ sub pagetemplate (@) { #{{{ my $destpage=$params{destpage}; my $template=$params{template}; - my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page); + my ($masterpage, $lang) = istranslation($page); if (istranslation($page) && $template->query(name => "percenttranslated")) { $template->param(percenttranslated => percenttranslated($page)); } if ($template->query(name => "istranslation")) { - $template->param(istranslation => istranslation($page)); + $template->param(istranslation => scalar istranslation($page)); } if ($template->query(name => "istranslatable")) { $template->param(istranslatable => istranslatable($page)); @@ -664,18 +658,35 @@ sub _istranslation ($) { #{{{ return 0; } - return istranslatable($masterpage); + return ($masterpage, $lang) if istranslatable($masterpage); } #}}} sub istranslation ($) { #{{{ my $page=shift; - if (_istranslation($page)) { - my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); + if (1 < (my ($masterpage, $lang) = _istranslation($page))) { $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang}; - return 1; + return ($masterpage, $lang); + } + return; +} #}}} + +sub masterpage ($) { #{{{ + my $page=shift; + + if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) { + return $masterpage; + } + return $page; +} #}}} + +sub lang ($) { #{{{ + my $page=shift; + + if (1 < (my ($masterpage, $lang) = _istranslation($page))) { + return $lang; } - return 0; + return $config{po_master_language}{code}; } #}}} package IkiWiki::PageSpec; -- cgit v1.2.3 From b4ab0d0b9eae7b2204b8ea8e1ee70e63117b23ab Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 21:19:21 +0100 Subject: po: rewrote otherlanguagesloop() It is now more elegant IMHO, and the output is now sorted according to the language name (instead of code). Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index eabc96977..1754260ed 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -98,8 +98,8 @@ sub getsetup () { #{{{ } #}}} sub islanguagecode ($) { #{{{ - my $code=shift; - return ($code =~ /^[a-z]{2}$/); + my $code=shift; + return ($code =~ /^[a-z]{2}$/); } #}}} sub checkconfig () { #{{{ @@ -483,39 +483,44 @@ sub percenttranslated ($) { #{{{ return $percent; } #}}} +sub languagename ($) { #{{{ + my $code=shift; + + return $config{po_master_language}{name} + if $code eq $config{po_master_language}{code}; + return $config{po_slave_languages}{$code} + if defined $config{po_slave_languages}{$code}; + return; +} #}}} + sub otherlanguagesloop ($) { #{{{ my $page=shift; my @ret; - if (istranslatable($page)) { - my %otherpages=%{otherlanguages($page)}; - while (my ($lang, $translation) = each %otherpages) { + my %otherpages=%{otherlanguages($page)}; + while (my ($lang, $otherpage) = each %otherpages) { + if (istranslation($page) && masterpage($page) eq $otherpage) { push @ret, { - url => urlto($translation, $page), + url => urlto_with_orig_beautiful_urlpath($otherpage, $page), code => $lang, - language => $config{po_slave_languages}{$lang}, - percent => percenttranslated($translation), + language => languagename($lang), + master => 1, }; } - } - elsif (istranslation($page)) { - my ($masterpage, $curlang) = (masterpage($page), lang($page)); - push @ret, { - url => urlto_with_orig_beautiful_urlpath($masterpage, $page), - code => $config{po_master_language}{code}, - language => $config{po_master_language}{name}, - master => 1, - }; - foreach my $lang (sort keys %{$translations{$masterpage}}) { + else { push @ret, { - url => urlto($translations{$masterpage}{$lang}, $page), + url => urlto($otherpage, $page), code => $lang, - language => $config{po_slave_languages}{$lang}, - percent => percenttranslated($translations{$masterpage}{$lang}), - } unless ($lang eq $curlang); + language => languagename($lang), + percent => percenttranslated($otherpage), + } } } - return @ret; + return sort { + return -1 if $a->{code} eq $config{po_master_language}{code}; + return 1 if $b->{code} eq $config{po_master_language}{code}; + return $a->{language} cmp $b->{language}; + } @ret; } #}}} sub pagetemplate (@) { #{{{ -- cgit v1.2.3 From a7d329c17312348d7511b48523fb54c18f8d4736 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Mon, 10 Nov 2008 21:30:06 +0100 Subject: po(match_lang, match_currentlang): use the lang() function ... now that it exists, instead of duplicating it. Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 1754260ed..2ffccb7e0 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -726,16 +726,7 @@ sub match_lang ($$;@) { #{{{ my $wanted=shift; my $regexp=IkiWiki::glob2re($wanted); - my $lang; - my $masterpage; - - if (IkiWiki::Plugin::po::istranslation($page)) { - ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); - } - else { - $lang = $config{po_master_language}{code}; - } - + my $lang=IkiWiki::Plugin::po::lang($page); if ($lang!~/^$regexp$/i) { return IkiWiki::FailReason->new("file language is $lang, not $wanted"); } @@ -746,26 +737,13 @@ sub match_lang ($$;@) { #{{{ sub match_currentlang ($$;@) { #{{{ my $page=shift; - shift; my %params=@_; - my ($currentmasterpage, $currentlang, $masterpage, $lang); return IkiWiki::FailReason->new("no location provided") unless exists $params{location}; - if (IkiWiki::Plugin::po::istranslation($params{location})) { - ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/); - } - else { - $currentlang = $config{po_master_language}{code}; - } - - if (IkiWiki::Plugin::po::istranslation($page)) { - ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); - } - else { - $lang = $config{po_master_language}{code}; - } + my $currentlang=IkiWiki::Plugin::po::lang($params{location}); + my $lang=IkiWiki::Plugin::po::lang($page); if ($lang eq $currentlang) { return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang"); -- cgit v1.2.3 From d57ef863803355f37b6c0c6d6caec203dfaeeed3 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 11 Nov 2008 04:05:39 +0100 Subject: po: reorder nearly all of the module code It has grown up incrementally and new helper functions were added right in the middle of the hooks, most often near the place they were used, which is practical when doing initial development, but quite ugly afterwards, when helper functions are useful to separate logic and implementation details. Today's refactoring commits have brought the code to a much more maintainable state, IMHO. Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 597 +++++++++++++++++++++++++++------------------------ 1 file changed, 313 insertions(+), 284 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 2ffccb7e0..389d4e940 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -21,16 +21,10 @@ use UNIVERSAL; my %translations; my @origneedsbuild; +my %origsubs; memoize("_istranslation"); memoize("percenttranslated"); -# FIXME: memoizing istranslatable() makes some test cases fail once every -# two tries; this may be related to the artificial way the testsuite is -# run, or not. -# memoize("istranslatable"); - -# backup references to subs that will be overriden -my %origsubs; sub import { #{{{ hook(type => "getsetup", id => "po", call => \&getsetup); @@ -53,11 +47,27 @@ sub import { #{{{ inject(name => "IkiWiki::urlto", call => \&myurlto); } #}}} + +# ,---- +# | Table of contents +# `---- + +# 1. Hooks +# 2. Injected functions +# 3. Blackboxes for private data +# 4. Helper functions +# 5. PageSpec's + + +# ,---- +# | Hooks +# `---- + sub getsetup () { #{{{ return plugin => { safe => 0, - rebuild => 1, # format plugin & changes html filenames + rebuild => 1, }, po_master_language => { type => "string", @@ -97,11 +107,6 @@ sub getsetup () { #{{{ }, } #}}} -sub islanguagecode ($) { #{{{ - my $code=shift; - return ($code =~ /^[a-z]{2}$/); -} #}}} - sub checkconfig () { #{{{ foreach my $field (qw{po_master_language po_slave_languages}) { if (! exists $config{$field} || ! defined $config{$field}) { @@ -137,97 +142,6 @@ sub checkconfig () { #{{{ push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/; } #}}} -sub otherlanguages($) { #{{{ - my $page=shift; - - my %ret; - if (istranslatable($page)) { - %ret = %{$translations{$page}}; - } - elsif (istranslation($page)) { - my $masterpage = masterpage($page); - $ret{$config{po_master_language}{code}} = $masterpage; - foreach my $lang (sort keys %{$translations{$masterpage}}) { - next if $lang eq lang($page); - $ret{$lang} = $translations{$masterpage}{$lang}; - } - } - return \%ret; -} #}}} - -sub potfile ($) { #{{{ - my $masterfile=shift; - - (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/); - $dir='' if $dir eq './'; - return File::Spec->catpath('', $dir, $name . ".pot"); -} #}}} - -sub pofile ($$) { #{{{ - my $masterfile=shift; - my $lang=shift; - - (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/); - $dir='' if $dir eq './'; - 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->{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. - my %po_options = ('porefs' => 'none'); - $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options); - $doc->{TT}{po_out}->set_charset('utf-8'); - # do the actual work - $doc->parse; - IkiWiki::prep_writefile(basename($potfile),dirname($potfile)); - $doc->writepo($potfile); -} #}}} - -sub refreshpofiles ($@) { #{{{ - my $masterfile=shift; - my @pofiles=@_; - - my $potfile=potfile($masterfile); - error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile); - - foreach my $pofile (@pofiles) { - IkiWiki::prep_writefile(basename($pofile),dirname($pofile)); - if (-e $pofile) { - system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0 - or error("[po/refreshpofiles:$pofile] failed to update"); - } - else { - File::Copy::syscopy($potfile,$pofile) - or error("[po/refreshpofiles:$pofile] failed to copy the POT file"); - } - } -} #}}} - -sub buildtranslationscache() { #{{{ - # use istranslation's side-effect - map istranslation($_), (keys %pagesources); -} #}}} - -sub resettranslationscache() { #{{{ - undef %translations; -} #}}} - sub needsbuild () { #{{{ my $needsbuild=shift; @@ -277,117 +191,6 @@ sub scan (@) { #{{{ } } #}}} -sub mytargetpage ($$) { #{{{ - my $page=shift; - my $ext=shift; - - if (istranslation($page)) { - my ($masterpage, $lang) = (masterpage($page), lang($page)); - if (! $config{usedirs} || $masterpage eq 'index') { - return $masterpage . "." . $lang . "." . $ext; - } - else { - return $masterpage . "/index." . $lang . "." . $ext; - } - } - elsif (istranslatable($page)) { - if (! $config{usedirs} || $page eq 'index') { - return $page . "." . $config{po_master_language}{code} . "." . $ext; - } - else { - return $page . "/index." . $config{po_master_language}{code} . "." . $ext; - } - } - return $origsubs{'targetpage'}->($page, $ext); -} #}}} - -sub mybeautify_urlpath ($) { #{{{ - my $url=shift; - - my $res=$origsubs{'beautify_urlpath'}->($url); - if ($config{po_link_to} eq "negotiated") { - $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!; - } - return $res; -} #}}} - -sub urlto_with_orig_beautiful_urlpath($$) { #{{{ - my $to=shift; - my $from=shift; - - inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'}); - my $res=urlto($to, $from); - inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_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')) { - return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}"); - } - return $origsubs{'urlto'}->($to,$from,$absolute); -} #}}} - -sub mybestlink ($$) { #{{{ - my $page=shift; - my $link=shift; - - my $res=$origsubs{'bestlink'}->($page, $link); - if (length $res) { - if ($config{po_link_to} eq "current" - && istranslatable($res) - && istranslation($page)) { - return $res . "." . lang($page); - } - else { - return $res; - } - } - return ""; -} #}}} - -# blackbox for %filtered -{ - my %filtered; - - 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 (@) { #{{{ @@ -460,76 +263,13 @@ sub htmlize (@) { #{{{ return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content); } #}}} -sub percenttranslated ($) { #{{{ - my $page=shift; - - return gettext("N/A") unless istranslation($page); - my $file=srcfile($pagesources{$page}); - my $masterfile = srcfile($pagesources{masterpage($page)}); - my (@pos,@masters); - push @pos,$file; - push @masters,$masterfile; - my %options = ( - "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0, - ); - my $doc=Locale::Po4a::Chooser::new('text',%options); - $doc->process( - 'po_in_name' => \@pos, - 'file_in_name' => \@masters, - 'file_in_charset' => 'utf-8', - 'file_out_charset' => 'utf-8', - ) or error("[po/percenttranslated:$page]: failed to translate"); - my ($percent,$hit,$queries) = $doc->stats(); - return $percent; -} #}}} - -sub languagename ($) { #{{{ - my $code=shift; - - return $config{po_master_language}{name} - if $code eq $config{po_master_language}{code}; - return $config{po_slave_languages}{$code} - if defined $config{po_slave_languages}{$code}; - return; -} #}}} +sub pagetemplate (@) { #{{{ + my %params=@_; + my $page=$params{page}; + my $destpage=$params{destpage}; + my $template=$params{template}; -sub otherlanguagesloop ($) { #{{{ - my $page=shift; - - my @ret; - my %otherpages=%{otherlanguages($page)}; - while (my ($lang, $otherpage) = each %otherpages) { - if (istranslation($page) && masterpage($page) eq $otherpage) { - push @ret, { - url => urlto_with_orig_beautiful_urlpath($otherpage, $page), - code => $lang, - language => languagename($lang), - master => 1, - }; - } - else { - push @ret, { - url => urlto($otherpage, $page), - code => $lang, - language => languagename($lang), - percent => percenttranslated($otherpage), - } - } - } - return sort { - return -1 if $a->{code} eq $config{po_master_language}{code}; - return 1 if $b->{code} eq $config{po_master_language}{code}; - return $a->{language} cmp $b->{language}; - } @ret; -} #}}} - -sub pagetemplate (@) { #{{{ - my %params=@_; - my $page=$params{page}; - my $destpage=$params{destpage}; - my $template=$params{template}; - - my ($masterpage, $lang) = istranslation($page); + my ($masterpage, $lang) = istranslation($page); if (istranslation($page) && $template->query(name => "percenttranslated")) { $template->param(percenttranslated => percenttranslated($page)); @@ -627,6 +367,120 @@ sub editcontent () { #{{{ return $params{content}; } #}}} + +# ,---- +# | Injected functions +# `---- + +sub mybestlink ($$) { #{{{ + my $page=shift; + my $link=shift; + + my $res=$origsubs{'bestlink'}->($page, $link); + if (length $res) { + if ($config{po_link_to} eq "current" + && istranslatable($res) + && istranslation($page)) { + return $res . "." . lang($page); + } + else { + return $res; + } + } + return ""; +} #}}} + +sub mybeautify_urlpath ($) { #{{{ + my $url=shift; + + my $res=$origsubs{'beautify_urlpath'}->($url); + if ($config{po_link_to} eq "negotiated") { + $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!; + } + return $res; +} #}}} + +sub mytargetpage ($$) { #{{{ + my $page=shift; + my $ext=shift; + + if (istranslation($page)) { + my ($masterpage, $lang) = (masterpage($page), lang($page)); + if (! $config{usedirs} || $masterpage eq 'index') { + return $masterpage . "." . $lang . "." . $ext; + } + else { + return $masterpage . "/index." . $lang . "." . $ext; + } + } + elsif (istranslatable($page)) { + if (! $config{usedirs} || $page eq 'index') { + return $page . "." . $config{po_master_language}{code} . "." . $ext; + } + else { + return $page . "/index." . $config{po_master_language}{code} . "." . $ext; + } + } + return $origsubs{'targetpage'}->($page, $ext); +} #}}} + +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')) { + return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}"); + } + return $origsubs{'urlto'}->($to,$from,$absolute); +} #}}} + + +# ,---- +# | Blackboxes for private data +# `---- + +{ + my %filtered; + + 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; + } #}}} +} + + +# ,---- +# | Helper functions +# `---- + sub istranslatable ($) { #{{{ my $page=shift; @@ -694,6 +548,181 @@ sub lang ($) { #{{{ return $config{po_master_language}{code}; } #}}} +sub islanguagecode ($) { #{{{ + my $code=shift; + return ($code =~ /^[a-z]{2}$/); +} #}}} + +sub otherlanguages($) { #{{{ + my $page=shift; + + my %ret; + if (istranslatable($page)) { + %ret = %{$translations{$page}}; + } + elsif (istranslation($page)) { + my $masterpage = masterpage($page); + $ret{$config{po_master_language}{code}} = $masterpage; + foreach my $lang (sort keys %{$translations{$masterpage}}) { + next if $lang eq lang($page); + $ret{$lang} = $translations{$masterpage}{$lang}; + } + } + return \%ret; +} #}}} + +sub potfile ($) { #{{{ + my $masterfile=shift; + + (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/); + $dir='' if $dir eq './'; + return File::Spec->catpath('', $dir, $name . ".pot"); +} #}}} + +sub pofile ($$) { #{{{ + my $masterfile=shift; + my $lang=shift; + + (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/); + $dir='' if $dir eq './'; + 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->{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. + my %po_options = ('porefs' => 'none'); + $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options); + $doc->{TT}{po_out}->set_charset('utf-8'); + # do the actual work + $doc->parse; + IkiWiki::prep_writefile(basename($potfile),dirname($potfile)); + $doc->writepo($potfile); +} #}}} + +sub refreshpofiles ($@) { #{{{ + my $masterfile=shift; + my @pofiles=@_; + + my $potfile=potfile($masterfile); + error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile); + + foreach my $pofile (@pofiles) { + IkiWiki::prep_writefile(basename($pofile),dirname($pofile)); + if (-e $pofile) { + system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0 + or error("[po/refreshpofiles:$pofile] failed to update"); + } + else { + File::Copy::syscopy($potfile,$pofile) + or error("[po/refreshpofiles:$pofile] failed to copy the POT file"); + } + } +} #}}} + +sub buildtranslationscache() { #{{{ + # use istranslation's side-effect + map istranslation($_), (keys %pagesources); +} #}}} + +sub resettranslationscache() { #{{{ + undef %translations; +} #}}} + +sub urlto_with_orig_beautiful_urlpath($$) { #{{{ + my $to=shift; + my $from=shift; + + inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'}); + my $res=urlto($to, $from); + inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath); + + return $res; +} #}}} + +sub percenttranslated ($) { #{{{ + my $page=shift; + + return gettext("N/A") unless istranslation($page); + my $file=srcfile($pagesources{$page}); + my $masterfile = srcfile($pagesources{masterpage($page)}); + my (@pos,@masters); + push @pos,$file; + push @masters,$masterfile; + my %options = ( + "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0, + ); + my $doc=Locale::Po4a::Chooser::new('text',%options); + $doc->process( + 'po_in_name' => \@pos, + 'file_in_name' => \@masters, + 'file_in_charset' => 'utf-8', + 'file_out_charset' => 'utf-8', + ) or error("[po/percenttranslated:$page]: failed to translate"); + my ($percent,$hit,$queries) = $doc->stats(); + return $percent; +} #}}} + +sub languagename ($) { #{{{ + my $code=shift; + + return $config{po_master_language}{name} + if $code eq $config{po_master_language}{code}; + return $config{po_slave_languages}{$code} + if defined $config{po_slave_languages}{$code}; + return; +} #}}} + +sub otherlanguagesloop ($) { #{{{ + my $page=shift; + + my @ret; + my %otherpages=%{otherlanguages($page)}; + while (my ($lang, $otherpage) = each %otherpages) { + if (istranslation($page) && masterpage($page) eq $otherpage) { + push @ret, { + url => urlto_with_orig_beautiful_urlpath($otherpage, $page), + code => $lang, + language => languagename($lang), + master => 1, + }; + } + else { + push @ret, { + url => urlto($otherpage, $page), + code => $lang, + language => languagename($lang), + percent => percenttranslated($otherpage), + } + } + } + return sort { + return -1 if $a->{code} eq $config{po_master_language}{code}; + return 1 if $b->{code} eq $config{po_master_language}{code}; + return $a->{language} cmp $b->{language}; + } @ret; +} #}}} + + +# ,---- +# | PageSpec's +# `---- + package IkiWiki::PageSpec; use warnings; use strict; -- cgit v1.2.3 From 0d8cbfc5e0c1c1408d2d48707fefbd60c12b78a6 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 11 Nov 2008 04:32:20 +0100 Subject: po(scan): make more clear what's being done Mostly comments, and also a small code change: simplification and implementation details hiding. Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 389d4e940..7deddf9ab 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -157,6 +157,10 @@ 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}; @@ -183,9 +187,10 @@ sub scan (@) { #{{{ elsif (! istranslatable($page) && ! istranslation($page)) { foreach my $destpage (@{$links{$page}}) { if (istranslatable($destpage)) { - map { - push @{$links{$page}}, $destpage . '.' . $_; - } (keys %{$config{po_slave_languages}}); + # make sure any destpage's translations has + # $page in its backlinks + push @{$links{$page}}, + values %{otherlanguages($destpage)}; } } } -- cgit v1.2.3 From 6b82ef44b07d19cd17dad3c26fe0a05715ffcb03 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 11 Nov 2008 14:11:34 +0100 Subject: po: some code simplification/clarification Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 65 +++++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 41 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 7deddf9ab..164d35270 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -364,10 +364,11 @@ sub change(@) { #{{{ } } #}}} +# As we're previewing or saving a page, the content may have +# changed, so tell the next filter() invocation it must not be lazy. 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 + unsetalreadyfiltered($params{page}, $params{page}); return $params{content}; } #}}} @@ -377,22 +378,19 @@ sub editcontent () { #{{{ # | Injected functions # `---- +# Implement po_link_to=current sub mybestlink ($$) { #{{{ my $page=shift; my $link=shift; my $res=$origsubs{'bestlink'}->($page, $link); - if (length $res) { - if ($config{po_link_to} eq "current" - && istranslatable($res) - && istranslation($page)) { - return $res . "." . lang($page); - } - else { - return $res; - } + if (length $res + && $config{po_link_to} eq "current" + && istranslatable($res) + && istranslation($page)) { + return $res . "." . lang($page); } - return ""; + return $res; } #}}} sub mybeautify_urlpath ($) { #{{{ @@ -409,7 +407,7 @@ sub mytargetpage ($$) { #{{{ my $page=shift; my $ext=shift; - if (istranslation($page)) { + if (istranslation($page) || istranslatable($page)) { my ($masterpage, $lang) = (masterpage($page), lang($page)); if (! $config{usedirs} || $masterpage eq 'index') { return $masterpage . "." . $lang . "." . $ext; @@ -418,14 +416,6 @@ sub mytargetpage ($$) { #{{{ return $masterpage . "/index." . $lang . "." . $ext; } } - elsif (istranslatable($page)) { - if (! $config{usedirs} || $page eq 'index') { - return $page . "." . $config{po_master_language}{code} . "." . $ext; - } - else { - return $page . "/index." . $config{po_master_language}{code} . "." . $ext; - } - } return $origsubs{'targetpage'}->($page, $ext); } #}}} @@ -491,11 +481,9 @@ sub istranslatable ($) { #{{{ my $file=$pagesources{$page}; - if (! defined $file - || (defined pagetype($file) && pagetype($file) eq 'po') - || $file =~ /\.pot$/) { - return 0; - } + return 0 unless defined $file; + return 0 if (defined pagetype($file) && pagetype($file) eq 'po'); + return 0 if $file =~ /\.pot$/; return pagespec_match($page, $config{po_translatable_pages}); } #}}} @@ -503,24 +491,17 @@ sub _istranslation ($) { #{{{ my $page=shift; my $file=$pagesources{$page}; - if (! defined $file) { - return IkiWiki::FailReason->new("no file specified"); - } - if (! defined $file - || ! defined pagetype($file) - || ! pagetype($file) eq 'po' - || $file =~ /\.pot$/) { - return 0; - } + return 0 unless (defined $file + && defined pagetype($file) + && pagetype($file) eq 'po'); + return 0 if $file =~ /\.pot$/; my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/); - if (! defined $masterpage || ! defined $lang - || ! (length($masterpage) > 0) || ! (length($lang) > 0) - || ! defined $pagesources{$masterpage} - || ! defined $config{po_slave_languages}{$lang}) { - return 0; - } + return 0 unless (defined $masterpage && defined $lang + && length $masterpage && length $lang + && defined $pagesources{$masterpage} + && defined $config{po_slave_languages}{$lang}); return ($masterpage, $lang) if istranslatable($masterpage); } #}}} @@ -555,6 +536,7 @@ sub lang ($) { #{{{ sub islanguagecode ($) { #{{{ my $code=shift; + return ($code =~ /^[a-z]{2}$/); } #}}} @@ -595,6 +577,7 @@ sub pofile ($$) { #{{{ sub pofiles ($) { #{{{ my $masterfile=shift; + return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}}); } #}}} -- cgit v1.2.3 From cfe101fff9f4469c4765e81ec5bb0a0b2ca2bec0 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 11 Nov 2008 15:01:16 +0100 Subject: po: fixed link to homepage from non-translation page Added testcases for this bug. It remains broken when generated from , though. Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 1 - t/po.t | 13 ++++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 164d35270..42a125808 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -427,7 +427,6 @@ sub myurlto ($$;$) { #{{{ # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto() if (! length $to && $config{po_link_to} eq "current" - && istranslation($from) && istranslatable('index')) { return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}"); } diff --git a/t/po.t b/t/po.t index 9c6c6da6c..be446f108 100755 --- a/t/po.t +++ b/t/po.t @@ -17,7 +17,7 @@ BEGIN { } } -use Test::More tests => 52; +use Test::More tests => 58; BEGIN { use_ok("IkiWiki"); } @@ -144,6 +144,17 @@ is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr") is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)"); is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)"); +### urlto -> index +$config{po_link_to}='current'; +$msgprefix="urlto (po_link_to=current)"; +is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''"); +is(urlto('', 'nontranslatable'), './../index.en.html', "$msgprefix nontranslatable -> ''"); +is(urlto('', 'translatable.fr'), './../index.fr.html', "$msgprefix translatable.fr -> ''"); +$msgprefix="urlto (po_link_to=negotiated)"; +is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''"); +is(urlto('', 'nontranslatable'), './../index.en.html', "$msgprefix nontranslatable -> ''"); +is(urlto('', 'translatable.fr'), './../index.fr.html', "$msgprefix translatable.fr -> ''"); + ### bestlink $config{po_link_to}='current'; $msgprefix="bestlink (po_link_to=current)"; -- cgit v1.2.3 From 43a1d36378c085ed9c71e066686907104c91da7e Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 11 Nov 2008 15:27:39 +0100 Subject: po: added HOMEPAGEURL template variable, documented when to use it Hopefully all links should now be consistent with the chosen linking behavior, but who knows... Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 8 ++++++++ doc/plugins/po.mdwn | 4 ++++ 2 files changed, 12 insertions(+) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 42a125808..739564c6b 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -285,6 +285,9 @@ sub pagetemplate (@) { #{{{ if ($template->query(name => "istranslatable")) { $template->param(istranslatable => istranslatable($page)); } + if ($template->query(name => "HOMEPAGEURL")) { + $template->param(homepageurl => homepageurl($page)); + } if ($template->query(name => "otherlanguages")) { $template->param(otherlanguages => [otherlanguagesloop($page)]); map add_depends($page, $_), (values %{otherlanguages($page)}); @@ -705,6 +708,11 @@ sub otherlanguagesloop ($) { #{{{ } @ret; } #}}} +sub homepageurl (;$) { #{{{ + my $page=shift; + + return urlto('', $page); +} #}}} # ,---- # | PageSpec's diff --git a/doc/plugins/po.mdwn b/doc/plugins/po.mdwn index 2f359bb83..d27b5af1d 100644 --- a/doc/plugins/po.mdwn +++ b/doc/plugins/po.mdwn @@ -129,6 +129,10 @@ Usage Templates --------- +When `po_link_to` is not set to `negotiated`, one should replace some +occurrences of `BASEURL` with `HOMEPAGEURL` to get correct links to +the wiki homepage. + The `ISTRANSLATION` and `ISTRANSLATABLE` variables can be used to display things only on translatable or translation pages. -- cgit v1.2.3 From f0e796d9a12d5f78fb89f312f138c1a0bb6f0141 Mon Sep 17 00:00:00 2001 From: intrigeri Date: Tue, 11 Nov 2008 16:04:40 +0100 Subject: po(change): fix uninitialized variables when running IkiWiki::refresh() Signed-off-by: intrigeri --- IkiWiki/Plugin/po.pm | 4 ++++ doc/plugins/po.mdwn | 12 ------------ 2 files changed, 4 insertions(+), 12 deletions(-) (limited to 'IkiWiki/Plugin') diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm index 739564c6b..a8d9b9cd9 100644 --- a/IkiWiki/Plugin/po.pm +++ b/IkiWiki/Plugin/po.pm @@ -362,6 +362,10 @@ sub change(@) { #{{{ resettranslationscache(); # Trigger a wiki refresh. require IkiWiki::Render; + # without preliminary saveindex/loadindex, refresh() + # complains about a lot of uninitialized variables + IkiWiki::saveindex(); + IkiWiki::loadindex(); IkiWiki::refresh(); IkiWiki::saveindex(); } diff --git a/doc/plugins/po.mdwn b/doc/plugins/po.mdwn index d27b5af1d..544c7ef62 100644 --- a/doc/plugins/po.mdwn +++ b/doc/plugins/po.mdwn @@ -463,18 +463,6 @@ gettext/po4a rough corners into the Pot file, and let it propagate; should be fixed in `773de05a7a1ee68d2bed173367cf5e716884945a`, time will tell. -When committing a translatable page to the repository, msgmerge -happens and then: - - Use of uninitialized value $page in pattern match (m//) at /usr/local/share/perl/5.10.0/IkiWiki.pm line 624. - Use of uninitialized value $p in hash element at /usr/local/share/perl/5.10.0/IkiWiki/Render.pm line 423. - Use of uninitialized value $file in string eq at /usr/local/share/perl/5.10.0/IkiWiki/Render.pm line 444. - Use of uninitialized value $page in pattern match (m//) at /usr/local/share/perl/5.10.0/IkiWiki.pm line 624. - -And then tons of: - Use of uninitialized value $page in pattern match (m//) at /usr/local/share/perl/5.10.0/IkiWiki.pm line 1860. - Use of uninitialized value $page in concatenation (.) or string at /usr/local/share/perl/5.10.0/IkiWiki.pm line 1869. - Page titles in links -------------------- -- cgit v1.2.3