summaryrefslogtreecommitdiffstats
path: root/perl-install/share/po/help_xml2pm.pl
blob: d6b0f8b2cc1a0f135598d60640949a978867023c (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
#!/usr/bin/perl

use XML::Parser;
use MDK::Common;

my $help;
my $dir = "doc/manual/literal/drakx";
my @langs = grep { /^..$/ } all($dir) or die "no XML help found in $dir\n";

my %helps = map {
    my $lang = $_;
    my $p = new XML::Parser(Style => 'Tree');
    my $tree = $p->parsefile("$dir/$lang/drakx-help.xml");

    $lang => rewrite2(rewrite1(@$tree), $lang);
} @langs;

my $base = delete $helps{en} || die;
save_help($base);

foreach my $lang (keys %helps) {
    local *F;
    open F, "> help-$lang.pot";
    print F "\n";
    foreach my $id (keys %{$helps{$lang}}) {
	$base->{$id} or die "$lang:$id doesn't exist in english\n";
	print F qq(# DO NOT BOTHER TO MODIFY HERE, SEE cvs.mandrakesoft.com:/cooker doc/manual/literal/drakx/$lang/drakx-help.xml\n);
	print F qq(msgid ""\n");
	print F join(qq(\\n"\n"), split "\n", $base->{$id});
	print F qq("\nmsgstr ""\n");
	print F join(qq(\\n"\n"), split "\n", $helps{$lang}{$id});
	print F qq("\n\n);
    }
}


sub save_help {
    my ($help) = @_;
    local *F;
    open F, "| LC_ALL=fr iconv -f utf8 -t ascii//TRANSLIT > ../../help.pm";
    print F q{package help;
use common;
%steps = (
empty => '',
};
    print F qq(
$_ => 
__("$help->{$_}"),
) foreach sort keys %$help;
    print F ");\n";
}

# i don't like the default tree format given by XML::Parser,
# rewrite it in my own tree format
sub rewrite1 {
    my ($tag, $tree) = @_;
    my ($attr, @nodes) = @$tree;
    my @l;
    while (@nodes) {
	my ($tag, $tree) = splice(@nodes, 0, 2);
	if ($tag eq '0') {
	    foreach ($tree) {
		s/\s+/ /gs;
		s/"/\\"/g;
	    }
	    push @l, $tree
	} elsif ($tag eq 'screen') {
	    $tree->[1] eq '0' or die "screen tag contains non CDATA\n";
	    push @l, $tree->[2];
	} else {
	    push @l, rewrite1($tag, $tree);
	}
    }
    { attr => $attr, tag => lc $tag, children => \@l };
}

# return the list of nodes named $tag
sub find {
    my ($tag, $tree) = @_;
    if (!ref($tree)) {
	();
    } elsif ($tree->{tag} eq $tag) {
	$tree;
    } else {
	map { find($tag, $_) } @{$tree->{children}};
    }
}

sub rewrite2 {
    my ($tree, $lang) = @_;
    my $i18ned_open_quote  = $ {{ fr => "«", de => "„"}}{$lang};
    my $i18ned_close_quote = $ {{ fr => "»", de => "“"}}{$lang};

    # rewrite2_ fills in $help
    $help = {};
    rewrite2_($tree);
    $help;
}

sub rewrite2_ {
    my ($tree) = @_;
    ref($tree) or return $tree;

    my $text = do {
	my @l = map { rewrite2_($_) } @{$tree->{children}};
	my $text;
	foreach (grep { !/^\s*$/ } @l) {
	    s/^ // if $text =~ /\s$/;
	    $text =~ s/ $// if /^\s/;
	    $text =~ s/\n+$// if /^\n/;
	    $text .= $_;
	}
	$text;
    };

    if (0) {
    } elsif (member($tree->{tag}, 'para', 'itemizedlist', 'orderedlist')) {
	$text =~ s/^\s(?!\s)//;
	$text =~ s/^( ?\n)+//;
	$text =~ s/\s+$//;
	qq(\n$text\n);
    } elsif (member($tree->{tag}, 'quote', 'citetitle', 'foreignphrase')) {
	($i18ned_open_quote || "``") . $text . ($i18ned_close_quote || "''");
    } elsif ($tree->{tag} eq 'guilabel') {
	($i18ned_open_quote || "\\\"") . $text . ($i18ned_close_quote || "\\\"");
    } elsif ($tree->{tag} eq 'command') {
	qq(\\"$text\\");
    } elsif ($tree->{tag} eq 'userinput') {
	qq(>>$text<<);
    } elsif (member($tree->{tag}, 'footnote', 'keysym')) {
	'(*)'
    } elsif ($tree->{tag} eq 'warning') {
	$text =~ s/^(\s+)/$1!! /;
	$text =~ s/(\s+)$/ !!$1/;
	$text;
    } elsif ($tree->{tag} eq 'listitem') {
	my $cnt;
	$text =~ s/^\s+//;
	$text =~ s/^/' ' . ($cnt++ ? '  ' : '* ')/emg;
	"\n$text\n";

    } elsif (member($tree->{tag}, 'guibutton', 'guimenu', 
                    'emphasis', 'acronym', 'keycap', 'ulink', 'tip', 'note',
		    'primary', 'indexterm', 'application', 'keycombo', 
		    'literal', 'superscript', 'xref',
		   )) {
	# ignored tags
	$text;
    } elsif (member($tree->{tag}, 'title', 'article')) {
	# dropped tags
	'';
    } elsif ($tree->{tag} eq 'sect1') {
	$text =~ s/^\s+//;

	my @footnotes = map { 
	    my $s = rewrite2_({ %$_, tag => 'para' });
	    $s =~ s/^\s+//;
	    "(*) $s";
	} find('footnote', $tree);
	$help->{$tree->{attr}{id}} = aerate($text . join('', @footnotes));
	'';
    } elsif ($tree->{tag} eq 'screen') {
	qq(\n$text\n);
    } else {
	die "unknown tag $tree->{tag}\n";
    }
}

sub aerate {
    my ($s) = @_;
    #- the warp_text column is adjusted so that xgettext do not wrap text around
    #- which cause msgmerge to add a lot of fuzzy
    my $s2 = join("\n\n", map { join("\n", warp_text($_, 75)) } split "\n", $s);
    $s2;
}