]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/po.pm
Revert "added the targetpage hook"
[ikiwiki.git] / IkiWiki / Plugin / po.pm
1 #!/usr/bin/perl
2 # .po as a wiki page type
3 # inspired by the GPL'd po4a-translate,
4 # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
5 package IkiWiki::Plugin::po;
6
7 use warnings;
8 use strict;
9 use IkiWiki 2.00;
10 use Encode;
11 use Locale::Po4a::Chooser;
12 use Locale::Po4a::Po;
13 use File::Basename;
14 use File::Copy;
15 use File::Spec;
16 use File::Temp;
17 use Memoize;
18
19 my %translations;
20 our %filtered;
21
22 ## FIXME: makes some test cases cry once every two tries; this may be
23 ## related to the artificial way the testsuite is run, or not.
24 # memoize("istranslatable");
25 memoize("_istranslation");
26 memoize("percenttranslated");
27
28 # backup references to subs that will be overriden
29 my %origsubs;
30 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
31 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
32
33 sub import {
34         hook(type => "getsetup", id => "po", call => \&getsetup);
35         hook(type => "checkconfig", id => "po", call => \&checkconfig);
36         hook(type => "needsbuild", id => "po", call => \&needsbuild);
37         hook(type => "targetpage", id => "po", call => \&targetpage);
38         hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
39         hook(type => "filter", id => "po", call => \&filter);
40         hook(type => "htmlize", id => "po", call => \&htmlize);
41         hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
42         inject(name => "IkiWiki::bestlink", call => \&mybestlink);
43         inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
44 }
45
46 sub getsetup () { #{{{
47         return
48                 plugin => {
49                         safe => 0,
50                         rebuild => 1, # format plugin
51                 },
52                 po_master_language => {
53                         type => "string",
54                         example => {
55                                 'code' => 'en',
56                                 'name' => 'English'
57                         },
58                         description => "master language (non-PO files)",
59                         safe => 0,
60                         rebuild => 1,
61                 },
62                 po_slave_languages => {
63                         type => "string",
64                         example => {
65                                 'fr' => 'Français',
66                                 'es' => 'Castellano',
67                                 'de' => 'Deutsch'
68                         },
69                         description => "slave languages (PO files)",
70                         safe => 0,
71                         rebuild => 1,
72                 },
73                 po_translatable_pages => {
74                         type => "pagespec",
75                         example => "!*/Discussion",
76                         description => "PageSpec controlling which pages are translatable",
77                         link => "ikiwiki/PageSpec",
78                         safe => 0,
79                         rebuild => 1,
80                 },
81                 po_link_to => {
82                         type => "string",
83                         example => "current",
84                         description => "internal linking behavior (default/current/negotiated)",
85                         safe => 0,
86                         rebuild => 1,
87                 },
88 } #}}}
89
90 sub checkconfig () { #{{{
91         foreach my $field (qw{po_master_language po_slave_languages}) {
92                 if (! exists $config{$field} || ! defined $config{$field}) {
93                         error(sprintf(gettext("Must specify %s"), $field));
94                 }
95         }
96         if (! exists $config{po_link_to} ||
97             ! defined $config{po_link_to}) {
98             $config{po_link_to}="default";
99         }
100         if (! exists $config{po_translatable_pages} ||
101             ! defined $config{po_translatable_pages}) {
102             $config{po_translatable_pages}="";
103         }
104         if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
105                 error(gettext("po_link_to=negotiated requires usedirs to be set"));
106         }
107         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
108 } #}}}
109
110 sub potfile ($) { #{{{
111         my $masterfile=shift;
112         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
113         return File::Spec->catfile($dir, $name . ".pot");
114 } #}}}
115
116 sub pofile ($$) { #{{{
117         my $masterfile=shift;
118         my $lang=shift;
119         (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
120         return File::Spec->catfile($dir, $name . "." . $lang . ".po");
121 } #}}}
122
123 sub refreshpot ($) { #{{{
124         my $masterfile=shift;
125         my $potfile=potfile($masterfile);
126         my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
127         my $doc=Locale::Po4a::Chooser::new('text',%options);
128         $doc->read($masterfile);
129         $doc->{TT}{utf_mode} = 1;
130         $doc->{TT}{file_in_charset} = 'utf-8';
131         $doc->{TT}{file_out_charset} = 'utf-8';
132         # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
133         # this is undocument use of internal Locale::Po4a::TransTractor's data,
134         # compulsory since this module prevents us from using the porefs option.
135         my %po_options = ('porefs' => 'none');
136         $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
137         # do the actual work
138         $doc->parse;
139         $doc->writepo($potfile);
140 } #}}}
141
142 sub refreshpofiles ($@) { #{{{
143         my $masterfile=shift;
144         my @pofiles=@_;
145
146         my $potfile=potfile($masterfile);
147         error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
148
149         foreach my $pofile (@pofiles) {
150                 if (-e $pofile) {
151                         my $cmd = "msgmerge -U --backup=none $pofile $potfile";
152                         system ($cmd) == 0
153                                 or error("[po/refreshpofiles:$pofile] failed to update");
154                 }
155                 else {
156                         File::Copy::syscopy($potfile,$pofile)
157                                 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
158                 }
159         }
160 } #}}}
161
162 sub needsbuild () { #{{{
163         my $needsbuild=shift;
164
165         # build %translations, using istranslation's side-effect
166         foreach my $page (keys %pagesources) {
167                 istranslation($page);
168         }
169
170         # refresh/create POT and PO files as needed
171         my $updated_po_files=0;
172         foreach my $page (keys %pagesources) {
173                 my $pageneedsbuild = grep { $_ eq $pagesources{$page} } @$needsbuild;
174                 if (istranslatable($page)) {
175                         my $file=srcfile($pagesources{$page});
176                         if ($pageneedsbuild || ! -e potfile($file)) {
177                                 refreshpot($file);
178                         }
179                         my @pofiles;
180                         foreach my $lang (keys %{$config{po_slave_languages}}) {
181                                 my $pofile=pofile($file, $lang);
182                                 if ($pageneedsbuild || ! -e $pofile) {
183                                         push @pofiles, $pofile;
184                                 }
185                         }
186                         if (@pofiles) {
187                                 refreshpofiles($file, @pofiles) ;
188                                 map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
189                                 $updated_po_files = 1;
190                         }
191                 }
192         }
193
194         # check staged changes in and trigger a wiki refresh.
195         if ($updated_po_files) {
196                 if ($config{rcs}) {
197                         IkiWiki::disable_commit_hook();
198                         IkiWiki::rcs_commit_staged(gettext("updated PO files"),
199                                 "refreshpofiles", "127.0.0.1");
200                         IkiWiki::enable_commit_hook();
201                         IkiWiki::rcs_update();
202                 }
203                 IkiWiki::refresh();
204                 IkiWiki::saveindex();
205                 # refresh module's private variables
206                 %filtered=undef;
207                 %translations=undef;
208                 foreach my $page (keys %pagesources) {
209                         istranslation($page);
210                 }
211         }
212
213
214         # make existing translations depend on the corresponding master page
215         foreach my $master (keys %translations) {
216                 foreach my $slave (values %{$translations{$master}}) {
217                         add_depends($slave, $master);
218                 }
219         }
220 } #}}}
221
222 sub targetpage (@) { #{{{
223         my %params = @_;
224         my $page=$params{page};
225         my $ext=$params{ext};
226
227         if (istranslation($page)) {
228                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
229                 if (! $config{usedirs} || $page eq 'index') {
230                         return $masterpage . "." . $lang . "." . $ext;
231                 }
232                 else {
233                         return $masterpage . "/index." . $lang . "." . $ext;
234                 }
235         }
236         elsif (istranslatable($page)) {
237                 if (! $config{usedirs} || $page eq 'index') {
238                         return $page . "." . $config{po_master_language}{code} . "." . $ext;
239                 }
240                 else {
241                         return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
242                 }
243         }
244         return;
245 } #}}}
246
247 sub mybeautify_urlpath ($) { #{{{
248         my $url=shift;
249         my $res=$origsubs{'beautify_urlpath'}->($url);
250         if ($config{po_link_to} eq "negotiated") {
251                 $res =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
252         }
253         return $res;
254 } #}}}
255
256 sub mybestlink ($$) { #{{{
257         my $page=shift;
258         my $link=shift;
259         my $res=$origsubs{'bestlink'}->($page, $link);
260         if (length $res) {
261                 if ($config{po_link_to} eq "current"
262                     && istranslatable($res)
263                     && istranslation($page)) {
264                         my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
265                         return $res . "." . $curlang;
266                 }
267                 else {
268                         return $res;
269                 }
270         }
271         return "";
272 } #}}}
273
274 # We use filter to convert PO to the master page's type,
275 # since other plugins should not work on PO files
276 sub filter (@) { #{{{
277         my %params = @_;
278         my $page = $params{page};
279         my $destpage = $params{destpage};
280         my $content = decode_utf8(encode_utf8($params{content}));
281
282         # decide if this is a PO file that should be converted into a translated document,
283         # and perform various sanity checks
284         if (! istranslation($page) || $filtered{$page}{$destpage}) {
285                 return $content;
286         }
287
288         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
289         my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
290         my $masterfile = srcfile($pagesources{$masterpage});
291         my (@pos,@masters);
292         push @pos,$file;
293         push @masters,$masterfile;
294         my %options = (
295                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
296                         );
297         my $doc=Locale::Po4a::Chooser::new('text',%options);
298         $doc->process(
299                 'po_in_name'    => \@pos,
300                 'file_in_name'  => \@masters,
301                 'file_in_charset'  => 'utf-8',
302                 'file_out_charset' => 'utf-8',
303         ) or error("[po/filter:$file]: failed to translate");
304         my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
305         my $tmpout = $tmpfh->filename;
306         $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
307         $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
308         $filtered{$page}{$destpage}=1;
309         return $content;
310 } #}}}
311
312 sub htmlize (@) { #{{{
313         my %params=@_;
314         my $page = $params{page};
315         my $content = $params{content};
316         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
317         my $masterfile = srcfile($pagesources{$masterpage});
318
319         # force content to be htmlize'd as if it was the same type as the master page
320         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
321 } #}}}
322
323 sub percenttranslated ($) { #{{{
324         my $page=shift;
325         return "N/A" unless (istranslation($page));
326         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
327         my $file=srcfile($pagesources{$page});
328         my $masterfile = srcfile($pagesources{$masterpage});
329         my (@pos,@masters);
330         push @pos,$file;
331         push @masters,$masterfile;
332         my %options = (
333                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
334                         );
335         my $doc=Locale::Po4a::Chooser::new('text',%options);
336         $doc->process(
337                 'po_in_name'    => \@pos,
338                 'file_in_name'  => \@masters,
339                 'file_in_charset'  => 'utf-8',
340                 'file_out_charset' => 'utf-8',
341         ) or error("[po/percenttranslated:$file]: failed to translate");
342         my ($percent,$hit,$queries) = $doc->stats();
343         return $percent;
344 } #}}}
345
346 sub otherlanguages ($) { #{{{
347         my $page=shift;
348         my @ret;
349         if (istranslatable($page)) {
350                 foreach my $lang (sort keys %{$translations{$page}}) {
351                         my $translation = $translations{$page}{$lang};
352                         push @ret, {
353                                 url => urlto($translation, $page),
354                                 code => $lang,
355                                 language => $config{po_slave_languages}{$lang},
356                                 percent => percenttranslated($translation),
357                         };
358                 }
359         }
360         elsif (istranslation($page)) {
361                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
362                 push @ret, {
363                         url => urlto($masterpage, $page),
364                         code => $config{po_master_language}{code},
365                         language => $config{po_master_language}{name},
366                         master => 1,
367                 };
368                 foreach my $lang (sort keys %{$translations{$masterpage}}) {
369                         push @ret, {
370                                 url => urlto($translations{$masterpage}{$lang}, $page),
371                                 code => $lang,
372                                 language => $config{po_slave_languages}{$lang},
373                                 percent => percenttranslated($translations{$masterpage}{$lang}),
374                         } unless ($lang eq $curlang);
375                 }
376         }
377         return @ret;
378 } #}}}
379
380 sub pagetemplate (@) { #{{{
381         my %params=@_;
382         my $page=$params{page};
383         my $template=$params{template};
384
385         if (istranslation($page) && $template->query(name => "percenttranslated")) {
386                 $template->param(percenttranslated => percenttranslated($page));
387         }
388         if ($template->query(name => "istranslation")) {
389                 $template->param(istranslation => istranslation($page));
390         }
391         if ($template->query(name => "istranslatable")) {
392                 $template->param(istranslatable => istranslatable($page));
393         }
394         if ($template->query(name => "otherlanguages")) {
395                 $template->param(otherlanguages => [otherlanguages($page)]);
396                 if (istranslatable($page)) {
397                         foreach my $translation (values %{$translations{$page}}) {
398                                 add_depends($page, $translation);
399                         }
400                 }
401                 elsif (istranslation($page)) {
402                         my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
403                         add_depends($page, $masterpage);
404                         foreach my $translation (values %{$translations{$masterpage}}) {
405                                 add_depends($page, $translation);
406                         }
407                 }
408         }
409 } # }}}
410
411 sub istranslatable ($) { #{{{
412         my $page=shift;
413         my $file=$pagesources{$page};
414
415         if (! defined $file
416             || (defined pagetype($file) && pagetype($file) eq 'po')
417             || $file =~ /\.pot$/) {
418                 return 0;
419         }
420         return pagespec_match($page, $config{po_translatable_pages});
421 } #}}}
422
423 sub _istranslation ($) { #{{{
424         my $page=shift;
425         my $file=$pagesources{$page};
426         if (! defined $file) {
427                 return IkiWiki::FailReason->new("no file specified");
428         }
429
430         if (! defined $file
431             || ! defined pagetype($file)
432             || ! pagetype($file) eq 'po'
433             || $file =~ /\.pot$/) {
434                 return 0;
435         }
436
437         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
438         if (! defined $masterpage || ! defined $lang
439             || ! (length($masterpage) > 0) || ! (length($lang) > 0)
440             || ! defined $pagesources{$masterpage}
441             || ! defined $config{po_slave_languages}{$lang}) {
442                 return 0;
443         }
444
445         return istranslatable($masterpage);
446 } #}}}
447
448 sub istranslation ($) { #{{{
449         my $page=shift;
450         if (_istranslation($page)) {
451                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
452                 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
453                 return 1;
454         }
455         return 0;
456 } #}}}
457
458 package IkiWiki::PageSpec;
459 use warnings;
460 use strict;
461 use IkiWiki 2.00;
462
463 sub match_istranslation ($;@) { #{{{
464         my $page=shift;
465         if (IkiWiki::Plugin::po::istranslation($page)) {
466                 return IkiWiki::SuccessReason->new("is a translation page");
467         }
468         else {
469                 return IkiWiki::FailReason->new("is not a translation page");
470         }
471 } #}}}
472
473 sub match_istranslatable ($;@) { #{{{
474         my $page=shift;
475         if (IkiWiki::Plugin::po::istranslatable($page)) {
476                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
477         }
478         else {
479                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
480         }
481 } #}}}
482
483 sub match_lang ($$;@) { #{{{
484         my $page=shift;
485         my $wanted=shift;
486         my $regexp=IkiWiki::glob2re($wanted);
487         my $lang;
488         my $masterpage;
489
490         if (IkiWiki::Plugin::po::istranslation($page)) {
491                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
492         }
493         else {
494                 $lang = $config{po_master_language}{code};
495         }
496
497         if ($lang!~/^$regexp$/i) {
498                 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
499         }
500         else {
501                 return IkiWiki::SuccessReason->new("file language is $wanted");
502         }
503 } #}}}
504
505 sub match_currentlang ($$;@) { #{{{
506         my $page=shift;
507         shift;
508         my %params=@_;
509         my ($currentmasterpage, $currentlang, $masterpage, $lang);
510
511         return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
512
513         if (IkiWiki::Plugin::po::istranslation($params{location})) {
514                 ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
515         }
516         else {
517                 $currentlang = $config{po_master_language}{code};
518         }
519
520         if (IkiWiki::Plugin::po::istranslation($page)) {
521                 ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
522         }
523         else {
524                 $lang = $config{po_master_language}{code};
525         }
526
527         if ($lang eq $currentlang) {
528                 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
529         }
530         else {
531                 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
532         }
533 } #}}}
534
535 1