summaryrefslogtreecommitdiffstats
path: root/manpo/mantranslate.pl
blob: 713da103c0be37246c2ee2c7550af877cb95915d (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
#! /usr/bin/perl
# This script extracts translateable strings from a manpage and/or
# translates a manpage using gettext.
# Copyright (C) 2006  Henning Makholm
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

use strict ; use warnings ;

my ($gettext,$shipout) ;

sub fontexpand1($$) {
    my ($kinds,$text) = @_ ;
    return "\\f$kinds$text\\fP" if 1 == length $kinds ;
    my $out = "" ;
    my $last = "R" ;
    while( $text )  {
        $kinds =~ s/^(.)(.*)/$2$1/ ;
        $last = $1 ;
        if( $text =~ s/^([^\" ]+)\s*// ||
            $text =~ s/^"([^\"]+)"\s*// ) {
            $out .= "\\f$last$1" ;
        } else {
            die "Bad font-change tail '$text'" ;
        }
    }
    $out .= "\\fR" unless $last eq "R" ;
    return $out ;
}
            

sub ggettext($$$) {
    my ($filename,$lineno,$text) = @_ ;
    return $text if $text =~ /^\d*$/ ;
    $text =~ s/\\\".*//g ;
    $text =~ s/\\\*p/%s/g ;
    $text =~ s/([^-])\\(-[a-zA-Z])/$1$2/g ;
    $text =~ s/^\.([BIR]+)\s+(.*)$/ fontexpand1($1,$2) /mge ;
    my $fontcode = $text !~ /[{}]/ ;
    if( $fontcode ) {
        $text =~ s/\\f([BI])(([^\\]|\\[^f])*)\\f[RP]/$1\{$2\}/mg ;
    }
    my $newline = $text =~ s/\n$//s ;
    if( $text !~ /^[.\']/m || $text =~ /\n/ ) {
        $text =~ s/\s+/ /gs ;
        my $pre = "" ;
        while( length($text) > 65 ) {
            $text =~ s/^(.{1,65}) // or $text =~ s/^([^ ]*) // ;
            $pre .= "$1\n" ;
        }
        $text = "$pre$text" ;
        $text =~ s/\s*$// ;
    }
    $text = $gettext->($filename,$lineno,$text);
    $text .= "\n" if $newline ;
    if( $fontcode ) {
        $text =~ s/([BI])\{([^{}]*)\}/\\f$1$2\\fP/g ;
    }
    $text =~ s/([^-])(-[a-zA-Z])/$1\\$2/g ;
    $text =~ s/%s/\\*p/g ;
    return $text ;
}

sub partraverse {
    my ($filename,$lineno,@lines) = @_ ;
    return unless @lines ;
    my @front ;
    foreach my $line ( @lines ) {
        if( @front &&
            $front[$#front] =~ /\.\)?\s*$/ &&
            $line =~ /^\(?[A-Z]/ ) {
            $shipout->(ggettext($filename,$lineno,join "",@front));
            $lineno += @front ;
            @front = () ;
        }
        push @front, $line ;
    }
    $shipout->(ggettext($filename,$lineno,join "",@front));
}

sub traverser($) {
    my ($filename) = @_ ;
    open MAN, "<", $filename or die "Cannot open $filename" ;
    my $lineno = 0 ;
    my @saved = () ;
    while( <MAN> ) {
        $lineno++ ;
        if( /^([^.\'\#]|.([BI]|[BRI][IRB]) )/ ) {
            push @saved, $_ ;
            next ;
        }
        if( @saved ) {
            partraverse($filename,$lineno-@saved,@saved);
            @saved = () ;
        }
        if( /^#line (\d+) \"(.*)\"/ ) {
            my ($newline,$newfile) = ($1,$2);
            $filename = $newfile ;
            $lineno = $newline-1 ;
        } elsif( /^\#/ ) {
            # Ok, breaking the translation block is all we need.
        } elsif( /^\.\\\"-+$/ ){
            # Another way to request block breaking.
        } elsif( /^\.(\\\"|TH|so|P|IP|ds)($| )/ ) {
            $shipout->($_) ;
        } elsif( /^\.(SH) (.*)$/ ) {
            $shipout->(".$1 ".ggettext($filename,$lineno,$2)."\n") ;
        } elsif( /^\.TP/ ) {
            $shipout->($_) ;
            $_ = <MAN> ; $lineno++ ;
            if( /^\\fB\\-/ ) {
                s/\\fI(.*?)\\fR/
                    "\\fI".ggettext($filename,$lineno,$1)."\\fR"/ge ;
                $shipout->($_) ;
            } else {
                partraverse($filename,$lineno,$_) ;
            }
        } else {
            /^.?(.?.?)/ ;
            die "$filename:$lineno: unsupported request .$1\n" ;
        }
    }
    if( @saved ) {
        partraverse($filename,$lineno-@saved,@saved);
    }
    close MAN ;
}


if( @ARGV && $ARGV[0] eq '-x' ) {
    shift @ARGV ;
    my %seenwhere = () ;
    my @strings = () ;
    $gettext = sub {
        my ($file,$line,$text) = @_ ;
        push @strings, $text unless exists $seenwhere{$text} ;
        push @{$seenwhere{$text}}, "$file:$line" ;
    };
    $shipout = sub {} ;
    for my $filename ( @ARGV ) {
        traverser($filename) ;
    }
    my $date = `date "+%Y-%m-%d %H:%M%z"` ;
    chomp $date ;
    print << "EOF" ;
# SOME DESCRIPTIVE TITLE.
# This file is put in the public domain.
# FIRST AUTHOR <EMAIL\@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Xcftools-manpages VERSION\\n"
"Report-Msgid-Bugs-To: henning\@makholm.net\\n"
"POT-Creation-Date: $date\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: FULL NAME <EMAIL\@ADDRESS>\\n"
"Language-Team: LANGUAGE <LL\@li.org>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=CHARSET\\n"
"Content-Transfer-Encoding: 8bit\\n"

EOF
;
    for my $string ( @strings )  {
        print(join(" ","#:",@{$seenwhere{$string}}),"\n");
        $string =~ s/([\\\"])/\\$1/g ;
        $string =~ s/\n$/\\n/ ;
        print "msgid \"" ;
        print(join "\\n\"\n      \"",split /\n/, $string) ;
        print "\"\n";
        print "msgstr \"\"\n\n" ;
    }
    exit 0 ;
}

if( @ARGV == 2 ) {
    my ($pofile,$manfile) = @ARGV ;
    open PO, "<", $pofile or die "Pofile $pofile not found" ;
    my %catalog ;
    my $msgid ;
    my $addto ;
    while( <PO> ) {
        # This is an extremely simple-minded parser.
        if( s/^\s*msgid\s+\"/\"/ ) {
            $msgid = "" ;
            $addto = \$msgid ;
        } elsif( s/^\s*msgstr\s+\"/\"/ ) {
            $catalog{$msgid} = "" ;
            $addto = \$catalog{$msgid} ;
        }
        if( /^\s*\"(.*)\"\s*$/ ) {
            $_ = $1 ;
            s/\\(.)/$1 eq 'n' ? "\n" : $1/ge ;
            $$addto .= $_ ;
        }
    }
    close PO ;
    $gettext = sub {
        my ($file,$line,$text) = @_ ;
        return $catalog{$text} if $catalog{$text} ;
        return $text ;
    };
    $shipout = sub {
        print @_ ;
    };
    traverser($manfile) ;
    exit 0 ;
}

print STDERR "Usage: $0 -x man-source-files\n" ;
print STDERR "   or: $0 pofile manfile\n" ;