digraphs: encode data include directly as json
[sheet.git] / tools / mkdigraphlist
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 use open OUT => ':encoding(utf-8)', ':std';
8 use JSON ();
9
10 our $VERSION = '1.07';
11
12 # import and combine various digraph data
13 push @INC, 'data';
14 my $rfc = do 'digraphs-rfc.inc.pl'
15         or die "error reading digraphs include: ", $@ // $!;
16 my $extra = do 'digraphs-shiar.inc.pl'
17         or warn "could not include shiar proposals: ", $@ // $!;
18 my $vim = do 'digraphs-vim.inc.pl'
19         or warn "could not include vim extensions ", $@ // $!;
20 my $vimold = do 'digraphs-vim-74.inc.pl'
21         or warn "could not include vim compatibility ", $@ // $!;
22 my $di = { %{$vim // {}}, %{$rfc}, %{$extra // {}} };
23
24 # optionally get unicode character information
25 my $uninfo = do 'unicode-char.inc.pl'
26         or warn "could not include unicode details: ", $@ // $!;
27
28 # output json map of character info
29 my %table;
30 $table{$_} = 0 for (
31         grep { !defined $di->{$_} }
32         map { substr($_, 1, 1).substr($_, 0, 1) } sort keys %{$di}
33 );
34 $table{$_} = [
35         ord $di->{$_},   # original code point
36         $uninfo->{ $di->{$_} }->[1] // '',  # name
37         join(' ',
38                 $rfc->{$_}
39                         ? $vim->{$_} ? 'l4' : 'l1'  # vim+rfc or rfc only
40                         : $vim->{$_} ? 'l3' : 'l2', # vim only or neither
41                 $vimold && $vim->{$_} && !$vimold->{$_} ? 'ex' : (), # new vim feature
42         ),
43         ($uninfo->{ $di->{$_} }->[0] // '') =~ s/ u-di| u-prop| ex//gr,  # class
44         $uninfo->{ $di->{$_} }->[4] // (),  # string
45 ] for sort keys %{$di};
46 print JSON->new->ascii->canonical->encode(\%table);
47
48 __END__
49
50 =head1 NAME
51
52 mkdigraphlist - Output character list of combined digraph data
53
54 =head1 SYNOPSIS
55
56     mkdigraphlist | jq -r '."DO"[0]' | perl -nE 'say chr' # $
57
58 =head1 DESCRIPTION
59
60 Combines precompiled digraph includes of rfc (1345), vim, and shiar
61 and outputs a complete map including character details and usage classes.
62
63 The value can either be a scalar string containing another
64 digraph which can be considered identical (usually inverted),
65 or an array ref containing at least the resulting character's
66 Unicode code point value.  If available, the following UCD data
67 is appended: character name, usage classes, unicode classes,
68 and replacement output string.
69 For example:
70
71   {
72    "AE" => [198, "LATIN CAPITAL LETTER AE", "u-di", "Latin Lu Xl u-v11"],
73    "EA" => "AE",
74   }
75
76 =head1 AUTHOR
77
78 Mischa POSLAWSKY <perl@shiar.org>
79
80 =head1 LICENSE
81
82 Licensed under the GNU Affero General Public License version 3.
83