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 => "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);
26 sub getsetup () { #{{{
30 rebuild => 1, # format plugin
32 po_master_language => {
38 description => "master language (non-PO files)",
42 po_slave_languages => {
44 example => {'fr' => { 'name' => 'Français' },
45 'es' => { 'name' => 'Castellano' },
46 'de' => { 'name' => 'Deutsch' },
48 description => "slave languages (PO files)",
55 description => "internal linking behavior (default/current/negotiated)",
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));
67 if (! exists $config{po_link_to} ||
68 ! defined $config{po_link_to}) {
69 $config{po_link_to}="default";
71 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
72 error(gettext("po_link_to=negotiated requires usedirs to be set"));
76 sub needsbuild (@) { #{{{
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};
92 sub targetpage (@) { #{{{
94 my $page=$params{page};
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;
103 return $masterpage . "/index." . $lang . "." . $ext;
106 elsif (pagespec_match($page,"istranslatable()")) {
107 if (! $config{usedirs} || $page eq 'index') {
108 return $page . "." . $config{po_master_language}{code} . "." . $ext;
111 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
117 sub tweakurlpath ($) { #{{{
119 my $url=$params{url};
120 if ($config{po_link_to} eq "negotiated") {
121 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
126 sub tweakbestlink ($$) { #{{{
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;
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 (@) { #{{{
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()")) {
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});
157 push @masters,$masterfile;
159 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
161 my $doc=Locale::Po4a::Chooser::new('text',%options);
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");
176 sub preprocess_translatable (@) { #{{{
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});
187 sub htmlize (@) { #{{{
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);
198 package IkiWiki::PageSpec;
203 sub match_istranslation ($;@) { #{{{
208 my $file=exists $params{file} ? $params{file} : $pagesources{$page};
209 if (! defined $file) {
210 return IkiWiki::FailReason->new("no file specified");
213 if (! defined pagetype($file) || ! pagetype($file) eq 'po') {
214 return IkiWiki::FailReason->new("is not a PO file");
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");
223 if (! defined $pagesources{$masterpage}) {
224 return IkiWiki::FailReason->new("the master page does not exist");
227 if (! defined $config{po_slave_languages}{$lang}) {
228 return IkiWiki::FailReason->new("language $lang is not supported");
231 return IkiWiki::SuccessReason->new("page $page is a translation");
234 sub match_istranslatable ($;@) { #{{{
239 my $file=exists $params{file} ? $params{file} : $pagesources{$page};
240 if (! defined $file) {
241 return IkiWiki::FailReason->new("no file specified");
244 if (defined pagetype($file) && pagetype($file) eq 'po') {
245 return IkiWiki::FailReason->new("is a PO file");
247 if ($file =~ /\.pot$/) {
248 return IkiWiki::FailReason->new("is a POT file");
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");
261 return IkiWiki::FailReason->new("is not set as translatable");