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