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