digraphs: translate vim support levels to appropriate fg classes
[sheet.git] / tools / mkdigraphs-xorg
index c2dd4414f55685e2635924ead388968ba86fe6e3..67c60a6f4fd7e4b92b1b658702ff71dd8e5ff2cb 100755 (executable)
@@ -4,40 +4,71 @@ use warnings;
 use utf8;
 use open IO => ':encoding(utf-8)', ':std';
 use re '/msx';
+use JSON 'decode_json';
 use Data::Dump 'pp';
+use Shiar_Sheet::FormatChar;
 
 our $VERSION = '1.01';
 
-my $keysymh;
-open $keysymh, '<', 'data/keysymdef.h'
-       or open $keysymh, '<', '/usr/include/X11/keysymdef.h'
-       or die "Could not find keysym definitions: $!\n";
-
-my %keysym;
-while (readline $keysymh) {
-       m{
-               \A  [#]define[ ]XK_ (?<name>[a-zA-Z_0-9]+)
-               \h+ 0x(?<value>[0-9a-f]+)
-               \h* [/][*] [\h(] U[+] (?<unicode>[0-9A-F]{4,6})
-       } or next;
-       $keysym{ $+{name} } = chr hex $+{unicode};
-}
+my $symname = eval {
+       open my $keysymh, '<', 'data/keysymdef.json' or die $!;
+       local $/;
+       return decode_json(readline $keysymh);
+} or die "Could not read keysym definitions: $@\n";
 
-say "# automatically generated by $0";
-say '+{';
+my $vidi = eval {
+       open my $jsfh, '<', 'data/digraphs.json' or die $!;
+       local $/;
+       return JSON->new->decode(readline $jsfh);
+} or warn "Could not read comparison digraphs: $@\n";
 
+my %table;
 while ($_ = readline) {
        my ($mnem, $chr, $trail) = m/\A <Multi_key> \h (.*?) \h+ : \h "([^"]+)" \h* (.*)/
                or next;
        $chr =~ s/\\(.)/$1/g;
        $mnem !~ m/<dead | <KP_ | <U[0-9A-Fa-f]{4}/ or next;  # skip non-standard keys
-       $mnem =~ s{<([^>]+)> \h?}{$keysym{$1} // die "reference to unknown keysym $1\n"}eg;
-       $mnem !~ m/[^\x20-\x7F]/ or next;  # skip unicode
-#      (state $seen = {})->{$chr}++ and next;
-       printf "%s => %s,\n", pp($mnem), pp($chr);
+       eval {
+               $mnem =~ s{<([^>]+)> \h?}{$symname->{$1} // die "reference to unknown keysym $1\n"}eg;
+               1;
+       } or warn($@), next;
+       $mnem =~ m/\A [\x20-\x7F]{2} \z/ or next;  # only interested in two ascii
+       my $alias = (state $seen = {})->{$chr}++;  # assume first is preferred
+       my $cp = ord $chr;
+       my $uninfo = Shiar_Sheet::FormatChar->glyph_info($cp);
+       my $comparison = (
+               !$vidi->{key}->{$mnem} ? 'l3' :  # free
+               $vidi->{key}->{$mnem}->[0] != $cp ? 'l1' :  # conflict
+               $vidi->{key}->{$mnem}->[2] eq 'l5' ? 'l5' :  # rfc
+               'l4'  # any
+       );
+       $table{$mnem} = [
+               $cp,
+               $uninfo->[1] // '',  # name
+               $comparison,
+               $alias ? 'l0 ex' : $uninfo->[0] // '',  # class
+               $uninfo->[4] // (),  # string
+       ];
 }
 
-say '}';
+print JSON->new->canonical->indent->encode({
+       title => 'X.Org',
+       key   => \%table,
+       intro => join("\n",
+               'Character mnemonics following compose key ⎄:',
+               'in the X Window System (Shift+AltGr by default).',
+               'Differences from <a href="/digraphs">RFC-1345</a> are indicated.',
+               'Also see <a href="/unicode">common Unicode</a>.',
+       ),
+       keywords => [qw( xorg x11 x )],
+       flag  => {
+               'l5' => "matching RFC-1345",
+               'l4' => "matching proposal",
+               'l3' => "unique to Xorg",
+               'l1' => "conflict",
+               'l0 ex' => "duplicate",
+       },
+});
 
 __END__
 
@@ -48,13 +79,13 @@ mkdigraphs-xorg - Output Xorg compose sequences
 =head1 SYNOPSIS
 
 
-    mkdigraphs-xorg /usr/share/X11/locale/en_US.UTF-8/Compose >digraphs-xorg.inc.pl
-    perl -e'$di = do "digraphs-xorg.inc.pl"; print chr $di->{AT}'
+    mkdigraphs-xorg /usr/share/X11/locale/en_US.UTF-8/Compose |
+    jq -r '.key."AT"[0]' | perl -nE 'say chr' # @
 
 =head1 DESCRIPTION
 
 Extracts Multi_key definitions from X11/Xorg Compose.pre include file.
-If successful, Perl code is output resulting in a hash
+If successful, a JSON object is output containing a digraphs list in C<key>
 with Unicode code points keyed by mnemonics.
 Any errors and warnings are given at STDERR.