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