]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/po.pm
po plugin: htmlize translated content as if it was the same type as the master page
[git.ikiwiki.info.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;
7 use warnings;
8 use strict;
9 use IkiWiki 2.00;
10 use Encode;
11 use Locale::Po4a::Chooser;
12 use File::Temp;
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 }
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 } #}}}
38 sub targetpage (@) { #{{{
39         my %params = @_;
40         my $page=$params{page};
41         my $ext=$params{ext};
43         if (! IkiWiki::PageSpec::match_istranslation($page, $page)) {
44                 return;
45         }
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 } #}}}
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}));
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         }
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 } #}}}
93 sub htmlize (@) { #{{{
94         my %params=@_;
95         my $page = $params{page};
96         my $content = $params{content};
97         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
98         my $masterfile = srcfile($pagesources{$masterpage});
100         # force content to be htmlize'd as if it was the same type as the master page
101         return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
102 } #}}}
104 package IkiWiki::PageSpec;
106 sub match_istranslation ($;@) { #{{{
107         my $page=shift;
108         my $wanted=shift;
110         my %params=@_;
111         my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
112         if (! defined $file) {
113                 return IkiWiki::FailReason->new("no file specified");
114         }
116         if (! IkiWiki::pagetype($page) eq 'po') {
117                 return IkiWiki::FailReason->new("is not a PO file");
118         }
120         my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
121         if (! defined $masterpage || ! defined $lang
122             || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
123                 return IkiWiki::FailReason->new("is not named like a translation file");
124         }
126         if (! defined $IkiWiki::pagesources{$masterpage}) {
127                 return IkiWiki::FailReason->new("the master page does not exist");
128         }
130         if (! defined $IkiWiki::config{po_supported_languages}{$lang}) {
131                 return IkiWiki::FailReason->new("language $lang is not supported");
132         }
134         return IkiWiki::SuccessReason->new("page $page is a translation");
136 } #}}}