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