Imported Upstream version 1.48.3
[platform/upstream/help2man.git] / build-aux / fixup-texi-trans
1 #!/usr/bin/perl
2
3 # Post-process the translated texinfo page to add the correct path for
4 # install-info, and to correct the alignment of the main menu.
5
6 use strict;
7 use warnings;
8 use open qw(:std :utf8);  # assume utf8 encoding
9 use Text::Wrap;
10
11 $Text::Wrap::columns = 72;
12
13 *OUT = *STDOUT;
14 if (@ARGV and $ARGV[0] =~ /^-o(.*)/)
15 {
16     shift;
17     my $out = $1 || shift || die "$0: missing output file\n";
18     open OUT, '>', $out or die "$0: can't open $out ($!)\n";
19 }
20
21 my %fixed;
22 my $encoding = 'none';
23 my $lang;
24 my $last = '';
25
26 $/ = '';  # read paragraphs
27 while (<>)
28 {
29     if (!$lang and /^This is help2man-(.*)\.info\b/)
30     {
31         $lang = $1;
32         next;
33     }
34
35     if (!$fixed{menu} and $last =~ /^\* Menu:\n*$/)
36     {
37         my @entries;
38         my $width = 0;
39         for (split /\n/)
40         {
41             if (/^\* (.*)::\s+(.*)/)
42             {
43                 my $w = length $1;
44                 push @entries, [$w, $1, $2];
45                 # Stash the largest width (within limits) to use when
46                 # calculating the indent.
47                 $width = $w if $w > $width and $w < 40;
48             }
49             else
50             {
51                 # Append to previous entry.
52                 s/^\s*//;
53                 $entries[-1][-1] .= $_;
54             }
55         }
56
57         $_ = join "\n", (map {
58             my ($w, $node, $description) = @$_;
59             my $prefix = "* ${node}::";
60             # 6 for leading *, trailing ::, and spaces.
61             my $indent = ' ' x ($width + 6);
62             my $first;
63             if ($w > $width)
64             {
65                 $first = $indent;
66                 $prefix .= "\n";
67             }
68             else
69             {
70                 $first = sprintf "%-*s", length $indent, $prefix;
71                 $prefix = '';
72             }
73
74             $prefix . wrap $first, "  $indent", $description;
75         } @entries), "\n";
76
77         $fixed{menu}++;
78         next;
79     }
80
81     if (!$fixed{info_dir} and /^START-INFO-DIR-ENTRY/m and $lang)
82     {
83         my $first = "* help2man-$lang: (help2man-$lang).  ";
84         my $indent = ' ' x ((length $first) + 2);
85         s/^\* help2man: \(help2man\)\.\s*(.*)\nEND-INFO-DIR-ENTRY/
86             (my $t = $1) =~ s#\s+# #g;  # normalise spaces
87             (wrap $first, $indent, $t) . "\nEND-INFO-DIR-ENTRY"/mse or next;
88
89         $fixed{info_dir}++;
90         next;
91     }
92
93     if (/^Local Variables:/m and /^coding: (\S+)/m)
94     {
95         $encoding = $1;
96         next;
97     }
98 }
99 continue
100 {
101     $last = $_;
102     print OUT;
103 }
104
105 warn "$0: didn't find menu to correct\n" unless $fixed{menu};
106 warn "$0: didn't find info dir entry to correct\n" unless $fixed{info_dir};
107 warn "$0: expected utf-8 encoding, found $encoding\n"
108     unless $encoding eq 'utf-8';