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