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