]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/po.pm
po plugin: added *.pot to wiki_file_prune_regexps
[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         push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
86 } #}}}
88 sub scan (@) { #{{{
89         my %params=@_;
90         my $page=$params{page};
92         # FIXME: cache (or memoize) the list of translatable/translation pages,
93         # and/or istranslation/istranslated results
94 } #}}}
96 sub targetpage (@) { #{{{
97         my %params = @_;
98         my $page=$params{page};
99         my $ext=$params{ext};
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 } #}}}
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 } #}}}
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 } #}}}
143 # We use filter to convert PO to the master page's type,
144 # since other plugins should not work on PO files
145 sub filter (@) { #{{{
146         my %params = @_;
147         my $page = $params{page};
148         my $content = decode_utf8(encode_utf8($params{content}));
150         # decide if this is a PO file that should be converted into a translated document,
151         # and perform various sanity checks
152         if (! istranslation($page)) {
153                 return $content;
154         }
156         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
157         my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
158         my $masterfile = srcfile($pagesources{$masterpage});
159         my (@pos,@masters);
160         push @pos,$file;
161         push @masters,$masterfile;
162         my %options = (
163                         "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
164                         );
165         my $doc=Locale::Po4a::Chooser::new('text',%options);
166         $doc->process(
167                 'po_in_name'    => \@pos,
168                 'file_in_name'  => \@masters,
169                 'file_in_charset'  => 'utf-8',
170                 'file_out_charset' => 'utf-8',
171         ) or error("[po/filter:$file]: failed to translate");
172         my ($percent,$hit,$queries) = $doc->stats();
173         my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
174         my $tmpout = $tmpfh->filename;
175         $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
176         $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
177         return $content;
178 } #}}}
180 sub htmlize (@) { #{{{
181         my %params=@_;
182         my $page = $params{page};
183         my $content = $params{content};
184         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
185         my $masterfile = srcfile($pagesources{$masterpage});
187         # force content to be htmlize'd as if it was the same type as the master page
188         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
189 } #}}}
191 sub istranslatable ($) { #{{{
192         my $page=shift;
193         my $file=$pagesources{$page};
195         if (! defined $file
196             || (defined pagetype($file) && pagetype($file) eq 'po')
197             || $file =~ /\.pot$/) {
198                 return 0;
199         }
200         return pagespec_match($page, $config{po_translatable_pages});
201 } #}}}
203 sub istranslation ($) { #{{{
204         my $page=shift;
205         my $file=$pagesources{$page};
206         if (! defined $file) {
207                 return IkiWiki::FailReason->new("no file specified");
208         }
210         if (! defined $file
211             || ! defined pagetype($file)
212             || ! pagetype($file) eq 'po'
213             || $file =~ /\.pot$/) {
214                 return 0;
215         }
217         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
218         if (! defined $masterpage || ! defined $lang
219             || ! (length($masterpage) > 0) || ! (length($lang) > 0)
220             || ! defined $pagesources{$masterpage}
221             || ! defined $config{po_slave_languages}{$lang}) {
222                 return 0;
223         }
225         return istranslatable($masterpage);
226 } #}}}
229 package IkiWiki::PageSpec;
230 use warnings;
231 use strict;
232 use IkiWiki 2.00;
234 sub match_istranslation ($;@) { #{{{
235         my $page=shift;
236         if (IkiWiki::Plugins::po::istranslation($page)) {
237                 return IkiWiki::SuccessReason->new("is a translation page");
238         }
239         else {
240                 return IkiWiki::FailReason->new("is not a translation page");
241         }
242 } #}}}
244 sub match_istranslatable ($;@) { #{{{
245         my $page=shift;
246         if (IkiWiki::Plugins::po::istranslatable($page)) {
247                 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
248         }
249         else {
250                 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
251         }
252 } #}}}