digraphs: reuse unicode character details
[sheet.git] / tools / mkdigraphlist
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 use open OUT => ':utf8', ':std';
8 use Data::Dumper;
9
10 our $VERSION = '1.03';
11
12 # create a hash of desired input
13 my $di = do 'data/digraphs-rfc.inc.pl'
14         or die "error reading digraphs include: ", $@ // $!;
15
16 # personal addendums
17 my $extra = do 'data/digraphs-shiar.inc.pl'
18         or warn "could not include shiar proposals: ", $@ // $!;
19 $di = { %{$di}, %{$extra // {}} };
20
21 # optionally get unicode character information
22 my $uninfo = do 'unicode-char.inc.pl'
23         or warn "could not include unicode details: ", $@ // $!;
24
25 # convert info hashes into arrays of strings to output in display order
26 for my $row (values %{$uninfo}) {
27         my ($class, $name, $di, $html, $string) = @{$row};
28         $row = [$name, $class];
29         push @{$row}, '', $string if defined $string;
30 }
31
32 # output perl code of hash
33 # (assume no backslashes or curlies, so we can just q{} w/o escaping)
34 print "# automatically generated by $0\n";
35 print "use utf8;\n";
36 print "+{\n";
37 printf '(map {$_=>0} qw{%s}),'."\n", join(' ',
38         map { substr($_, 1, 1).substr($_, 0, 1) } sort keys %{$di}
39 );
40 printf "q{%s}=>[%s],\n", $_, join(',',
41         $di->{$_},   # original code point
42         (map {"'$_'"} @{ $uninfo->{ chr $di->{$_} } // [] }),  # optional additional arguments
43 ) for sort keys %{$di};
44 print "}\n";
45
46 __END__
47
48 =head1 NAME
49
50 mkdigraphlist - Output character list of combined digraph data
51
52 =head1 SYNOPSIS
53
54     mkdigraphlist >digraphs.inc.pl
55     perl -e'$di = do "digraphs.inc.pl"; print chr $di->{DO}->[0]'
56
57 =head1 DESCRIPTION
58
59 Parses the official RFC-1345 document, searching the
60 'character mnemonic table' for all digraph definitions.
61 If successful, Perl code is output resulting in a hash
62 with character data keyed by digraph.
63 Any errors and warnings are given at STDERR.
64
65 The value can either be a scalar string containing another
66 digraph which can be considered identical (usually inverted),
67 or an array ref containing at least the resulting character's
68 Unicode code point value.  If available, the following UCD data
69 is appended: character name, category, script, and output string.
70 For example:
71
72  +{
73    AE => [198, 'LATIN CAPITAL LETTER AE', 'Lu Xl', 'Latin'],
74    EA => 'AE',
75   }
76
77 =head1 AUTHOR
78
79 Mischa POSLAWSKY <perl@shiar.org>
80
81 =head1 LICENSE
82
83 Licensed under the GNU Affero General Public License version 3.
84