summaryrefslogtreecommitdiffstats
path: root/man/po4a/Locale/Po4a/Manext.pm
blob: bc939cf042b6bf4e4e73096f0b6cd19286f0337b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#! /usr/bin/perl -w

=encoding UTF-8

=head1 NAME

Locale::Po4a::Manext - private extended version of Locale::Po4a::Man

=head1 DESCRIPTION

Extended version of Locale::Po4a::Man that copes with a few intricacies of
man-db's manual pages, until such time as these are incorporated into po4a
proper.

=head1 SEE ALSO

L<Locale::Po4a::Man(3pm)>.

=head1 AUTHOR

 Colin Watson <cjwatson@debian.org>

=head1 COPYRIGHT AND LICENCE

Copyright 2011 Colin Watson.

man-db is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

man-db is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with man-db; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

=cut

package Locale::Po4a::Manext;

use 5.006;
use strict;
use warnings;

use vars qw(@ISA);
@ISA = qw(Locale::Po4a::Man);

use version;

use Locale::Po4a::Man;
use Locale::Po4a::TransTractor;

sub initialize {
    my $self = shift;
    $self->SUPER::initialize(@_);

    $self->{manext_shift_tbl_state} = 0;
    $self->{manext_shift_tbl_lines} = [];
    $self->{manext_push_tbl_state} = 0;
    $self->{manext_push_tbl_line} = '';
}

sub _parse_version {
    # Parse a po4a version string.  Unfortunately these aren't always
    # directly parseable using version->parse, because they may have more
    # than two components without a leading "v".
    my $version = shift;
    $version = "v$version" unless $version =~ /^v/;
    return version->parse($version);
}

sub shiftline {
    my $self = shift;

    if (_parse_version($Locale::Po4a::TransTractor::VERSION) >= 'v0.47') {
        return $self->SUPER::shiftline();
    }

    my ($line, $ref);

    if (@{$self->{manext_shift_tbl_lines}}) {
        ($line, $ref) = @{shift @{$self->{manext_shift_tbl_lines}}};
        return ($line, $ref);
    }

NEXT_LINE:
    ($line, $ref) = $self->SUPER::shiftline();

    if (!defined $line) {
        # end of file
        return ($line, $ref);
    }

    if ($line =~ /^\.TS/) {
        $self->{manext_shift_tbl_state} = 1;
    } elsif ($line =~ /^\.TE/) {
        $self->{manext_shift_tbl_state} = 0;
    } elsif ($self->{manext_shift_tbl_state} == 1) {
        if ($line =~ /\.$/) {
            $self->{manext_shift_tbl_state} = 2;
        }
    } elsif ($self->{manext_shift_tbl_state} == 2) {
        # Theoretically s/@/\t/g should be enough, but for some reason
        # Locale::Po4a::Man splits on /\\t/ rather than on /\t/.
        if ($line =~ /T\{$/) {
            my ($l2, $r2) = $self->SUPER::shiftline();
            while (defined $l2) {
                $line .= $l2;
                last if $l2 =~ /^T\}/;
                ($l2, $r2) = $self->SUPER::shiftline();
            }
        }
        if ($line =~ /@/) {
            for my $column (split /@/, $line) {
                next if $column =~ /^$/m;
                push @{$self->{manext_shift_tbl_lines}}, [$column, $ref];
            }
            ($line, $ref) = @{shift @{$self->{manext_shift_tbl_lines}}};
            return ($line, $ref);
        }
    }

    return ($line, $ref);
}

sub pushline {
    my ($self, $line) = (shift, shift);

    if (_parse_version($Locale::Po4a::TransTractor::VERSION) >= 'v0.47') {
        $self->SUPER::pushline($line);
        return;
    }

    if ($line =~ /^\.TS/) {
        $self->{manext_push_tbl_state} = 1;
    } elsif ($line =~ /^\.TE/) {
        $self->{manext_push_tbl_state} = 0;
    } elsif ($self->{manext_push_tbl_state} == 1) {
        if ($line =~ /\.$/) {
            $self->{manext_push_tbl_state} = 2;
        }
    } elsif ($self->{manext_push_tbl_state} == 2) {
        if (length $self->{manext_push_tbl_line}) {
            $self->{manext_push_tbl_line} .= '@';
        }
        $self->{manext_push_tbl_line} .= $line;
        if ($self->{manext_push_tbl_line} =~ /\n$/) {
            $self->SUPER::pushline($self->{manext_push_tbl_line});
            $self->{manext_push_tbl_line} = '';
        }
        return;
    }

    $self->SUPER::pushline($line);
}

sub translate {
    my ($self, $str, $ref, $type) = (shift, shift, shift, shift);
    my %options = @_;

    if (defined $type and $type eq 'tbl table') {
        if (_parse_version($Locale::Po4a::TransTractor::VERSION) < 'v0.47') {
            if ($str =~ /^T\{\n?(.*)T\}(\n?)$/s) {
                my $inner = $1;
                chomp $inner;
                return "T{\n" .
                       $self->SUPER::translate($inner, $ref, $type, %options) .
                       "\nT}$2";
            }
        } else {
            my $postline = '';
            if (chomp $str) {
                $postline = "\n";
            }
            if ($str =~ /@/) {
                return join('@', map {
                    $self->translate($_, $ref, 'tbl table manext')
                } split (/@/, $str, -1)) . $postline;
            } else {
                return $self->translate($str, $ref, 'tbl table manext') .
                       $postline;
            }
        }
    }

    if (defined $type and $type =~ /^tbl table(?: manext)$/) {
        # Do not translate horizontal rules.
        return $str if $str =~ /^[-_=]$/;

        # Do not translate table entries that consist only of numbers (e.g.
        # the sections table in man(1)).
        return $str if $str =~ /^[0-9]+$/;

        # Do not translate table entries that consist only of a groff
        # special character or escape (e.g. the --ascii translation table in
        # man(1)).
        if ($str =~ /^\\[^([]$/ or              # e.g. \`
            $str =~ /^\\\(..$/ or               # e.g. \(bu
            $str =~ /^\\\[.*?\]$/ or            # e.g. \[bu]
            $str =~ /^\\[^([][^([]$/ or         # e.g. \fB
            $str =~ /^\\[^([]\(..$/ or          # e.g. \n(XX
            $str =~ /^\\[^([]\[.*?\]$/) {       # e.g. \*[softhyphen]
            return $str;
        }

        # Do not translate "latin1" and "ascii" (from the --ascii
        # translation table in man(1)).
        return $str if $str =~ /^(?:latin1|ascii)$/;

        # Do not translate symbols used in the --ascii translation table in
        # man(1).
        return $str if $str =~ /^[o'x]$/;

        $type =~ s/ manext$//;
    }

    if (not defined $type and $self->{type} eq 'TH') {
        # Do not translate page names and other similar headings.
        return $str if $str =~ /^%.*%$/ or $str =~ /^[A-Z]+$/;
    }

    return $self->SUPER::translate($str, $ref, $type, %options);
}

1;