Debian Codegraph @

man/po4a/Locale/Po4a/Manext.pm

raw
1 #! /usr/bin/perl -w
2
3 =encoding UTF-8
4
5 =head1 NAME
6
7 Locale::Po4a::Manext - private extended version of Locale::Po4a::Man
8
9 =head1 DESCRIPTION
10
11 Extended version of Locale::Po4a::Man that copes with a few intricacies of
12 man-db's manual pages, until such time as these are incorporated into po4a
13 proper.
14
15 =head1 SEE ALSO
16
17 L<Locale::Po4a::Man(3pm)>.
18
19 =head1 AUTHOR
20
21 Colin Watson <[email protected]>
22
23 =head1 COPYRIGHT AND LICENCE
24
25 Copyright 2011 Colin Watson.
26
27 man-db is free software; you can redistribute it and/or modify it
28 under the terms of the GNU General Public License as published by
29 the Free Software Foundation; either version 2 of the License, or
30 (at your option) any later version.
31
32 man-db is distributed in the hope that it will be useful, but
33 WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 GNU General Public License for more details.
36
37 You should have received a copy of the GNU General Public License
38 along with man-db; if not, write to the Free Software Foundation,
39 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40
41 =cut
42
43 package Locale::Po4a::Manext;
44
45 use 5.006;
46 use strict;
47 use warnings;
48
49 use vars qw(@ISA);
50 @ISA = qw(Locale::Po4a::Man);
51
52 use version;
53
54 use Locale::Po4a::Man;
55 use Locale::Po4a::TransTractor;
56
57 sub initialize {
58 my $self = shift;
59 $self->SUPER::initialize(@_);
60
61 $self->{manext_shift_tbl_state} = 0;
62 $self->{manext_shift_tbl_lines} = [];
63 $self->{manext_push_tbl_state} = 0;
64 $self->{manext_push_tbl_line} = '';
65 return;
66 }
67
68 sub _parse_version {
69 # Parse a po4a version string. Unfortunately these aren't always
70 # directly parseable using version->parse, because they may have more
71 # than two components without a leading "v".
72 my $version = shift;
73 $version = "v$version" unless $version =~ /^v/;
74 return version->parse($version);
75 }
76
77 sub shiftline {
78 my $self = shift;
79
80 if (_parse_version($Locale::Po4a::TransTractor::VERSION) >= 'v0.47') {
81 return $self->SUPER::shiftline();
82 }
83
84 my ($line, $ref);
85
86 if (@{$self->{manext_shift_tbl_lines}}) {
87 ($line, $ref) = @{shift @{$self->{manext_shift_tbl_lines}}};
88 return ($line, $ref);
89 }
90
91 NEXT_LINE:
92 ($line, $ref) = $self->SUPER::shiftline();
93
94 if (!defined $line) {
95 # end of file
96 return ($line, $ref);
97 }
98
99 if ($line =~ /^\.TS/) {
100 $self->{manext_shift_tbl_state} = 1;
101 } elsif ($line =~ /^\.TE/) {
102 $self->{manext_shift_tbl_state} = 0;
103 } elsif ($self->{manext_shift_tbl_state} == 1) {
104 if ($line =~ /\.$/) {
105 $self->{manext_shift_tbl_state} = 2;
106 }
107 } elsif ($self->{manext_shift_tbl_state} == 2) {
108 # Theoretically s/@/\t/g should be enough, but for some reason
109 # Locale::Po4a::Man splits on /\\t/ rather than on /\t/.
110 if ($line =~ /T\{$/) {
111 my ($l2, $r2) = $self->SUPER::shiftline();
112 while (defined $l2) {
113 $line .= $l2;
114 last if $l2 =~ /^T\}/;
115 ($l2, $r2) = $self->SUPER::shiftline();
116 }
117 }
118 if ($line =~ /@/) {
119 for my $column (split /@/, $line) {
120 next if $column =~ /^$/m;
121 push @{$self->{manext_shift_tbl_lines}}, [$column, $ref];
122 }
123 ($line, $ref) = @{shift @{$self->{manext_shift_tbl_lines}}};
124 return ($line, $ref);
125 }
126 }
127
128 return ($line, $ref);
129 }
130
131 sub pushline {
132 my ($self, $line) = (shift, shift);
133
134 if (_parse_version($Locale::Po4a::TransTractor::VERSION) >= 'v0.47') {
135 $self->SUPER::pushline($line);
136 return;
137 }
138
139 if ($line =~ /^\.TS/) {
140 $self->{manext_push_tbl_state} = 1;
141 } elsif ($line =~ /^\.TE/) {
142 $self->{manext_push_tbl_state} = 0;
143 } elsif ($self->{manext_push_tbl_state} == 1) {
144 if ($line =~ /\.$/) {
145 $self->{manext_push_tbl_state} = 2;
146 }
147 } elsif ($self->{manext_push_tbl_state} == 2) {
148 if (length $self->{manext_push_tbl_line}) {
149 $self->{manext_push_tbl_line} .= '@';
150 }
151 $self->{manext_push_tbl_line} .= $line;
152 if ($self->{manext_push_tbl_line} =~ /\n$/) {
153 $self->SUPER::pushline($self->{manext_push_tbl_line});
154 $self->{manext_push_tbl_line} = '';
155 }
156 return;
157 }
158
159 $self->SUPER::pushline($line);
160 return;
161 }
162
163 sub translate {
164 my ($self, $str, $ref, $type, %options) = @_;
165
166 if (defined $type and $type eq 'tbl table') {
167 if (_parse_version($Locale::Po4a::TransTractor::VERSION) < 'v0.47') {
168 if ($str =~ /^T\{\n?(.*)T\}(\n?)$/s) {
169 my $inner = $1;
170 chomp $inner;
171 return "T{\n" .
172 $self->SUPER::translate($inner, $ref, $type, %options) .
173 "\nT}$2";
174 }
175 } else {
176 my $postline = '';
177 if (chomp $str) {
178 $postline = "\n";
179 }
180 if ($str =~ /@/) {
181 return join('@', map {
182 $self->translate($_, $ref, 'tbl table manext')
183 } split (/@/, $str, -1)) . $postline;
184 } else {
185 return $self->translate($str, $ref, 'tbl table manext') .
186 $postline;
187 }
188 }
189 }
190
191 if (defined $type and $type =~ /^tbl table(?: manext)$/) {
192 # Do not translate horizontal rules.
193 return $str if $str =~ /^[-_=]$/;
194
195 # Do not translate table entries that consist only of numbers (e.g.
196 # the sections table in man(1)).
197 return $str if $str =~ /^[0-9]+$/;
198
199 # Do not translate table entries that consist only of a groff
200 # special character or escape (e.g. the --ascii translation table in
201 # man(1)).
202 if ($str =~ /^\\[^([]$/ or # e.g. \`
203 $str =~ /^\\\(..$/ or # e.g. \(bu
204 $str =~ /^\\\[.*?\]$/ or # e.g. \[bu]
205 $str =~ /^\\[^([][^([]$/ or # e.g. \fB
206 $str =~ /^\\[^([]\(..$/ or # e.g. \n(XX
207 $str =~ /^\\[^([]\[.*?\]$/) { # e.g. \*[softhyphen]
208 return $str;
209 }
210
211 # Do not translate "latin1" and "ascii" (from the --ascii
212 # translation table in man(1)).
213 return $str if $str =~ /^(?:latin1|ascii)$/;
214
215 # Do not translate symbols used in the --ascii translation table in
216 # man(1).
217 return $str if $str =~ /^[o'x]$/;
218
219 $type =~ s/ manext$//;
220 }
221
222 if (not defined $type and $self->{type} eq 'TH') {
223 # Do not translate page names and other similar headings.
224 return $str if $str =~ /^%.*%$/ or $str =~ /^[A-Z]+$/;
225 }
226
227 return $self->SUPER::translate($str, $ref, $type, %options);
228 }
229
230 1;