summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 4b29909219ba5a7bb6020943b1d1ce09e08a4112 (plain)
  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. use warnings;
  7. use strict;
  8. use IkiWiki 2.00;
  9. use Encode;
  10. use Locale::Po4a::Chooser;
  11. use File::Temp;
  12. sub import {
  13. hook(type => "getsetup", id => "po", call => \&getsetup);
  14. hook(type => "checkconfig", id => "po", call => \&checkconfig);
  15. hook(type => "targetpage", id => "po", call => \&targetpage);
  16. hook(type => "tweakurlpath", id => "po", call => \&tweakurlpath);
  17. hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
  18. hook(type => "filter", id => "po", call => \&filter);
  19. hook(type => "preprocess", id => "translatable", call => \&preprocess_translatable);
  20. hook(type => "htmlize", id => "po", call => \&htmlize);
  21. }
  22. sub getsetup () { #{{{
  23. return
  24. plugin => {
  25. safe => 0,
  26. rebuild => 1, # format plugin
  27. },
  28. po_master_language => {
  29. type => "string",
  30. example => {
  31. 'code' => 'en',
  32. 'name' => 'English'
  33. },
  34. description => "master language (non-PO files)",
  35. safe => 1,
  36. rebuild => 1,
  37. },
  38. po_slave_languages => {
  39. type => "string",
  40. example => {'fr' => { 'name' => 'Français' },
  41. 'es' => { 'name' => 'Castellano' },
  42. 'de' => { 'name' => 'Deutsch' },
  43. },
  44. description => "slave languages (PO files)",
  45. safe => 1,
  46. rebuild => 1,
  47. },
  48. po_link_to => {
  49. type => "string",
  50. example => "current",
  51. description => "internal linking behavior (default/current/negotiated)",
  52. safe => 1,
  53. rebuild => 1,
  54. },
  55. } #}}}
  56. sub checkconfig () { #{{{
  57. foreach my $field (qw{po_master_language po_slave_languages}) {
  58. if (! exists $config{$field} || ! defined $config{$field}) {
  59. error(sprintf(gettext("Must specify %s"), $field));
  60. }
  61. }
  62. if (! exists $config{po_link_to} ||
  63. ! defined $config{po_link_to}) {
  64. $config{po_link_to}="default";
  65. }
  66. if ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
  67. error(gettext("po_link_to=negotiated requires usedirs to be set"));
  68. }
  69. } #}}}
  70. sub targetpage (@) { #{{{
  71. my %params = @_;
  72. my $page=$params{page};
  73. my $ext=$params{ext};
  74. if (pagespec_match($page,"istranslation()")) {
  75. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  76. if (! $config{usedirs} || $page eq 'index') {
  77. return $masterpage . "." . $lang . "." . $ext;
  78. }
  79. else {
  80. return $masterpage . "/index." . $lang . "." . $ext;
  81. }
  82. }
  83. elsif (pagespec_match($page,"istranslatable()")) {
  84. if (! $config{usedirs} || $page eq 'index') {
  85. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  86. }
  87. else {
  88. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  89. }
  90. }
  91. return;
  92. } #}}}
  93. sub tweakurlpath ($) { #{{{
  94. my %params = @_;
  95. my $url=$params{url};
  96. if ($config{po_link_to} eq "negotiated") {
  97. $url =~ s!/index.$config{po_master_language}{code}.$config{htmlext}$!/!;
  98. }
  99. return $url;
  100. } #}}}
  101. sub tweakbestlink ($$) { #{{{
  102. my %params = @_;
  103. my $page=$params{page};
  104. my $link=$params{link};
  105. if ($config{po_link_to} eq "current" && pagespec_match($link, "istranslatable()")) {
  106. if (pagespec_match($page, "istranslation()")) {
  107. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  108. return $link . "." . $curlang;
  109. }
  110. }
  111. return $link;
  112. } #}}}
  113. # We use filter to convert PO to the master page's type,
  114. # since other plugins should not work on PO files
  115. sub filter (@) { #{{{
  116. my %params = @_;
  117. my $page = $params{page};
  118. my $content = decode_utf8(encode_utf8($params{content}));
  119. # decide if this is a PO file that should be converted into a translated document,
  120. # and perform various sanity checks
  121. if (! pagespec_match($page, "istranslation()")) {
  122. return $content;
  123. }
  124. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  125. my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
  126. my $masterfile = srcfile($pagesources{$masterpage});
  127. my (@pos,@masters);
  128. push @pos,$file;
  129. push @masters,$masterfile;
  130. my %options = (
  131. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  132. );
  133. my $doc=Locale::Po4a::Chooser::new('text',%options);
  134. $doc->process(
  135. 'po_in_name' => \@pos,
  136. 'file_in_name' => \@masters,
  137. 'file_in_charset' => 'utf-8',
  138. 'file_out_charset' => 'utf-8',
  139. ) or error("[po/filter:$file]: failed to translate");
  140. my ($percent,$hit,$queries) = $doc->stats();
  141. my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
  142. my $tmpout = $tmpfh->filename;
  143. $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
  144. $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
  145. return $content;
  146. } #}}}
  147. sub preprocess_translatable (@) { #{{{
  148. my %params = @_;
  149. my $match = exists $params{match} ? $params{match} : $params{page};
  150. $pagestate{$params{page}}{po_translatable}{$match}=1;
  151. return "" if ($params{silent} && IkiWiki::yesno($params{silent}));
  152. return sprintf(gettext("pages %s set as translatable"), $params{match});
  153. } #}}}
  154. sub htmlize (@) { #{{{
  155. my %params=@_;
  156. my $page = $params{page};
  157. my $content = $params{content};
  158. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  159. my $masterfile = srcfile($pagesources{$masterpage});
  160. # force content to be htmlize'd as if it was the same type as the master page
  161. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  162. } #}}}
  163. package IkiWiki::PageSpec;
  164. use warnings;
  165. use strict;
  166. use IkiWiki 2.00;
  167. sub match_istranslation ($;@) { #{{{
  168. my $page=shift;
  169. my $wanted=shift;
  170. my %params=@_;
  171. my $file=exists $params{file} ? $params{file} : $pagesources{$page};
  172. if (! defined $file) {
  173. return IkiWiki::FailReason->new("no file specified");
  174. }
  175. if (! defined pagetype($file) || ! pagetype($file) eq 'po') {
  176. return IkiWiki::FailReason->new("is not a PO file");
  177. }
  178. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  179. if (! defined $masterpage || ! defined $lang
  180. || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
  181. return IkiWiki::FailReason->new("is not named like a translation file");
  182. }
  183. if (! defined $pagesources{$masterpage}) {
  184. return IkiWiki::FailReason->new("the master page does not exist");
  185. }
  186. if (! defined $config{po_slave_languages}{$lang}) {
  187. return IkiWiki::FailReason->new("language $lang is not supported");
  188. }
  189. return IkiWiki::SuccessReason->new("page $page is a translation");
  190. } #}}}
  191. sub match_istranslatable ($;@) { #{{{
  192. my $page=shift;
  193. my $wanted=shift;
  194. my %params=@_;
  195. my $file=exists $params{file} ? $params{file} : $pagesources{$page};
  196. if (! defined $file) {
  197. return IkiWiki::FailReason->new("no file specified");
  198. }
  199. if (defined pagetype($file) && pagetype($file) eq 'po') {
  200. return IkiWiki::FailReason->new("is a PO file");
  201. }
  202. if ($file =~ /\.pot$/) {
  203. return IkiWiki::FailReason->new("is a POT file");
  204. }
  205. foreach my $registering_page (keys %pagestate) {
  206. if (exists $pagestate{$registering_page}{po_translatable}) {
  207. foreach my $pagespec (sort keys %{$pagestate{$registering_page}{po_translatable}}) {
  208. if (pagespec_match($page, $pagespec, location => $registering_page)) {
  209. return IkiWiki::SuccessReason->new("is set as translatable on $registering_page");
  210. }
  211. }
  212. }
  213. }
  214. return IkiWiki::FailReason->new("is not set as translatable");
  215. } #}}}
  216. 1