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;
11 use Locale::Po4a::Chooser;
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);
21 sub getsetup () { #{{{
25 rebuild => 1, # format plugin
27 po_master_language => {
33 description => "master language (non-PO files)",
37 po_slave_languages => {
39 example => {'fr' => { 'name' => 'Français' },
40 'es' => { 'name' => 'Castellano' },
41 'de' => { 'name' => 'Deutsch' },
43 description => "slave languages (PO files)",
49 sub targetpage (@) { #{{{
51 my $page=$params{page};
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 . "." . $lang . "." . $ext;
60 return $masterpage . "/index." . $lang . "." . $ext;
64 if (! $config{usedirs} || $page eq 'index') {
65 return $page . "." . $config{po_master_language}{code} . "." . $ext;
68 return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
73 # We use filter to convert PO to the master page's type,
74 # since other plugins should not work on PO files
77 my $page = $params{page};
78 my $content = decode_utf8(encode_utf8($params{content}));
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)) {
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});
91 push @masters,$masterfile;
93 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
95 my $doc=Locale::Po4a::Chooser::new('text',%options);
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");
110 sub htmlize (@) { #{{{
112 my $page = $params{page};
113 my $content = $params{content};
114 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
115 my $masterfile = srcfile($pagesources{$masterpage});
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);
121 package IkiWiki::PageSpec;
123 sub match_istranslation ($;@) { #{{{
128 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
129 if (! defined $file) {
130 return IkiWiki::FailReason->new("no file specified");
133 if (! IkiWiki::pagetype($page) eq 'po') {
134 return IkiWiki::FailReason->new("is not a PO file");
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");
143 if (! defined $IkiWiki::pagesources{$masterpage}) {
144 return IkiWiki::FailReason->new("the master page does not exist");
147 if (! defined $IkiWiki::config{po_slave_languages}{$lang}) {
148 return IkiWiki::FailReason->new("language $lang is not supported");
151 return IkiWiki::SuccessReason->new("page $page is a translation");