digraphs/xorg: json include with character info
[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
10 our $VERSION = '1.01';
11
12 my $symname = eval {
13         open my $keysymh, '<', 'data/keysymdef.json' or die $!;
14         local $/;
15         return decode_json(readline $keysymh);
16 } or die "Could not read keysym definitions: $@\n";
17
18 # optionally get unicode character information
19 my $uninfo = do './data/unicode-char.inc.pl'
20         or warn "could not include unicode details: ", $@ // $!;
21
22 my %table;
23 while ($_ = readline) {
24         my ($mnem, $chr, $trail) = m/\A <Multi_key> \h (.*?) \h+ : \h "([^"]+)" \h* (.*)/
25                 or next;
26         $chr =~ s/\\(.)/$1/g;
27         $mnem !~ m/<dead | <KP_ | <U[0-9A-Fa-f]{4}/ or next;  # skip non-standard keys
28         eval {
29                 $mnem =~ s{<([^>]+)> \h?}{$symname->{$1} // die "reference to unknown keysym $1\n"}eg;
30                 1;
31         } or warn($@), next;
32         $mnem =~ m/\A [\x20-\x7F]{2} \z/ or next;  # only interested in two ascii
33         my $alias = (state $seen = {})->{$chr}++;  # assume first is preferred
34         $table{$mnem} = [
35                 ord $chr,
36                 $uninfo->{$chr}->[1] // '',  # name
37                 0,  # comparison
38                 $alias ? 'l0 ex' :
39                 ($uninfo->{$chr}->[0] // '') =~ s/ u-di| u-prop| ex//gr,  # class
40                 $uninfo->{$chr}->[4] // (),  # string
41         ];
42 }
43
44 print JSON->new->canonical->indent->encode(\%table);
45
46 __END__
47
48 =head1 NAME
49
50 mkdigraphs-xorg - Output Xorg compose sequences
51
52 =head1 SYNOPSIS
53
54
55     mkdigraphs-xorg /usr/share/X11/locale/en_US.UTF-8/Compose >digraphs-xorg.inc.pl
56     perl -e'$di = do "digraphs-xorg.inc.pl"; print chr $di->{AT}'
57
58 =head1 DESCRIPTION
59
60 Extracts Multi_key definitions from X11/Xorg Compose.pre include file.
61 If successful, Perl code is output resulting in a hash
62 with Unicode code points keyed by mnemonics.
63 Any errors and warnings are given at STDERR.
64
65 =head1 AUTHOR
66
67 Mischa POSLAWSKY <perl@shiar.org>
68
69 =head1 LICENSE
70
71 Licensed under the GNU Affero General Public License version 3.
72