summaryrefslogtreecommitdiff
path: root/perl/Locale/Po4a/Text.pm
blob: f8dd497ec25c02def0a7894d39fcdfdf7faacf8e (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.     my $file $ref;
  93.     $file =~ s/:[0-9]+$//;
  94.     while (defined($line)) {
  95.         $ref =~ m/^(.*):[0-9]+$/;
  96.         if ($1 ne $file) {
  97.             $file $1;
  98.             do_paragraph($self,$paragraph,$wrapped_mode);
  99.             $paragraph="";
  100.             $wrapped_mode 1;
  101.             $expect_header 1;
  102.         }
  103.         # TODO: preserve original line ends throughout the code instead
  104.         chomp($line);
  105.         $self->{ref}="$ref";
  106.         if ($debianchangelog and
  107.             $expect_header and
  108.             $line =~ /^(\w[-+0-9a-z.]*)\ \(([^\(\\t]+)\# src, version
  109.                        \s+([-+0-9a-z.]+);                 # distribution
  110.                        \s*urgency\s*\=\s*(.*\S)\s*$/ix) { #
  111.             do_paragraph($self,$paragraph,$wrapped_mode);
  112.             $paragraph="";
  113.             $self->pushline("$line\n");
  114.             $expect_header=0;
  115.         elsif ($debianchangelog and
  116.                  $line =~ m/^ \-\- (.*) <(.*)> ((\w+\,\s*)?\d{1,2}\s+\w+\s+\d{4}\s+\d{1,2}:\d\d:\d\d\s+[-+]\d{4}(\s+\([^\\\(\)]\))?)$/) {
  117.             # Found trailer
  118.             do_paragraph($self,$paragraph,$wrapped_mode);
  119.             $paragraph="";
  120.             $self->pushline("$line\n");
  121.             $expect_header=1;
  122.         elsif ($fortunes and
  123.                  $line =~ m/^%%?\s*$/) {
  124.             # Found end of fortune
  125.             do_paragraph($self,$paragraph,$wrapped_mode);
  126.             # FIXME: test if this is still needed when always adding
  127.             #        newline in do_paragraph()
  128.             $self->pushline("\n"unless (   $wrapped_mode == 0
  129.                                           or $paragraph eq "");
  130.             $paragraph="";
  131.             $wrapped_mode 1;
  132.             $self->pushline("$line\n");
  133.         elsif ($line =~ /^\s*$/) {
  134.             # Break paragraphs on lines containing only spaces
  135.             do_paragraph($self,$paragraph,$wrapped_mode);
  136.             $paragraph="";
  137.             $wrapped_mode 1;
  138.             $self->pushline($line."\n");
  139.         elsif (   $line =~ /^=*$/
  140.                  or $line =~ /^_*$/
  141.                  or $line =~ /^-*$/) {
  142.             $wrapped_mode 0;
  143.             $paragraph .= $line."\n";
  144.             do_paragraph($self,$paragraph,$wrapped_mode);
  145.             $paragraph="";
  146.             $wrapped_mode 1;
  147.         elsif ($markdown and
  148.                  (   $line =~ /^#/            # headline
  149.                   or $line =~ /^\s*\[\[\!\S[^\]]*\]\]\s*$/)) { # sole macro
  150.             # Found Markdown markup that should be preserved as a single line
  151.             do_paragraph($self,$paragraph,$wrapped_mode);
  152.             $paragraph="$line\n";
  153.             $wrapped_mode 0;
  154.             do_paragraph($self,$paragraph,$wrapped_mode);
  155.             $wrapped_mode 1;
  156.             $paragraph="";
  157.         elsif ($markdown and
  158.                  (   $paragraph =~ m/^>/       # blockquote
  159.                   or $paragraph =~ m/[<>]/     # maybe html
  160.                   or $paragraph =~ m/^"""/     # textblock inside macro end
  161.                   or $paragraph =~ m/"""$/)) { # textblock inside macro begin
  162.             # Found Markdown markup that might not survive wrapping
  163.             $wrapped_mode 0;
  164.             $paragraph .= $line."\n";
  165.         else {
  166.             if ($line =~ /^\s/) {
  167.                 # A line starting by a space indicates a non-wrap
  168.                 # paragraph
  169.                 $wrapped_mode 0;
  170.             }
  171.             if ($fortunes) {
  172.                 $line =~ s/%%(.*)$//;
  173.             }
  174. # TODO: comments
  175.             $paragraph .= $line."\n";
  176.         }
  177.         # paragraphs starting by a bullet, or numbered
  178.         # or paragraphs with a line containing many consecutive spaces
  179.         # (more than 3)
  180.         # are considered as verbatim paragraphs
  181.         $wrapped_mode if (   $paragraph =~ m/^(\*|[0-9]+[.)] )/s
  182.                               or $paragraph =~ m/[ \t][ \t][ \t]/s);
  183.         ($line,$ref)=$self->shiftline();
  184.     }
  185.     if (length $paragraph) {
  186.         do_paragraph($self,$paragraph,$wrapped_mode);
  187.     }
  188. }
  189. sub do_paragraph {
  190.     my ($self$paragraph$wrap) = (shiftshiftshift);
  191.     return if ($paragraph eq "");
  192.     if ($bullets) {
  193.         # Detect bullets
  194.         # |        * blah blah
  195.         # |<spaces>  blah
  196.         # |          ^-- aligned
  197.         # <empty line>
  198.         #
  199.         # Other bullets supported:
  200.         # - blah         o blah         + blah
  201.         # 1. blah       1) blah       (1) blah
  202. TEST_BULLET:
  203.         if ($paragraph =~ m/^(\s*)((?:[-*o+]|([0-9]+[.\)])|\([0-9]+\))\s+)([^\n]*\n)(.*)$/s) {
  204.             my $para $5;
  205.             my $bullet $2;
  206.             my $indent1 $1;
  207.             my $indent2 "$1".(' ' x length $bullet);
  208.             my $text $4;
  209.             while ($para !~ m/$indent2(?:[-*o+]|([0-9]+[.\)])|\([0-9]+\))\s+/
  210.                    and $para =~ s/^$indent2(\S[^\n]*\n)//s) {
  211.                 $text .= $1;
  212.             }
  213.             # TODO: detect if a line starts with the same bullet
  214.             if ($text !~ m/\S[ \t][ \t][ \t]+\S/s) {
  215.                 my $bullet_regex quotemeta($indent1.$bullet);
  216.                 $bullet_regex =~ s/[0-9]+/\\d\+/;
  217.                 if ($para eq '' or $para =~ m/^$bullet_regex\S/s) {
  218.                     my $trans $self->translate($text,
  219.                                                  $self->{ref},
  220.                                                  "Bullet: '$indent1$bullet'",
  221.                                                  "wrap" => 1,
  222.                                                  "wrapcol" => - (length $indent2));
  223.                     $trans =~ s/^/$indent1$bullet/s;
  224.                     $trans =~ s/\n(.)/\n$indent2$1/sg;
  225.                     $self->pushline$trans."\n" );
  226.                     if ($para eq '') {
  227.                         return;
  228.                     else {
  229.                         # Another bullet
  230.                         $paragraph $para;
  231.                         goto TEST_BULLET;
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.     }
  237.     # TODO: detect indented paragraphs
  238.     my $transfinal $self->translate($paragraph,
  239.                                       $self->{ref},
  240.                                       "Plain text",
  241.                                       "wrap" => $wrap);
  242.     # TODO: preserve original line ends throughout the code instead
  243.     chomp $transfinal;
  244.     $transfinal .= "\n";
  245.     $self->pushline$transfinal );
  246. }
  247. 1;
  248. =head1 STATUS OF THIS MODULE
  249. Tested successfully on simple text files and NEWS.Debian files.
  250. =head1 AUTHORS
  251.  Nicolas François <nicolas.francois@centraliens.net>
  252. =head1 COPYRIGHT AND LICENSE
  253.  Copyright 2005-2008 by Nicolas FRANÇOIS <nicolas.francois@centraliens.net>.
  254. This program is free software; you may redistribute it and/or modify it
  255. under the terms of GPL (see the COPYING file).