summaryrefslogtreecommitdiff
path: root/perl/Locale/Po4a/Text.pm
blob: ae62667bc875c183a05b91f635caf051b931b021 (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<markdown>
  60. Handle some special markup in Markdown-formatted texts.
  61. =cut
  62. my $markdown 0;
  63. sub initialize {
  64.     my $self shift;
  65.     my %options @_;
  66.     $self->{options}{'nobullets'}='';
  67.     if (defined $options{'nobullets'}) {
  68.         $bullets 0;
  69.     }
  70.     if (defined $options{'debianchangelog'}) {
  71.         $debianchangelog=1;
  72.     }
  73.     if (defined $options{'markdown'}) {
  74.         $markdown=1;
  75.     }
  76. }
  77. sub parse {
  78.     my $self shift;
  79.     my ($line,$ref);
  80.     my $paragraph="";
  81.     my $wrapped_mode 1;
  82.     my $expect_header 1;
  83.     ($line,$ref)=$self->shiftline();
  84.     while (defined($line)) {
  85.         chomp($line);
  86.         $self->{ref}="$ref";
  87.         if ($debianchangelog and
  88.             $expect_header and
  89.             $line =~ /^(\w[-+0-9a-z.]*)\ \(([^\(\\t]+)\# src, version
  90.                        \s+([-+0-9a-z.]+);                 # distribution
  91.                        \s*urgency\s*\=\s*(.*\S)\s*$/ix) { #
  92.             do_paragraph($self,$paragraph,$wrapped_mode);
  93.             $paragraph="";
  94.             $self->pushline("$line\n");
  95.             $expect_header=0;
  96.         elsif ($debianchangelog and
  97.                  $line =~ m/^ \-\- (.*) <(.*)> ((\w+\,\s*)?\d{1,2}\s+\w+\s+\d{4}\s+\d{1,2}:\d\d:\d\d\s+[-+]\d{4}(\s+\([^\\\(\)]\))?)$/) {
  98.             # Found trailer
  99.             do_paragraph($self,$paragraph,$wrapped_mode);
  100.             $paragraph="";
  101.             $self->pushline("$line\n");
  102.             $expect_header=1;
  103.         elsif ($line =~ /^\s*$/) {
  104.             # Break paragraphs on lines containing only spaces
  105.             do_paragraph($self,$paragraph,$wrapped_mode);
  106.             $self->pushline("\n"unless (   $wrapped_mode == 0
  107.                                           or $paragraph eq "");
  108.             $paragraph="";
  109.             $wrapped_mode 1;
  110.             $self->pushline($line."\n");
  111.         elsif (   $line =~ /^=*$/
  112.                  or $line =~ /^_*$/
  113.                  or $line =~ /^-*$/) {
  114.             $wrapped_mode 0;
  115.             $paragraph .= $line."\n";
  116.             do_paragraph($self,$paragraph,$wrapped_mode);
  117.             $paragraph="";
  118.             $wrapped_mode 1;
  119.         elsif ($markdown and
  120.                  (   $line =~ m/^#/       # headline
  121.                   or $line =~ m/^>/       # blockquote
  122.                   or $line =~ m/[<>]/     # maybe html
  123.                   or $line =~ m/^"""/     # textblock inside macro end
  124.                   or $line =~ m/"""$/)) { # textblock inside macro begin
  125.             # Found headline
  126.             $wrapped_mode 0;
  127.             $paragraph .= $line."\n";
  128.             do_paragraph($self,$paragraph,$wrapped_mode);
  129.             $paragraph="";
  130.             $wrapped_mode 1;
  131.         else {
  132.             if ($line =~ /^\s/) {
  133.                 # A line starting by a space indicates a non-wrap
  134.                 # paragraph
  135.                 $wrapped_mode 0;
  136.             }
  137.             $paragraph .= $line."\n";
  138.         }
  139.         # paragraphs starting by a bullet, or numbered
  140.         # or paragraphs with a line containing many consecutive spaces
  141.         # (more than 3)
  142.         # are considered as verbatim paragraphs
  143.         $wrapped_mode if (   $paragraph =~ m/^(\*|[0-9]+[.)] )/s
  144.                               or $paragraph =~ m/[ \t][ \t][ \t]/s);
  145.         ($line,$ref)=$self->shiftline();
  146.     }
  147.     if (length $paragraph) {
  148.         do_paragraph($self,$paragraph,$wrapped_mode);
  149.     }
  150. }
  151. sub do_paragraph {
  152.     my ($self$paragraph$wrap) = (shiftshiftshift);
  153.     return if ($paragraph eq "");
  154.     if ($bullets) {
  155.         # Detect bullets
  156.         # |        * blah blah
  157.         # |<spaces>  blah
  158.         # |          ^-- aligned
  159.         # <empty line>
  160.         #
  161.         # Other bullets supported:
  162.         # - blah         o blah         + blah
  163.         # 1. blah       1) blah       (1) blah
  164. TEST_BULLET:
  165.         if ($paragraph =~ m/^(\s*)((?:[-*o+]|([0-9]+[.\)])|\([0-9]+\))\s+)([^\n]*\n)(.*)$/s) {
  166.             my $para $5;
  167.             my $bullet $2;
  168.             my $indent1 $1;
  169.             my $indent2 "$1".(' ' x length $bullet);
  170.             my $text $4;
  171.             while ($para !~ m/$indent2(?:[-*o+]|([0-9]+[.\)])|\([0-9]+\))\s+/
  172.                    and $para =~ s/^$indent2(\S[^\n]*\n)//s) {
  173.                 $text .= $1;
  174.             }
  175.             # TODO: detect if a line starts with the same bullet
  176.             if ($text !~ m/\S[ \t][ \t][ \t]+\S/s) {
  177.                 my $bullet_regex quotemeta($indent1.$bullet);
  178.                 $bullet_regex =~ s/[0-9]+/\\d\+/;
  179.                 if ($para eq '' or $para =~ m/^$bullet_regex\S/s) {
  180.                     my $trans $self->translate($text,
  181.                                                  $self->{ref},
  182.                                                  "Bullet: '$indent1$bullet'",
  183.                                                  "wrap" => 1,
  184.                                                  "wrapcol" => - (length $indent2));
  185.                     $trans =~ s/^/$indent1$bullet/s;
  186.                     $trans =~ s/\n(.)/\n$indent2$1/sg;
  187.                     $self->pushline$trans."\n" );
  188.                     if ($para eq '') {
  189.                         return;
  190.                     else {
  191.                         # Another bullet
  192.                         $paragraph $para;
  193.                         goto TEST_BULLET;
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.     }
  199.     # TODO: detect indented paragraphs
  200.     $self->pushline$self->translate($paragraph,
  201.                                       $self->{ref},
  202.                                       "Plain text",
  203.                                       "wrap" => $wrap) );
  204. }
  205. 1;
  206. =head1 STATUS OF THIS MODULE
  207. Tested successfully on simple text files and NEWS.Debian files.
  208. =head1 AUTHORS
  209.  Nicolas François <nicolas.francois@centraliens.net>
  210. =head1 COPYRIGHT AND LICENSE
  211.  Copyright 2005,2007 by Nicolas FRANÇOIS <nicolas.francois@centraliens.net>.
  212. This program is free software; you may redistribute it and/or modify it
  213. under the terms of GPL (see the COPYING file).