6 use open OUT => ':utf8', ':std';
11 # determine input data source
14 # manual contents specified (either piped or filename(s) given)
18 # automatic download from official website
20 my $contents = LWP::Simple::get('http://www.ietf.org/rfc/rfc1345.txt')
21 or die "Couldn't download RFC-1345 from ietf.org";
22 open $input, '<', \$contents; # emulate file handle
25 # skip everything until a character indented by 1 space (table start)
28 defined or die "Premature input end";
31 my @t = $_; # add first line (already read, assume it's ok)
33 # read the rest of the character table
34 while ($_ = readline $input) {
35 # check for table end (chapter 4)
38 # parse table lines (ignore (unindented) page break)
44 # continuation line (add to last entry)
53 # create a hash of desired input
56 my ($mnem, $char, $name) = split / +/, $_, 3;
57 next if length $mnem != 2;
58 $di{$mnem} = hex $char;
63 0xE001 => 0, # join lines: not accepted
64 0xE004 => 0, # umlaut is no different from diaeresis 0x0308
65 0xE005 => 0x0344, # discouraged
87 0xE01B => 0x03D0, # middle beta = curled beta?
91 0xE01F => 0x33C2, # am, compatibility char
92 0xE020 => 0x33D8, # pm, compatibility char
95 0xE023 => 0, # dutch guilder 0192 is already encoded, and not very useful anyway
97 0xE025 => 0x20D7, # also 20D1; non-spacing
100 0xE028 => 0x01F0, #but uppercase
103 $_ >= 0xE000 or next;
104 $_ = $trans{$_} if defined $trans{$_};
109 if (-r 'shiar.inc.txt') {
110 open my $include, '<:utf8', 'shiar.inc.txt';
111 for (readline $include) {
112 m{^([!"%'-Z_a-z]{2}) (.)} or next;
113 warn("$1 already defined"), next if defined $di{$1};
120 # optionally get unicode character information
122 require Unicode::UCD;
124 $_ => Unicode::UCD::charinfo($di{$_})
125 || { block => '?', category => 'Xn', name => '', script => '' }
129 # add custom categories for certain blocks
131 $_->{category} .= ' Xa' if $_->{block} eq 'Basic Latin';
132 $_->{category} .= ' Xl' if $_->{block} eq 'Latin-1 Supplement';
135 # mark unofficial extras as such
136 $info{$_}->{category} .= ' Xz' for @extra;
139 $info{$_}->{string} = chr(9676) . chr($di{$_}) if $info{$_}->{combining};
140 # find control characters (first 32 chars from 0 and 128)
141 next unless ($di{$_} & ~0b1001_1111) == 0 or $di{$_} == 127;
142 # rename to something more descriptive
143 $info{$_}->{name} = $info{$_}->{unicode10}
144 ? '<'.$info{$_}->{unicode10}.'>' # the old name was much more useful
145 : sprintf('<control U+%04X>', $di{$_}); # at least identify by value
146 # show descriptive symbols instead of control chars themselves
147 $info{$_}->{string} = $di{$_} < 32 ? chr($di{$_} + 0x2400) : chr(0xFFFD);
150 # convert info hashes into arrays of strings to output in display order
151 for my $row (values %info) {
152 $row = [ map { $row->{$_} } qw/name category script string/ ];
153 # strip off trailing missing values (especially string may be unknown)
154 defined $row->[-1] ? last : pop @$row for 1 .. @$row;
157 # output perl code of hash
158 # (assume no backslashes or curlies, so we can just q{} w/o escaping)
160 printf '(map {$_=>0} qw{%s}),'."\n", join(' ',
161 map { substr($_, 1, 1).substr($_, 0, 1) } sort keys %di
163 printf "q{%s}=>[%s],\n", $_, join(',',
164 $di{$_}, # original code point
165 $info{$_} # optional additional arguments
166 ? map {"'$_'"} @{ $info{$_} }
175 rfc1345convert - Output digraph data from RFC-1345
179 Download and convert the digraph specification from ietf.org:
181 rfc1345convert > digraphs.inc.pl
183 Test by printing the character for DO (should be a dollar sign):
185 perl -e'$di = do "digraphs.inc.pl"; print chr $di->{DO}->[0]'
187 Manual specification of source retrieval:
189 rfc1345convert rfc1345.txt
190 curl $url | rfc1345convert -
194 Parses the official RFC-1345 document, searching the
195 'character mnemonic table' for all digraph definitions.
196 If successful, Perl code is output resulting in a hash
197 with character data keyed by digraph.
198 Any errors and warnings are given at STDERR.
200 The value can either be a scalar string containing another
201 digraph which can be considered identical (usually inverted),
202 or an array ref containing at least the resulting character's
203 Unicode code point value. If available, the following UCD data
204 is appended: character name, category, script, and output string.
208 AE => [198, 'LATIN CAPITAL LETTER AE', 'Lu Xl', 'Latin'],
214 Mischa POSLAWSKY <perl@shiar.org>
218 Licensed under the GNU Affero General Public License version 3.