]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/po.pm
ca59a8c9c9aad654c34bc9d49c8e9151493cbedb
[git.ikiwiki.info.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;
7 use warnings;
8 use strict;
9 use IkiWiki 2.00;
10 use Encode;
11 use Locale::Po4a::Chooser;
12 use File::Temp;
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 }
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 } #}}}
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 } #}}}
87 sub scan (@) { #{{{
88         my %params=@_;
89         my $page=$params{page};
91         # FIXME: cache (or memoize) the list of translatable/translation pages,
92         # and/or istranslation/istranslated results
93 } #}}}
95 sub targetpage (@) { #{{{
96         my %params = @_;
97         my $page=$params{page};
98         my $ext=$params{ext};
100         if (istranslation($page)) {
101                 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
102                 if (! $config{usedirs} || $page eq 'index') {
103                         return $masterpage . "." . $lang . "." . $ext;
104                 }
105                 else {
106                         return $masterpage . "/index." . $lang . "." . $ext;
107                 }
108         }
109         elsif (istranslatable($page)) {
110                 if (! $config{usedirs} || $page eq 'index') {
111                         return $page . "." . $config{po_master_language}{code} . "." . $ext;
112                 }
113                 else {
114                         return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
115                 }
116         }
117         return;
118 } #}}}
120 sub tweakurlpath ($) { #{{{
121         my %params = @_;
122         my $url=$params{url};
123         if ($config{po_link_to} eq "negotiated") {
124                 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
125         }
126         return $url;
127 } #}}}
129 sub tweakbestlink ($$) { #{{{
130         my %params = @_;
131         my $page=$params{page};
132         my $link=$params{link};
133         if ($config{po_link_to} eq "current"
134             && istranslatable($link)
135             && istranslation($page)) {
136                 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
137                 return $link . "." . $curlang;
138         }
139         return $link;
140 } #}}}
142 # We use filter to convert PO to the master page's type,
143 # since other plugins should not work on PO files
144 sub filter (@) { #{{{
145         my %params = @_;
146         my $page = $params{page};
147         my $content = decode_utf8(encode_utf8($params{content}));
149         # decide if this is a PO file that should be converted into a translated document,
150         # and perform various sanity checks
151         if (! istranslation($page)) {
152                 return $content;
153         }
155         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
156         my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
157         my $masterfile = srcfile($pagesources{$masterpage});
158         my (@pos,@masters);
159         push @pos,$file;
160         push @masters,$masterfile;
161         my %options = (
162                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
163                         );
164         my $doc=Locale::Po4a::Chooser::new('text',%options);
165         $doc->process(
166                 'po_in_name'    => \@pos,
167                 'file_in_name'  => \@masters,
168                 'file_in_charset'  => 'utf-8',
169                 'file_out_charset' => 'utf-8',
170         ) or error("[po/filter:$file]: failed to translate");
171         my ($percent,$hit,$queries) = $doc->stats();
172         my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
173         my $tmpout = $tmpfh->filename;
174         $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
175         $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
176         return $content;
177 } #}}}
179 sub htmlize (@) { #{{{
180         my %params=@_;
181         my $page = $params{page};
182         my $content = $params{content};
183         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
184         my $masterfile = srcfile($pagesources{$masterpage});
186         # force content to be htmlize'd as if it was the same type as the master page
187         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
188 } #}}}
190 sub istranslatable ($) { #{{{
191         my $page=shift;
192         my $file=$pagesources{$page};
194         if (! defined $file
195             || (defined pagetype($file) && pagetype($file) eq 'po')
196             || $file =~ /\.pot$/) {
197                 return 0;
198         }
199         return pagespec_match($page, $config{po_translatable_pages});
200 } #}}}
202 sub istranslation ($) { #{{{
203         my $page=shift;
204         my $file=$pagesources{$page};
205         if (! defined $file) {
206                 return IkiWiki::FailReason->new("no file specified");
207         }
209         if (! defined $file
210             || ! defined pagetype($file)
211             || ! pagetype($file) eq 'po'
212             || $file =~ /\.pot$/) {
213                 return 0;
214         }
216         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
217         if (! defined $masterpage || ! defined $lang
218             || ! (length($masterpage) > 0) || ! (length($lang) > 0)
219             || ! defined $pagesources{$masterpage}
220             || ! defined $config{po_slave_languages}{$lang}) {
221                 return 0;
222         }
224         return istranslatable($masterpage);
225 } #}}}
228 package IkiWiki::PageSpec;
229 use warnings;
230 use strict;
231 use IkiWiki 2.00;
233 sub match_istranslation ($;@) { #{{{
234         my $page=shift;
235         if (IkiWiki::Plugins::po::istranslation($page)) {
236                 return IkiWiki::SuccessReason->new("is a translation page");
237         }
238         else {
239                 return IkiWiki::FailReason->new("is not a translation page");
240         }
241 } #}}}
243 sub match_istranslatable ($;@) { #{{{
244         my $page=shift;
245         if (IkiWiki::Plugins::po::istranslatable($page)) {
246                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
247         }
248         else {
249                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
250         }
251 } #}}}