summaryrefslogtreecommitdiff
path: root/help2txt
blob: 6bfb4386442114b044c4618bb9bb82b53d116f2b (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
#! /usr/bin/perl

use Getopt::Long;
use HTML::Parser;

sub text_handler;
sub comment_handler;
sub default_handler;
sub start_handler;
sub end_handler;
sub body_start;
sub find_tag;
sub nospaces;

$opt_product = "openSUSE";

%help_key_rename = (
  'F2' => 'F3',
  'F3' => 'F4',
  'F4' => 'F2',
);

GetOptions(
  'product=s' => \$opt_product
);

$p = HTML::Parser->new(api_version => 3);

# $p->utf8_mode(1);
$p->unbroken_text(1);

$p->handler(default => "" );
$p->handler(start => \&body_start, "self,tagname,line");

$p->parse_file(shift);

# print "help type = \"$helptype\"\n";


$t .= $_->[1] for @t;

$t =~ s/ +/ /sg;
$t =~ s/\n /\n/sg;
$t =~ s/ \n/\n/sg;
$t =~ s/(\x14.*?\x10)\s*/$1/sg;
$t =~ s/\s*(\x04|$)/$1/sg;
$t =~ s/(&product;|\@product\@)/$opt_product/g;

# FIXME: we need a better solution
$t =~ s/(\bF[234]\b)/$help_key_rename{$1}/sg if $helptype eq 'install';

print $t, "\x00";


if($helptype eq 'boot') {
  $ref{help}++;
  $ref{keytable}++;
  $ref{profile}++;
}

if($helptype eq 'install') {
  $ref{bits}++;
}

for (keys %label) {
  if(!$ref{$_}) {
    $err = 1;
    print STDERR "\"$_\" never referenced\n"
  }
}

for (keys %ref) {
  if(!$label{$_}) {
    $err = 1;
    print STDERR "\"$_\" never defined\n"
  }
}

warn "*** inconsistencies detected ***\n" if $err;



sub text_handler
{
  local $_;

  $_ = shift;
  s/\s+/ /g;

  push @t, [ "", $_ ];
}


sub comment_handler
{
  $helptype = $1 if $_[0] =~ /\bhelp=([a-z]+)/;
}


sub default_handler
{
  my $line = shift;

  die "oops at line $line\n";
}


sub body_start
{
  my ($self, $tag) = @_;

  if($tag eq "body") {
    $self->handler(text => \&text_handler, "text");
    $self->handler(comment => \&comment_handler, "text" );
    $self->handler(default => \&default_handler, "line" );
    $self->handler(start => \&start_handler, "self,tagname,attr,line");
    $self->handler(end => \&end_handler, "self,tagname,line");
  }
}


sub start_handler
{
  my ($self, $tag, $attr, $line) = @_;
  my $l;

  return if $tag eq "hr";
  return if $tag eq "h3";

  if($tag eq "br") {
    push @t, [ "", "\n" ];
  }
  elsif($tag eq "em") {
    push @t, [ "em" ];
  }
  elsif($tag eq "a") {
    $l = $attr->{href};
    $l =~ s/^#//;
    push @t, [ "a", undef, $attr->{name}, $l ];
  }
  else {
    die "line $line: unsupported tag $tag\n";
  }
}


sub end_handler
{
  my ($self, $tag, $line) = @_;
  my ($i, $j);

  return if $tag eq "h3";

  if($tag eq "body") {
    $self->handler(text => "");
    $self->handler(comment => "" );
    $self->handler(default => "" );
    $self->handler(start => "");
    $self->handler(end => "");

    return;
  }

  $i = find_tag $tag, $line;
  die "line $line: no matching opening tag for $tag found\n" unless $i;

  if($tag eq "em") {
    $i = pop @t;
    pop @t;
    push @t, [ "", "\x11$i->[1]\x10" ];
  }
  elsif($tag eq "a") {
    $i = pop @t;
    $j = pop @t;
    if($j->[2]) {
      # name
      die "line $line: label $j->[2] too long (max. 32)\n" if length($j->[2]) > 32;
      die "line $line: label $j->[2] already exists\n" if $label{$j->[2]};
      $label{$j->[2]} = 1;
      push @t, [ "", "\x04\x12$j->[2]\x14$i->[1]\x10" ];
    }
    elsif($j->[3]) {
      # href
      die "line $line: label $j->[3] too long (max. 32)\n" if length($j->[3]) > 32;
      $ref{$j->[3]}++;
      push @t, [ "", "\x12$j->[3]\x13" . nospaces($i->[1]) . "\x10" ];
    }
    else {
      die "line $line: strange tag $tag\n";
    }
  }
  else {
    die "line $line: unexpected tag $tag\n";
  }

}


sub find_tag
{
  my $i = @t - 1;

  return undef if $i < 1;

  return 1 if $t[$i][0] eq "" && $t[$i-1][0] eq $_[0];

  die "line $_[1]: nested tags not supported\n";
}


# \x1f looks like a space but is not a space. This is useful
# to prevent automatic line breaks.
sub nospaces
{
  local $_;

  $_ = shift;
  s/\s/\x1f/g;

  return $_;
}