7a201a11edcbba198bbd54874bdd747dd936b800
[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         grep { !defined $di->{$_} }
39         map { substr($_, 1, 1).substr($_, 0, 1) } sort keys %{$di}
40 );
41 printf "q{%s}=>[%s],\n", $_, join(',',
42         $di->{$_},   # original code point
43         (map {"'$_'"} @{ $uninfo->{ chr $di->{$_} } // [] }),  # optional additional arguments
44 ) for sort keys %{$di};
45 print "}\n";
46
47 __END__
48
49 =head1 NAME
50
51 mkdigraphlist - Output character list of combined digraph data
52
53 =head1 SYNOPSIS
54
55     mkdigraphlist >digraphs.inc.pl
56     perl -e'$di = do "digraphs.inc.pl"; print chr $di->{DO}->[0]'
57
58 =head1 DESCRIPTION
59
60 Parses the official RFC-1345 document, searching the
61 'character mnemonic table' for all digraph definitions.
62 If successful, Perl code is output resulting in a hash
63 with character data keyed by digraph.
64 Any errors and warnings are given at STDERR.
65
66 The value can either be a scalar string containing another
67 digraph which can be considered identical (usually inverted),
68 or an array ref containing at least the resulting character's
69 Unicode code point value.  If available, the following UCD data
70 is appended: character name, category, script, and output string.
71 For example:
72
73  +{
74    AE => [198, 'LATIN CAPITAL LETTER AE', 'Lu Xl', 'Latin'],
75    EA => 'AE',
76   }
77
78 =head1 AUTHOR
79
80 Mischa POSLAWSKY <perl@shiar.org>
81
82 =head1 LICENSE
83
84 Licensed under the GNU Affero General Public License version 3.
85