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)) {
58 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
59 if (! $config{usedirs} || $page eq 'index') {
60 return $masterpage.".".$ext.".".$lang;
63 return $masterpage."/index.".$ext.".".$lang;
67 # We use filter to convert PO to the master page's type,
68 # since other plugins should not work on PO files
71 my $page = $params{page};
72 my $content = decode_utf8(encode_utf8($params{content}));
74 # decide if this is a PO file that should be converted into a translated document,
75 # and perform various sanity checks
76 if (! IkiWiki::PageSpec::match_istranslation($page, $page)) {
80 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
81 my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
82 my $masterfile = srcfile($pagesources{$masterpage});
85 push @masters,$masterfile;
87 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
89 my $doc=Locale::Po4a::Chooser::new('text',%options);
91 'po_in_name' => \@pos,
92 'file_in_name' => \@masters,
93 'file_in_charset' => 'utf-8',
94 'file_out_charset' => 'utf-8',
95 ) or error("[po/filter:$file]: failed to translate");
96 my ($percent,$hit,$queries) = $doc->stats();
97 my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
98 my $tmpout = $tmpfh->filename;
99 $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
100 $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
104 sub htmlize (@) { #{{{
106 my $page = $params{page};
107 my $content = $params{content};
108 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
109 my $masterfile = srcfile($pagesources{$masterpage});
111 # force content to be htmlize'd as if it was the same type as the master page
112 return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
115 package IkiWiki::PageSpec;
117 sub match_istranslation ($;@) { #{{{
122 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
123 if (! defined $file) {
124 return IkiWiki::FailReason->new("no file specified");
127 if (! IkiWiki::pagetype($page) eq 'po') {
128 return IkiWiki::FailReason->new("is not a PO file");
131 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
132 if (! defined $masterpage || ! defined $lang
133 || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
134 return IkiWiki::FailReason->new("is not named like a translation file");
137 if (! defined $IkiWiki::pagesources{$masterpage}) {
138 return IkiWiki::FailReason->new("the master page does not exist");
141 if (! defined $IkiWiki::config{po_slave_languages}{$lang}) {
142 return IkiWiki::FailReason->new("language $lang is not supported");
145 return IkiWiki::SuccessReason->new("page $page is a translation");