digraphs: separate rfc parser from include generator
[sheet.git] / tools / mkdigraphs-rfc
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use utf8;
6 use open OUT => ':utf8', ':std';
7
8 our $VERSION = '1.00';
9
10 # expect input data source at command line
11 @ARGV or die "Specify input source file or - for STDIN\n";
12
13 # skip everything until a character indented by 1 space (table start)
14 do {
15         $_ = readline;
16         defined or die "Premature input end";
17 } until /^\s\S/;
18
19 my @t = $_;  # add first line (already read, assume it's ok)
20
21 # read the rest of the character table
22 while ($_ = readline) {
23         # check for table end (chapter 4)
24         last if /^\d/;
25
26         # parse table lines (ignore (unindented) page break)
27         next unless s/^ //;
28         chomp;
29
30         # add the line to @t
31         if (s/^ {15}/ /) {
32                 # continuation line (add to last entry)
33                 $t[-1] .= $_;
34         }
35         else {
36                 # add a new entry
37                 push @t, $_;
38         }
39 }
40
41 # create a hash of desired input
42 my %di;
43 for (@t) {
44         my ($mnem, $char, $name) = split / +/, $_, 3;
45         next if length $mnem != 2;
46         $di{$mnem} = hex $char;
47 }
48
49 # XXX
50 my %trans = (
51         0xE001 => 0,  # join lines: not accepted
52         0xE004 => 0,  # umlaut is no different from diaeresis 0x0308
53         0xE005 => 0x0344, # discouraged
54         0xE006 => 0x0300,
55         0xE007 => 0x0301,
56         0xE008 => 0x0302,
57         0xE009 => 0x0303,
58         0xE00A => 0x0304,
59         0xE00B => 0x0306,
60         0xE00C => 0x0307,
61         0xE00D => 0x0308,
62         0xE00E => 0x030A,
63         0xE00F => 0x030B,
64         0xE010 => 0x030C,
65         0xE011 => 0x0327,
66         0xE012 => 0x0328,
67         0xE013 => 0x0332,
68         0xE014 => 0x0333,
69         0xE015 => 0x0338,
70         0xE016 => 0x0345,
71         0xE017 => 0x0314,
72         0xE018 => 0x0313,
73         0xE019 => 0x1FFE,
74         0xE01A => 0x1FBF,
75         0xE01B => 0x03D0,  # middle beta = curled beta?
76         0xE01C => 0x25CB,
77         0xE01D => 0x0192,
78         0xE01E => 0x0292,
79         0xE01F => 0x33C2,  # am, compatibility char
80         0xE020 => 0x33D8,  # pm, compatibility char
81         0xE021 => 0x2121,
82         0xE022 => 0xFE8E,
83         0xE023 => 0,  # dutch guilder 0192 is already encoded, and not very useful anyway
84         0xE024 => 0x0393,
85         0xE025 => 0x20D7,  # also 20D1; non-spacing
86         0xE026 => 0x1FEF,
87         0xE027 => 0x1FC0,
88         0xE028 => 0x01F0, #but uppercase
89 );
90 for (values %di) {
91         $_ >= 0xE000 or next;
92         $_ = $trans{$_} if defined $trans{$_};
93 }
94
95 # output perl code of hash
96 # (assume no backslashes or curlies, so we can just q{} w/o escaping)
97 print "# automatically generated by $0\n";
98 print "use utf8;\n";
99 print "+{\n";
100 printf "q{%s}=>%s,\n", $_, $di{$_} for sort keys %di;
101 print "}\n";
102
103 __END__
104
105 =head1 NAME
106
107 mkdigraphs-rfc - Output digraph data from RFC-1345
108
109 =head1 SYNOPSIS
110
111 Extract digraphs from text specifications as a perl hash:
112
113     mkdigraphs-rfc rfc1345.txt >digraphs-rfc.inc.pl
114
115 Input can be the literal RFC (or similar) document:
116
117     curl http://www.ietf.org/rfc/rfc1345.txt | mkdigraphlist -
118
119 Test by printing the character for DO (should be a dollar sign):
120
121     perl -e'$di = do "digraphs-rfc.inc.pl"; print chr $di->{DO}'
122
123 =head1 DESCRIPTION
124
125 Parses the official RFC-1345 document, searching the
126 'character mnemonic table' for all digraph definitions.
127 If successful, Perl code is output resulting in a hash
128 with Unicode code points keyed by digraph.
129 Obsolete values (references to private use area)
130 are converted to modern alternatives.
131 Any errors and warnings are given at STDERR.
132
133 =head1 AUTHOR
134
135 Mischa POSLAWSKY <perl@shiar.org>
136
137 =head1 LICENSE
138
139 Licensed under the GNU Affero General Public License version 3.
140