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