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;
11 use Locale::Po4a::Chooser;
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);
25 sub getsetup () { #{{{
29 rebuild => 1, # format plugin
31 po_master_language => {
37 description => "master language (non-PO files)",
41 po_slave_languages => {
43 example => {'fr' => { 'name' => 'Français' },
44 'es' => { 'name' => 'Castellano' },
45 'de' => { 'name' => 'Deutsch' },
47 description => "slave languages (PO files)",
51 po_translatable_pages => {
53 example => "!*/Discussion",
54 description => "PageSpec controlling which pages are translatable",
55 link => "ikiwiki/PageSpec",
62 description => "internal linking behavior (default/current/negotiated)",
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));
74 if (! exists $config{po_link_to} ||
75 ! defined $config{po_link_to}) {
76 $config{po_link_to}="default";
78 if (! exists $config{po_translatable_pages} ||
79 ! defined $config{po_translatable_pages}) {
80 $config{po_translatable_pages}="";
82 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
83 error(gettext("po_link_to=negotiated requires usedirs to be set"));
85 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
90 my $page=$params{page};
92 # FIXME: cache (or memoize) the list of translatable/translation pages,
93 # and/or istranslation/istranslated results
96 sub targetpage (@) { #{{{
98 my $page=$params{page};
101 if (istranslation($page)) {
102 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
103 if (! $config{usedirs} || $page eq 'index') {
104 return $masterpage . "." . $lang . "." . $ext;
107 return $masterpage . "/index." . $lang . "." . $ext;
110 elsif (istranslatable($page)) {
111 if (! $config{usedirs} || $page eq 'index') {
112 return $page . "." . $config{po_master_language}{code} . "." . $ext;
115 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
121 sub tweakurlpath ($) { #{{{
123 my $url=$params{url};
124 if ($config{po_link_to} eq "negotiated") {
125 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
130 sub tweakbestlink ($$) { #{{{
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;
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 (@) { #{{{
148 my $page = $params{page};
149 my $destpage = $params{destpage};
150 my $content = decode_utf8(encode_utf8($params{content}));
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}) {
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});
163 push @masters,$masterfile;
165 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
167 my $doc=Locale::Po4a::Chooser::new('text',%options);
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;
183 sub htmlize (@) { #{{{
185 my $page = $params{page};
186 my $content = $params{content};
187 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
188 my $masterfile = srcfile($pagesources{$masterpage});
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);
194 sub istranslatable ($) { #{{{
196 my $file=$pagesources{$page};
199 || (defined pagetype($file) && pagetype($file) eq 'po')
200 || $file =~ /\.pot$/) {
203 return pagespec_match($page, $config{po_translatable_pages});
206 sub istranslation ($) { #{{{
208 my $file=$pagesources{$page};
209 if (! defined $file) {
210 return IkiWiki::FailReason->new("no file specified");
214 || ! defined pagetype($file)
215 || ! pagetype($file) eq 'po'
216 || $file =~ /\.pot$/) {
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}) {
228 return istranslatable($masterpage);
232 package IkiWiki::PageSpec;
237 sub match_istranslation ($;@) { #{{{
239 if (IkiWiki::Plugin::po::istranslation($page)) {
240 return IkiWiki::SuccessReason->new("is a translation page");
243 return IkiWiki::FailReason->new("is not a translation page");
247 sub match_istranslatable ($;@) { #{{{
249 if (IkiWiki::Plugin::po::istranslatable($page)) {
250 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
253 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");