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 => "targetpage", id => "po", call => \&targetpage);
18 hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
19 hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
20 hook(type => "filter", id => "po", call => \&filter);
21 hook(type => "preprocess", id => "translatable", call => \&preprocess_translatable);
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)",
54 description => "internal linking behavior (default/current/negotiated)",
60 sub checkconfig () { #{{{
61 foreach my $field (qw{po_master_language po_slave_languages}) {
62 if (! exists $config{$field} || ! defined $config{$field}) {
63 error(sprintf(gettext("Must specify %s"), $field));
66 if (! exists $config{po_link_to} ||
67 ! defined $config{po_link_to}) {
68 $config{po_link_to}="default";
70 if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
71 error(gettext("po_link_to=negotiated requires usedirs to be set"));
75 sub targetpage (@) { #{{{
77 my $page=$params{page};
80 if (pagespec_match($page,"istranslation()")) {
81 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
82 if (! $config{usedirs} || $page eq 'index') {
83 return $masterpage . "." . $lang . "." . $ext;
86 return $masterpage . "/index." . $lang . "." . $ext;
89 elsif (pagespec_match($page,"istranslatable()")) {
90 if (! $config{usedirs} || $page eq 'index') {
91 return $page . "." . $config{po_master_language}{code} . "." . $ext;
94 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
100 sub tweakurlpath ($) { #{{{
102 my $url=$params{url};
103 if ($config{po_link_to} eq "negotiated") {
104 $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
109 sub tweakbestlink ($$) { #{{{
111 my $page=$params{page};
112 my $link=$params{link};
113 if ($config{po_link_to} eq "current" && pagespec_match($link, "istranslatable()")) {
114 if (pagespec_match($page, "istranslation()")) {
115 my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
116 return $link . "." . $curlang;
122 # We use filter to convert PO to the master page's type,
123 # since other plugins should not work on PO files
124 sub filter (@) { #{{{
126 my $page = $params{page};
127 my $content = decode_utf8(encode_utf8($params{content}));
129 # decide if this is a PO file that should be converted into a translated document,
130 # and perform various sanity checks
131 if (! pagespec_match($page, "istranslation()")) {
135 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
136 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
137 my $masterfile = srcfile($pagesources{$masterpage});
140 push @masters,$masterfile;
142 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
144 my $doc=Locale::Po4a::Chooser::new('text',%options);
146 'po_in_name' => \@pos,
147 'file_in_name' => \@masters,
148 'file_in_charset' => 'utf-8',
149 'file_out_charset' => 'utf-8',
150 ) or error("[po/filter:$file]: failed to translate");
151 my ($percent,$hit,$queries) = $doc->stats();
152 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
153 my $tmpout = $tmpfh->filename;
154 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
155 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
159 sub preprocess_translatable (@) { #{{{
161 my $match = exists $params{match} ? $params{match} : $params{page};
163 $pagestate{$params{page}}{po}{translatable}{$match}=1;
165 return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
166 return sprintf(gettext("pages %s set as translatable"), $params{match});
170 sub htmlize (@) { #{{{
172 my $page = $params{page};
173 my $content = $params{content};
174 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
175 my $masterfile = srcfile($pagesources{$masterpage});
177 # force content to be htmlize'd as if it was the same type as the master page
178 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
181 package IkiWiki::PageSpec;
186 sub match_istranslation ($;@) { #{{{
191 my $file=exists $params{file} ? $params{file} : $pagesources{$page};
192 if (! defined $file) {
193 return IkiWiki::FailReason->new("no file specified");
196 if (! defined pagetype($file) || ! pagetype($file) eq 'po') {
197 return IkiWiki::FailReason->new("is not a PO file");
200 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
201 if (! defined $masterpage || ! defined $lang
202 || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
203 return IkiWiki::FailReason->new("is not named like a translation file");
206 if (! defined $pagesources{$masterpage}) {
207 return IkiWiki::FailReason->new("the master page does not exist");
210 if (! defined $config{po_slave_languages}{$lang}) {
211 return IkiWiki::FailReason->new("language $lang is not supported");
214 return IkiWiki::SuccessReason->new("page $page is a translation");
217 sub match_istranslatable ($;@) { #{{{
222 my $file=exists $params{file} ? $params{file} : $pagesources{$page};
223 if (! defined $file) {
224 return IkiWiki::FailReason->new("no file specified");
227 if (defined pagetype($file) && pagetype($file) eq 'po') {
228 return IkiWiki::FailReason->new("is a PO file");
230 if ($file =~ /\.pot$/) {
231 return IkiWiki::FailReason->new("is a POT file");
234 foreach my $registering_page (keys %pagestate) {
235 if (exists $pagestate{$registering_page}{po}{translatable}) {
236 foreach my $pagespec (sort keys %{$pagestate{$registering_page}{po}{translatable}}) {
237 if (pagespec_match($page, $pagespec, location => $registering_page)) {
238 return IkiWiki::SuccessReason->new("is set as translatable on $registering_page");
244 return IkiWiki::FailReason->new("is not set as translatable");