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_supported_languages => {
29 example => { 'fr' => { 'name' => 'Français' },
30 'es' => { 'name' => 'Castellano' },
31 'de' => { 'name' => 'Deutsch' },
38 sub targetpage (@) { #{{{
40 my $page=$params{page};
43 if (! IkiWiki::PageSpec::match_istranslation($page, $page)) {
47 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
48 if (! $config{usedirs} || $page eq 'index') {
49 return $masterpage.".".$ext.".".$lang;
52 return $masterpage."/index.".$ext.".".$lang;
56 # We use filter to convert PO to the master page's type,
57 # since other plugins should not work on PO files
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)) {
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});
74 push @masters,$masterfile;
76 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
78 my $doc=Locale::Po4a::Chooser::new('text',%options);
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");
93 sub htmlize (@) { #{{{
95 my $content = $params{content};
96 # FIXME: run master page's type htmlize hook
100 package IkiWiki::PageSpec;
102 sub match_istranslation ($;@) { #{{{
107 my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
108 if (! defined $file) {
109 return IkiWiki::FailReason->new("no file specified");
112 if (! IkiWiki::pagetype($page) eq 'po') {
113 return IkiWiki::FailReason->new("is not a PO file");
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");
122 if (! defined $IkiWiki::pagesources{$masterpage}) {
123 return IkiWiki::FailReason->new("the master page does not exist");
126 if (! defined $IkiWiki::config{po_supported_languages}{$lang}) {
127 return IkiWiki::FailReason->new("language $lang is not supported");
130 return IkiWiki::SuccessReason->new("page $page is a translation");