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