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

sub nospaces;

if(!@ARGV) {
  print "Usage: help2txt help_file\n" .
  "Converts a help file from HTML to the format used by the boot loader.\n";
  exit;
}

while(<>) {
  s/\s*<\/?(hr|body|html|h\d+)\s*>//g;
  s/\s*<!.*?>\s*//g;
  s/<(em|strong)>/\x11/g;
  s/<\/(em|strong)>/\x10/g;
  while(/<a\s/) {
    if(s/\<a\s+name=\"([^"]+)\">([^<]*)<\/a>\s*/\x04\x12$1\x14$2\x10/) {
      if($anchor{$1}) {
        die "line $.: anchor \"$1\" already exists\n";
      }
      else {
        $anchor{$1} = 1;
      }
      die "line $.: label \"$1\" too long (max. 32)\n" if length $1 > 32;
    }
    elsif(s/<a\s+href=\"#([^"]+)\">([^<]*)<\/a>/"\x12" . $1 . "\x13" . nospaces($2) . "\x10"/e) {
      $ref{$1}++;
      die "line $.: label \"$1\" too long (max. 32)\n" if length $1 > 32;
    }
    else {
      die "line $.: invalid 'A' element\n";
    }
  }

  $txt .= $_;
}

$txt =~ s/\n( +)(?!\n)/"\n<!" . length($1) . ">"/seg;
$txt =~ s/\s+/ /sg;
$txt =~ s/\s*<br>\s*/\n/sg;
$txt =~ s/<!(\d+)>/" " x $1/seg;
$txt =~ s/(\x14.*?\x10)\s*/$1/sg;
$txt =~ s/\s*(\x04|$)/$1/sg;

print $txt, "\x00";

$ref{help}++;

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

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

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


# \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;

  $_
}