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