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