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