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