summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: b33d4dc1bb6b3b21037e5d78367d9b5e248588ec (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. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  51. if (! $config{usedirs} || $page eq 'index') {
  52. return $masterpage . "." . $lang . "." . $ext;
  53. }
  54. else {
  55. return $masterpage . "/index." . $lang . "." . $ext;
  56. }
  57. }
  58. else {
  59. if (! $config{usedirs} || $page eq 'index') {
  60. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  61. }
  62. else {
  63. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  64. }
  65. }
  66. } #}}}
  67. # We use filter to convert PO to the master page's type,
  68. # since other plugins should not work on PO files
  69. sub filter (@) { #{{{
  70. my %params = @_;
  71. my $page = $params{page};
  72. my $content = decode_utf8(encode_utf8($params{content}));
  73. # decide if this is a PO file that should be converted into a translated document,
  74. # and perform various sanity checks
  75. if (! IkiWiki::PageSpec::match_istranslation($page, $page)) {
  76. return $content;
  77. }
  78. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  79. my $file=srcfile(exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page});
  80. my $masterfile = srcfile($pagesources{$masterpage});
  81. my (@pos,@masters);
  82. push @pos,$file;
  83. push @masters,$masterfile;
  84. my %options = (
  85. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  86. );
  87. my $doc=Locale::Po4a::Chooser::new('text',%options);
  88. $doc->process(
  89. 'po_in_name' => \@pos,
  90. 'file_in_name' => \@masters,
  91. 'file_in_charset' => 'utf-8',
  92. 'file_out_charset' => 'utf-8',
  93. ) or error("[po/filter:$file]: failed to translate");
  94. my ($percent,$hit,$queries) = $doc->stats();
  95. my $tmpfh = File::Temp->new(TEMPLATE => "/tmp/ikiwiki-po-filter-out.XXXXXXXXXX");
  96. my $tmpout = $tmpfh->filename;
  97. $doc->write($tmpout) or error("[po/filter:$file] could not write $tmpout");
  98. $content = readfile($tmpout) or error("[po/filter:$file] could not read $tmpout");
  99. return $content;
  100. } #}}}
  101. sub htmlize (@) { #{{{
  102. my %params=@_;
  103. my $page = $params{page};
  104. my $content = $params{content};
  105. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  106. my $masterfile = srcfile($pagesources{$masterpage});
  107. # force content to be htmlize'd as if it was the same type as the master page
  108. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  109. } #}}}
  110. package IkiWiki::PageSpec;
  111. sub match_istranslation ($;@) { #{{{
  112. my $page=shift;
  113. my $wanted=shift;
  114. my %params=@_;
  115. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  116. if (! defined $file) {
  117. return IkiWiki::FailReason->new("no file specified");
  118. }
  119. if (! IkiWiki::pagetype($page) eq 'po') {
  120. return IkiWiki::FailReason->new("is not a PO file");
  121. }
  122. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  123. if (! defined $masterpage || ! defined $lang
  124. || ! (length($masterpage) > 0) || ! (length($lang) > 0)) {
  125. return IkiWiki::FailReason->new("is not named like a translation file");
  126. }
  127. if (! defined $IkiWiki::pagesources{$masterpage}) {
  128. return IkiWiki::FailReason->new("the master page does not exist");
  129. }
  130. if (! defined $IkiWiki::config{po_slave_languages}{$lang}) {
  131. return IkiWiki::FailReason->new("language $lang is not supported");
  132. }
  133. return IkiWiki::SuccessReason->new("page $page is a translation");
  134. } #}}}
  135. 1