]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/po.pm
0698b248838451e8788afd2a21a83ddbe0846fd5
[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::Temp;
13
14 sub import {
15         hook(type => "getsetup", id => "po", call => \&getsetup);
16         hook(type => "checkconfig", id => "po", call => \&checkconfig);
17         hook(type => "scan", id => "po", call => \&scan);
18         hook(type => "targetpage", id => "po", call => \&targetpage);
19         hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
20         hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
21         hook(type => "filter", id => "po", call => \&filter);
22         hook(type => "htmlize", id => "po", call => \&htmlize);
23 }
24
25 sub getsetup () { #{{{
26         return
27                 plugin => {
28                         safe => 0,
29                         rebuild => 1, # format plugin
30                 },
31                 po_master_language => {
32                         type => "string",
33                         example => {
34                                 'code' => 'en',
35                                 'name' => 'English'
36                         },
37                         description => "master language (non-PO files)",
38                         safe => 1,
39                         rebuild => 1,
40                 },
41                 po_slave_languages => {
42                         type => "string",
43                         example => {'fr' => { 'name' => 'Français' },
44                                     'es' => { 'name' => 'Castellano' },
45                                     'de' => { 'name' => 'Deutsch' },
46                         },
47                         description => "slave languages (PO files)",
48                         safe => 1,
49                         rebuild => 1,
50                 },
51                 po_translatable_pages => {
52                         type => "pagespec",
53                         example => "!*/Discussion",
54                         description => "PageSpec controlling which pages are translatable",
55                         link => "ikiwiki/PageSpec",
56                         safe => 1,
57                         rebuild => 1,
58                 },
59                 po_link_to => {
60                         type => "string",
61                         example => "current",
62                         description => "internal linking behavior (default/current/negotiated)",
63                         safe => 1,
64                         rebuild => 1,
65                 },
66 } #}}}
67
68 sub checkconfig () { #{{{
69         foreach my $field (qw{po_master_language po_slave_languages}) {
70                 if (! exists $config{$field} || ! defined $config{$field}) {
71                         error(sprintf(gettext("Must specify %s"), $field));
72                 }
73         }
74         if (! exists $config{po_link_to} ||
75             ! defined $config{po_link_to}) {
76             $config{po_link_to}="default";
77         }
78         if (! exists $config{po_translatable_pages} ||
79             ! defined $config{po_translatable_pages}) {
80             $config{po_translatable_pages}="";
81         }
82         if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
83                 error(gettext("po_link_to=negotiated requires usedirs to be set"));
84         }
85         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
86 } #}}}
87
88 sub scan (@) { #{{{
89         my %params=@_;
90         my $page=$params{page};
91
92         # FIXME: cache (or memoize) the list of translatable/translation pages,
93         # and/or istranslation/istranslated results
94 } #}}}
95
96 sub targetpage (@) { #{{{
97         my %params = @_;
98         my $page=$params{page};
99         my $ext=$params{ext};
100
101         if (istranslation($page)) {
102                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
103                 if (! $config{usedirs} || $page eq 'index') {
104                         return $masterpage . "." . $lang . "." . $ext;
105                 }
106                 else {
107                         return $masterpage . "/index." . $lang . "." . $ext;
108                 }
109         }
110         elsif (istranslatable($page)) {
111                 if (! $config{usedirs} || $page eq 'index') {
112                         return $page . "." . $config{po_master_language}{code} . "." . $ext;
113                 }
114                 else {
115                         return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
116                 }
117         }
118         return;
119 } #}}}
120
121 sub tweakurlpath ($) { #{{{
122         my %params = @_;
123         my $url=$params{url};
124         if ($config{po_link_to} eq "negotiated") {
125                 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
126         }
127         return $url;
128 } #}}}
129
130 sub tweakbestlink ($$) { #{{{
131         my %params = @_;
132         my $page=$params{page};
133         my $link=$params{link};
134         if ($config{po_link_to} eq "current"
135             && istranslatable($link)
136             && istranslation($page)) {
137                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
138                 return $link . "." . $curlang;
139         }
140         return $link;
141 } #}}}
142
143 our %filtered;
144 # We use filter to convert PO to the master page's type,
145 # since other plugins should not work on PO files
146 sub filter (@) { #{{{
147         my %params = @_;
148         my $page = $params{page};
149         my $destpage = $params{destpage};
150         my $content = decode_utf8(encode_utf8($params{content}));
151
152         # decide if this is a PO file that should be converted into a translated document,
153         # and perform various sanity checks
154         if (! istranslation($page) || $filtered{$page}{$destpage}) {
155                 return $content;
156         }
157
158         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
159         my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
160         my $masterfile = srcfile($pagesources{$masterpage});
161         my (@pos,@masters);
162         push @pos,$file;
163         push @masters,$masterfile;
164         my %options = (
165                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
166                         );
167         my $doc=Locale::Po4a::Chooser::new('text',%options);
168         $doc->process(
169                 'po_in_name'    => \@pos,
170                 'file_in_name'  => \@masters,
171                 'file_in_charset'  => 'utf-8',
172                 'file_out_charset' => 'utf-8',
173         ) or error("[po/filter:$file]: failed to translate");
174         my ($percent,$hit,$queries) = $doc->stats();
175         my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
176         my $tmpout = $tmpfh->filename;
177         $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
178         $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
179         $filtered{$page}{$destpage}=1;
180         return $content;
181 } #}}}
182
183 sub htmlize (@) { #{{{
184         my %params=@_;
185         my $page = $params{page};
186         my $content = $params{content};
187         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
188         my $masterfile = srcfile($pagesources{$masterpage});
189
190         # force content to be htmlize'd as if it was the same type as the master page
191         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
192 } #}}}
193
194 sub istranslatable ($) { #{{{
195         my $page=shift;
196         my $file=$pagesources{$page};
197
198         if (! defined $file
199             || (defined pagetype($file) && pagetype($file) eq 'po')
200             || $file =~ /\.pot$/) {
201                 return 0;
202         }
203         return pagespec_match($page, $config{po_translatable_pages});
204 } #}}}
205
206 sub istranslation ($) { #{{{
207         my $page=shift;
208         my $file=$pagesources{$page};
209         if (! defined $file) {
210                 return IkiWiki::FailReason->new("no file specified");
211         }
212
213         if (! defined $file
214             || ! defined pagetype($file)
215             || ! pagetype($file) eq 'po'
216             || $file =~ /\.pot$/) {
217                 return 0;
218         }
219
220         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
221         if (! defined $masterpage || ! defined $lang
222             || ! (length($masterpage) > 0) || ! (length($lang) > 0)
223             || ! defined $pagesources{$masterpage}
224             || ! defined $config{po_slave_languages}{$lang}) {
225                 return 0;
226         }
227
228         return istranslatable($masterpage);
229 } #}}}
230
231
232 package IkiWiki::PageSpec;
233 use warnings;
234 use strict;
235 use IkiWiki 2.00;
236
237 sub match_istranslation ($;@) { #{{{
238         my $page=shift;
239         if (IkiWiki::Plugin::po::istranslation($page)) {
240                 return IkiWiki::SuccessReason->new("is a translation page");
241         }
242         else {
243                 return IkiWiki::FailReason->new("is not a translation page");
244         }
245 } #}}}
246
247 sub match_istranslatable ($;@) { #{{{
248         my $page=shift;
249         if (IkiWiki::Plugin::po::istranslatable($page)) {
250                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
251         }
252         else {
253                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
254         }
255 } #}}}
256
257 1