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