aboutsummaryrefslogtreecommitdiff
path: root/compose2html
blob: 2d1533a400a11a9c27b760bd43caedfbc21906b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env perl 
use strict;
use warnings;
use 5.020;
use utf8;
use open ':std',':locale';
use List::AllUtils qw(natatime);
use HTML::Tiny;
use HTML::Entities;
use Unicode::UCD qw(charinfo);
use Unicode::Normalize;
 
my $COLUMNS = 5;
 
sub parse_compose_file {
    my ($fn) = @_;
    $fn //$ENV{HOME}.'/.XCompose';
 
    open my $fh'<:utf8'$fn;
    my @ret;
    while (my $line = <$fh>) {
        $line =~ s{#.*\z}{}sm;
 
        if ($line =~ m{^include \s* "(.+?)"}x) {
            my $file = $1;
            $file =~ s{%L}{/usr/share/X11/locale/$ENV{LC_ALL}/Compose}g;
            push @ret, parse_compose_file($file);
        }
        elsif ($line =~ m{<Multi_key>}) {
            my ($keys,$output) = split /:/,$line;
            unless ($keys && $output) {
                warn "<$line>";
                next;
            }
            my @keys = $keys =~ m{<(.+?)>}g;
            $output =~ s{\A \s* "(.+?)" .* \z}{$1}smx;
            $output = NFC($output);
            warn sprintf "%s %d <%s> [%s%d %s\n",
                $fn$.,
                join(' ',@keys),
                $output,length($output),
                join(' ',map { sprintf 'U+%04X'ord($_) } split //,$output);
            push @ret, [$output,@keys];
        }
    }
    return @ret;
}
 
sub sort_it {
    sort { $a->[0] cmp $b->[0] } @_
}
 
my %glyph = (
    acute => '`',
    ampersand => '&',
    apostrophe => q{'},
    asciicircum => '^',
    asciitilde => '~',
    asterisk => '*',
    at => '@',
    backslash => '\\',
    BackSpace => '',
    bar => '|',
    braceleft => '{',
    braceright => '}',
    bracketleft => '[',
    bracketright => ']',
    colon => ':',
    comma => ',',
    division => '/',
    dollar => '$',
    Down => '',
    equal => '=',
    exclam => '!',
    grave => q{'},
    greater => '>',
    Left => '',
    less => '<',
    minus => '-',
    Multi_key => '',
    numbersign => '#',
    parenleft => '(',
    parenright => ')',
    percent => '%',
    period => '.',
    plus => '+',
    question => '?',
    quotedbl => '"',
    Right => '',
    semicolon => ';',
    slash => '/',
    space => '',
    tilde => '~',
    underscore => '_',
    Up => '',
    KP_1 => '1⃣',
    KP_2 => '2⃣',
    KP_3 => '3⃣',
    KP_4 => '4⃣',
    KP_5 => '5⃣',
    KP_6 => '6⃣',
    KP_7 => '7⃣',
    KP_8 => '8⃣',
    KP_9 => '9⃣',
    KP_0 => '0⃣',
    #KP_Space => '␣⃣', # this seems to be used as equivalent to KP_2 
    KP_Equal => '=⃣',
    KP_Divide => '/⃣',
    KP_Add => '+⃣',
    KP_Multiply => '*⃣',
);
 
sub to_presentation_form {
    map {
        my ($output@keys) = @$_;
        my $unmapped=0;
        @keys = map {
            length($_)==1
                $_
                : ($glyph{$_// do { warn "What's a <$_>??";++$unmapped });
        @keys;
        if ($unmapped) {
            ();
        }
        else {
            [(join '',@keys),$output];
        }
    @_;
}
 
sub fill_table {
    my @rows;
    my $it = natatime $COLUMNS,@_;
    while (my @row_data = $it->()) {
        push @rows,[ \'tr', [
            map {
                my ($sequence$output) = @{$_};
                my @infos;
                if ($output =~ /\s|\P{Print}/) {
                    my @points = split //,$output;
                    for my $p (@points) {
                        my $info = charinfo(ord($p));
                        if ($info) {
                            push @infoslc($info->{name});
                        }
                        else {
                            warn sprintf 'no charinfo for %s (%04X)',
                                $p,ord($p);
                        }
                    }
                }
 
                (
                    [ \'th', {class=>'key'}, encode_entities($sequence), ],
                    [ \'td', {class=>'out'}, [
                        [\'span', {class=>'out'}, encode_entities($output), ],
                        (@infos
                             ? [ \'span',{class=>'info'}, encode_entities("(@infos)") ]
                             : ()
                         ),
                    ] ],
                );
            @row_data
        ] ];
    }
 
    return [ \'table', [
        @rows,
    ] ];
}
 
my $css = <<'CSS';
table {
 width: 100%;
 border-collapse: collapse;
 font-size: x-large;
}
tdth {
 text-align: left;
 border-top: solid thin black;
 vertical-align: baseline;
}
td {
 border-right: solid thin black;
 padding: 0.2em;
}
th {
 font-weight: normal;
 font-family: monospace;
 white-space: pre-line;
}
span.out {
 border: dotted thin black;
 padding: 0.1em;
}
span.info {
  margin-left: 0.5em;
  font-size: 50%;
}
CSS
 
print HTML::Tiny->new->html([
    [\'head',[
        [\'meta', {'http-equiv'=>'Content-Type',content=>'text/html;charset=utf-8'}],
        [\'style',$css],
        [\'title','Compose mappings']
    ] ],
    [ \'body', [
        fill_table to_presentation_form sort_it parse_compose_file,
    ] ],
]);