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