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