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