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"));
89 my $page=$params{page};
91 # FIXME: cache (or memoize) the list of translatable/translation pages,
92 # and/or istranslation/istranslated results
95 sub targetpage (@) { #{{{
97 my $page=$params{page};
100 if (istranslation($page)) {
101 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
102 if (! $config{usedirs} || $page eq 'index') {
103 return $masterpage . "." . $lang . "." . $ext;
106 return $masterpage . "/index." . $lang . "." . $ext;
109 elsif (istranslatable($page)) {
110 if (! $config{usedirs} || $page eq 'index') {
111 return $page . "." . $config{po_master_language}{code} . "." . $ext;
114 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
120 sub tweakurlpath ($) { #{{{
122 my $url=$params{url};
123 if ($config{po_link_to} eq "negotiated") {
124 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
129 sub tweakbestlink ($$) { #{{{
131 my $page=$params{page};
132 my $link=$params{link};
133 if ($config{po_link_to} eq "current"
134 && istranslatable($link)
135 && istranslation($page)) {
136 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
137 return $link . "." . $curlang;
142 # We use filter to convert PO to the master page's type,
143 # since other plugins should not work on PO files
144 sub filter (@) { #{{{
146 my $page = $params{page};
147 my $content = decode_utf8(encode_utf8($params{content}));
149 # decide if this is a PO file that should be converted into a translated document,
150 # and perform various sanity checks
151 if (! istranslation($page)) {
155 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
156 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
157 my $masterfile = srcfile($pagesources{$masterpage});
160 push @masters,$masterfile;
162 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
164 my $doc=Locale::Po4a::Chooser::new('text',%options);
166 'po_in_name' => \@pos,
167 'file_in_name' => \@masters,
168 'file_in_charset' => 'utf-8',
169 'file_out_charset' => 'utf-8',
170 ) or error("[po/filter:$file]: failed to translate");
171 my ($percent,$hit,$queries) = $doc->stats();
172 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
173 my $tmpout = $tmpfh->filename;
174 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
175 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
179 sub htmlize (@) { #{{{
181 my $page = $params{page};
182 my $content = $params{content};
183 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
184 my $masterfile = srcfile($pagesources{$masterpage});
186 # force content to be htmlize'd as if it was the same type as the master page
187 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
190 sub istranslatable ($) { #{{{
192 my $file=$pagesources{$page};
195 || (defined pagetype($file) && pagetype($file) eq 'po')
196 || $file =~ /\.pot$/) {
199 return pagespec_match($page, $config{po_translatable_pages});
202 sub istranslation ($) { #{{{
204 my $file=$pagesources{$page};
205 if (! defined $file) {
206 return IkiWiki::FailReason->new("no file specified");
210 || ! defined pagetype($file)
211 || ! pagetype($file) eq 'po'
212 || $file =~ /\.pot$/) {
216 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
217 if (! defined $masterpage || ! defined $lang
218 || ! (length($masterpage) > 0) || ! (length($lang) > 0)
219 || ! defined $pagesources{$masterpage}
220 || ! defined $config{po_slave_languages}{$lang}) {
224 return istranslatable($masterpage);
228 package IkiWiki::PageSpec;
233 sub match_istranslation ($;@) { #{{{
235 if (IkiWiki::Plugins::po::istranslation($page)) {
236 return IkiWiki::SuccessReason->new("is a translation page");
239 return IkiWiki::FailReason->new("is not a translation page");
243 sub match_istranslatable ($;@) { #{{{
245 if (IkiWiki::Plugins::po::istranslatable($page)) {
246 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
249 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");