summaryrefslogtreecommitdiff
path: root/perl/Locale/Po4a/Text.pm
blob: eb8ded9f7463f0513b95915127ebfe8d0e9ae30c (plain)
  1. #!/usr/bin/perl -w
  2. # Po4a::Text.pm
  3. # extract and translate translatable strings from a text documents
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. #
  20. ########################################################################
  21. =head1 NAME
  22. Locale::Po4a::Text - Convert text documents from/to PO files
  23. =head1 DESCRIPTION
  24. The po4a (po for anything) project goal is to ease translations (and more
  25. interestingly, the maintenance of translations) using gettext tools on
  26. areas where they were not expected like documentation.
  27. Locale::Po4a::Text is a module to help the translation of text documents into
  28. other [human] languages.
  29. Paragraphs are splitted on empty lines (or lines containing only spaces or
  30. tabulations).
  31. If a paragraph contains a line starting by a space (or tabulation), this
  32. paragraph won't be rewrapped.
  33. =cut
  34. package Locale::Po4a::Text;
  35. use 5.006;
  36. use strict;
  37. use warnings;
  38. require Exporter;
  39. use vars qw(@ISA @EXPORT);
  40. @ISA = qw(Locale::Po4a::TransTractor);
  41. @EXPORT qw();
  42. use Locale::Po4a::TransTractor;
  43. use Locale::Po4a::Common;
  44. =head1 OPTIONS ACCEPTED BY THIS MODULE
  45. These are this module's particular options:
  46. =over
  47. =item B<nobullet>
  48. Deactivate detection of bullets.
  49. By default, when a bullet is detected, the bullet paragraph is not considered
  50. as a verbatim paragraph (with the no-wrap flag in the PO file), but the module
  51. rewrap this paragraph in the generated PO file and in the translation.
  52. =cut
  53. my $bullets 1;
  54. =item B<debianchangelog>
  55. Handle the header and footer of
  56. released versions, which only contain non translatable informations.
  57. =cut
  58. my $debianchangelog 0;
  59. =item B<fortunes>
  60. Handle the fortunes format, which separate fortunes with a line which
  61. consists in '%' or '%%', and use '%%' as the beginning of a comment.
  62. =cut
  63. my $fortunes 0;
  64. =item B<markdown>
  65. Handle some special markup in Markdown-formatted texts.
  66. =cut
  67. my $markdown 0;
  68. sub initialize {
  69.     my $self shift;
  70.     my %options @_;
  71.     $self->{options}{'nobullets'}='';
  72.     if (defined $options{'nobullets'}) {
  73.         $bullets 0;
  74.     }
  75.     if (defined $options{'debianchangelog'}) {
  76.         $debianchangelog=1;
  77.     }
  78.     if (defined $options{'fortunes'}) {
  79.         $fortunes=1;
  80.     }
  81.     if (defined $options{'markdown'}) {
  82.         $markdown=1;
  83.     }
  84. }
  85. sub parse {
  86.     my $self shift;
  87.     my ($line,$ref);
  88.     my $paragraph="";
  89.     my $wrapped_mode 1;
  90.     my $expect_header 1;
  91.     ($line,$ref)=$self->shiftline();
  92.     while (defined($line)) {
  93.         chomp($line);
  94.         $self->{ref}="$ref";
  95.         if ($debianchangelog and
  96.             $expect_header and
  97.             $line =~ /^(\w[-+0-9a-z.]*)\ \(([^\(\\t]+)\# src, version
  98.                        \s+([-+0-9a-z.]+);                 # distribution
  99.                        \s*urgency\s*\=\s*(.*\S)\s*$/ix) { #
  100.             do_paragraph($self,$paragraph,$wrapped_mode);
  101.             $paragraph="";
  102.             $self->pushline("$line\n");
  103.             $expect_header=0;
  104.         elsif ($debianchangelog and
  105.                  $line =~ m/^ \-\- (.*) <(.*)> ((\w+\,\s*)?\d{1,2}\s+\w+\s+\d{4}\s+\d{1,2}:\d\d:\d\d\s+[-+]\d{4}(\s+\([^\\\(\)]\))?)$/) {
  106.             # Found trailer
  107.             do_paragraph($self,$paragraph,$wrapped_mode);
  108.             $paragraph="";
  109.             $self->pushline("$line\n");
  110.             $expect_header=1;
  111.         elsif ($fortunes and
  112.                  $line =~ m/^%%?\s*$/) {
  113.             # Found end of fortune
  114.             do_paragraph($self,$paragraph,$wrapped_mode);
  115.             $self->pushline("\n"unless (   $wrapped_mode == 0
  116.                                           or $paragraph eq "");
  117.             $paragraph="";
  118.             $wrapped_mode 1;
  119.             $self->pushline("$line\n");
  120.         elsif ($line =~ /^\s*$/) {
  121.             # Break paragraphs on lines containing only spaces
  122.             do_paragraph($self,$paragraph,$wrapped_mode);
  123.             $self->pushline("\n"unless (   $wrapped_mode == 0
  124.                                           or $paragraph eq "");
  125.             $paragraph="";
  126.             $wrapped_mode 1;
  127.             $self->pushline($line."\n");
  128.         elsif (   $line =~ /^=*$/
  129.                  or $line =~ /^_*$/
  130.                  or $line =~ /^-*$/) {
  131.             $wrapped_mode 0;
  132.             $paragraph .= $line."\n";
  133.             do_paragraph($self,$paragraph,$wrapped_mode);
  134.             $paragraph="";
  135.             $wrapped_mode 1;
  136.         elsif ($markdown and
  137.                  (   $line =~ /^#/            # headline
  138.                   or $line =~ /^\s*\[\[\!\S[^\]]*\]\]\s*$/)) { # sole macro
  139.             # Found Markdown markup that should be preserved as a single line
  140.             do_paragraph($self,$paragraph,$wrapped_mode);
  141.             do_paragraph($self,$line."\n",0);
  142.             $paragraph="";
  143.             $wrapped_mode 1;
  144.         elsif ($markdown and
  145.                  (   $paragraph =~ m/^>/       # blockquote
  146.                   or $paragraph =~ m/[<>]/     # maybe html
  147.                   or $paragraph =~ m/^"""/     # textblock inside macro end
  148.                   or $paragraph =~ m/"""$/)) { # textblock inside macro begin
  149.             # Found Markdown markup that might not survive wrapping
  150.             $wrapped_mode 0;
  151.             $paragraph .= $line."\n";
  152.         else {
  153.             if ($line =~ /^\s/) {
  154.                 # A line starting by a space indicates a non-wrap
  155.                 # paragraph
  156.                 $wrapped_mode 0;
  157.             }
  158.             if ($fortunes) {
  159.                 $line =~ s/%%(.*)$//;
  160.             }
  161. # TODO: comments
  162.             $paragraph .= $line."\n";
  163.         }
  164.         # paragraphs starting by a bullet, or numbered
  165.         # or paragraphs with a line containing many consecutive spaces
  166.         # (more than 3)
  167.         # are considered as verbatim paragraphs
  168.         $wrapped_mode if (   $paragraph =~ m/^(\*|[0-9]+[.)] )/s
  169.                               or $paragraph =~ m/[ \t][ \t][ \t]/s);
  170.         ($line,$ref)=$self->shiftline();
  171.     }
  172.     if (length $paragraph) {
  173.         do_paragraph($self,$paragraph,$wrapped_mode);
  174.     }
  175. }
  176. sub do_paragraph {
  177.     my ($self$paragraph$wrap) = (shiftshiftshift);
  178.     return if ($paragraph eq "");
  179.     if ($bullets) {
  180.         # Detect bullets
  181.         # |        * blah blah
  182.         # |<spaces>  blah
  183.         # |          ^-- aligned
  184.         # <empty line>
  185.         #
  186.         # Other bullets supported:
  187.         # - blah         o blah         + blah
  188.         # 1. blah       1) blah       (1) blah
  189. TEST_BULLET:
  190.         if ($paragraph =~ m/^(\s*)((?:[-*o+]|([0-9]+[.\)])|\([0-9]+\))\s+)([^\n]*\n)(.*)$/s) {
  191.             my $para $5;
  192.             my $bullet $2;
  193.             my $indent1 $1;
  194.             my $indent2 "$1".(' ' x length $bullet);
  195.             my $text $4;
  196.             while ($para !~ m/$indent2(?:[-*o+]|([0-9]+[.\)])|\([0-9]+\))\s+/
  197.                    and $para =~ s/^$indent2(\S[^\n]*\n)//s) {
  198.                 $text .= $1;
  199.             }
  200.             # TODO: detect if a line starts with the same bullet
  201.             if ($text !~ m/\S[ \t][ \t][ \t]+\S/s) {
  202.                 my $bullet_regex quotemeta($indent1.$bullet);
  203.                 $bullet_regex =~ s/[0-9]+/\\d\+/;
  204.                 if ($para eq '' or $para =~ m/^$bullet_regex\S/s) {
  205.                     my $trans $self->translate($text,
  206.                                                  $self->{ref},
  207.                                                  "Bullet: '$indent1$bullet'",
  208.                                                  "wrap" => 1,
  209.                                                  "wrapcol" => - (length $indent2));
  210.                     $trans =~ s/^/$indent1$bullet/s;
  211.                     $trans =~ s/\n(.)/\n$indent2$1/sg;
  212.                     $self->pushline$trans."\n" );
  213.                     if ($para eq '') {
  214.                         return;
  215.                     else {
  216.                         # Another bullet
  217.                         $paragraph $para;
  218.                         goto TEST_BULLET;
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.     }
  224.     # TODO: detect indented paragraphs
  225.     $self->pushline$self->translate($paragraph,
  226.                                       $self->{ref},
  227.                                       "Plain text",
  228.                                       "wrap" => $wrap) );
  229. }
  230. 1;
  231. =head1 STATUS OF THIS MODULE
  232. Tested successfully on simple text files and NEWS.Debian files.
  233. =head1 AUTHORS
  234.  Nicolas François <nicolas.francois@centraliens.net>
  235. =head1 COPYRIGHT AND LICENSE
  236.  Copyright 2005-2008 by Nicolas FRANÇOIS <nicolas.francois@centraliens.net>.
  237. This program is free software; you may redistribute it and/or modify it
  238. under the terms of GPL (see the COPYING file).