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
|
#!/usr/bin/perl
use XML::Parser;
use MDK::Common;
@ARGV == 1 or die "usage: help_xml2pm <drakx-help.xml>\n";
my $p = new XML::Parser(Style => 'Tree');
my $tree = $p->parsefile($ARGV[0]);
my $help = {};
# rewrite2 fills in $help
rewrite2(rewrite1(@$tree));
print
q{package help;
use common;
%steps = (
empty => '',
};
print qq(
$_ =>
__("$help->{$_}"),
) foreach sort keys %$help;
print ");\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, $tag eq '0' ? $tree : rewrite1($tag, $tree);
}
{ attr => $attr, tag => $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) = @_;
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 ($tree->{tag} eq 'quote') {
qq(``$text'');
} elsif ($tree->{tag} eq 'command') {
qq(\\"$text\\");
} elsif ($tree->{tag} eq 'userinput') {
qq(>>$text<<);
} elsif ($tree->{tag} eq 'footnote') {
'(*)'
} 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', 'guilabel',
'emphasis', 'acronym', 'keycap', 'ulink', 'tip', 'note',
'primary', 'indexterm',
)) {
# 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));
'';
} else {
die "unknown tag $tree->{tag}\n";
}
}
sub aerate {
my ($s) = @_;
my $s2 = join("\n\n", map { join("\n", warp_text($_)) } split "\n", $s);
$s2;
}
|