4d25445ba2a9bdeb61203891d7943820e7098e4f
[sheet.git] / tools / mkdigraphs-xorg
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use utf8;
5 use open IO => ':encoding(utf-8)', ':std';
6 use re '/msx';
7 use lib '.';
8 use JSON 'decode_json';
9 use Data::Dump 'pp';
10 use Shiar_Sheet::FormatChar;
11
12 our $VERSION = '1.01';
13
14 my $matchvim;  # enable to prefer best compatibility
15
16 my $symname = eval {
17         open my $keysymh, '<', 'data/keysymdef.json' or die $!;
18         local $/;
19         return decode_json(readline $keysymh);
20 } or die "Could not read keysym definitions: $@\n";
21
22 my $vidi = eval {
23         open my $jsfh, '<', 'data/digraphs.json' or die $!;
24         local $/;
25         return JSON->new->decode(readline $jsfh);
26 } or warn "Could not read comparison digraphs: $@\n";
27
28 my %table;
29 while ($_ = readline) {
30         my ($mnem, $chr, $trail) = m/\A <Multi_key> \h (.*?) \h+ : \h "([^"]+)" \h* (.*)/
31                 or next;
32         $chr =~ s/\\(.)/$1/g;
33         $mnem !~ m/<dead | <KP_ | <U[0-9A-Fa-f]{4}/ or next;  # skip non-standard keys
34         eval {
35                 $mnem =~ s{<([^>]+)> \h?}{$symname->{$1} // die "reference to unknown keysym $1\n"}eg;
36                 1;
37         } or warn($@), next;
38         $mnem =~ m/\A [\x20-\x7F]{2} \z/ or next;  # only interested in two ascii
39
40         my $alias = \(state $seen = {})->{$chr};  # assume first is preferred
41         my $cp = ord $chr;
42         my ($class, $name, undef, undef, $string) = @{
43                 Shiar_Sheet::FormatChar->glyph_info($cp)
44         };
45         my $comparison = (
46                 !$vidi->{key}->{$mnem} ? 'l3' :  # free
47                 $vidi->{key}->{$mnem}->[0] != $cp ? 'l1' :  # conflict
48                 $vidi->{key}->{$mnem}->[2] eq 'l5' ? 'l5' :  # rfc
49                 'l4'  # any
50         );
51
52         if (${$alias}) {
53                 # aliases an earlier occurrence
54                 if ($matchvim and ${$alias}->[2] lt $comparison) {
55                         # replace lower compatibility level
56                         ${$alias}->[3] = 'l0';
57                         ${$alias}->[2] .=  ' u-' . ${$alias}->[2];
58                         ${$alias} = undef;
59                 }
60                 else {
61                         $class = 'l0';
62                         my $menm = substr($mnem, 1, 1).substr($mnem, 0, 1);
63                         if ($table{$menm} && $table{$menm}[0] == $cp) {
64                                 # unannotated if identical to reversed input
65                                 $cp = 0;
66                         }
67                         else {
68                                 $class .= ' ex';
69                         }
70                 }
71         }
72
73         $table{$mnem} = [ $cp, $name, $comparison, $class, $string // () ];
74         ${$alias} //= $table{$mnem};
75 }
76
77 print JSON->new->canonical->indent->encode({
78         title => 'X.Org',
79         key   => \%table,
80         intro => join("\n",
81                 'Character mnemonics following compose key ⎄:',
82                 'in the X Window System (Shift+AltGr by default).',
83                 'Differences from <a href="/digraphs">RFC-1345</a> are indicated.',
84                 'Also see <a href="/keyboard/altgr">monograph maps</a>',
85                 'of alternative Xorg input modes with an AltGr modifier.</p>',
86         ),
87         keywords => [qw( xorg x11 x )],
88         flag  => {
89                 'l5' => "matching RFC-1345",
90                 'l4' => "matching Vim extension",
91                 'l3' => "unique to Xorg",
92                 'l1' => "conflict",
93                 ('l0' => "Xorg preference") x !!$matchvim,
94                 'l0 ex' => "alias",
95         },
96         flagclass => {
97                 l5 => 'u-l4',
98                 l4 => 'u-l5',
99         },
100 });
101
102 __END__
103
104 =head1 NAME
105
106 mkdigraphs-xorg - Output Xorg compose sequences
107
108 =head1 SYNOPSIS
109
110
111     mkdigraphs-xorg /usr/share/X11/locale/en_US.UTF-8/Compose |
112     jq -r '.key."AT"[0]' | perl -nE 'say chr' # @
113
114 =head1 DESCRIPTION
115
116 Extracts Multi_key definitions from X11/Xorg Compose.pre include file.
117 If successful, a JSON object is output containing a digraphs list in C<key>
118 with Unicode code points keyed by mnemonics.
119 Any errors and warnings are given at STDERR.
120
121 =head1 AUTHOR
122
123 Mischa POSLAWSKY <perl@shiar.org>
124
125 =head1 LICENSE
126
127 Licensed under the GNU Affero General Public License version 3.
128