887d1abc24e3a0e2759f4caf8ad33addfa6b2dc5
[sheet.git] / digraphs.plp
1 <(common.inc.plp)><:
2 use 5.010;  # state
3
4 my $mode = ($ENV{PATH_INFO} // '') eq '/xorg' || exists $get{xorg};
5 my $modename = $mode ? 'X.Org' : 'RFC-1345';
6
7 Html({
8         title => 'digraph cheat sheet',
9         version => '1.1',
10         description => [
11                 "Complete table of digraph characters from $modename.",
12         ],
13         keywords => [qw'
14                 digraph mnemonic compose composition pair
15                 character char glyph table unicode vim xorg x11 x
16         '],
17         stylesheet => [qw'light'],
18         data => [qw( digraphs.inc.pl )],
19 });
20
21 :>
22 <h1><:= $modename :> Digraphs</h1>
23
24 <p>Character mnemonics following compose key ⎄<:
25 say join("\n",
26         $mode ? (
27                 ' in the X Window System (Shift+AltGr by default).',
28                 'Differences from <a href="/digraphs">RFC-1345</a> are indicated.',
29         ) : (':',
30                 'i^k in <a href="/vi">Vim</a>,',
31                 '^u^\ in <a href="/readline">Emacs</a>,',
32                 '^a^v in <a href="/screen">Screen</a>.',
33                 'Similar but different from <a href="/digraphs/xorg">X.Org</a>.',
34         ),
35         'Also see <a href="/unicode">common Unicode</a>.</p>',
36 );
37 say '<p class="aside">Unofficial <span class="u-prop ex">proposals</span>',
38         ' are available as <a href="/digraphs.vim">ex commands</a>.' if not $mode;
39 :>
40
41 <:
42 my $di = do 'digraphs.inc.pl'
43         or die "Error loading digraphs data: ", $@ // $!;
44
45 if (exists $get{v}) {
46         # show characters for inverted mnemonics (vim alternatives)
47         $di->{ substr($_, 1, 1) . substr($_, 0, 1) } ||=
48                 [ $di->{$_}->[0], '', 'l0 ex', '', $di->{$_}->[4] ]
49                 for grep { ref $di->{$_} } keys %{$di};
50 }
51
52 my @chars = (
53         [qw{! " % ' ( ) * + , - . /}],
54         ['0'..'9'], [qw{: ; < = > ?}],
55         ['A'..'M'], ['N'..'Z'],
56         ['a'..'m'], ['n'..'z'],
57 );
58 my @chars2 = (['_'], @chars);  # trailing character (extended set)
59 my @columns = !exists $get{split} ? \@chars2 :
60         ([@chars2[0, 1, 3, 4, 6]], [@chars2[2, 5, 7]]);
61
62 if ($mode) {
63         my $xorg = do 'data/digraphs-xorg.inc.pl'
64                 or die "Error loading Xorg data: ", $@ // $!;
65         $_ = [ord $_] for values %{$xorg};
66         $xorg->{$_}->[2] = # class = compatibility
67                 $di->{$_} ? $di->{$_}->[0] != $xorg->{$_}->[0] ? 'l1' :  # conflict
68                 $di->{$_}->[2] =~ /\bu-di\b/ ? 'l5' : 'l3' : 'l2'  # rfc|any|none
69                 for keys %{$xorg};
70
71         for my $cp (map {$_->[0]} values %{$xorg}) {
72                 next if (state $seen = {})->{$cp}++;  # List::MoreUtils::uniq
73
74                 # find multiple equivalent mnemonics
75                 my @equiv = grep {$cp eq $_->[0]}
76                         map {$xorg->{$_}} sort keys %{$xorg}; # values ordered by mnem.
77
78                 # search for the most compatible match
79                 my ($compat) = sort {
80                         $equiv[$b]->[2] cmp $equiv[$a]->[2]  # highest level
81                         || $b <=> $a  # fallback to last mnemonic
82                 } 0 .. $#equiv;
83
84                 # reclassify all but one as level 0 (omitted)
85                 splice @equiv, $compat // -1, 1, ();
86                 $_->[2] = 'l0 ex' for @equiv;
87         }
88
89         $chars2[0] = [qw( # ^ _ ` ~ )];
90         @chars = @chars2;
91         $di = $xorg;
92 }
93
94 for my $colchars (@columns) {
95 print '<table class="glyphs dimap"><col>';
96 print qq'<colgroup span="$_">' for map {scalar @$_} @{$colchars};
97 print "</colgroup><col>\n";
98 for my $section (qw{thead tfoot}) {
99         print "<$section><tr><th>↳";
100         print '<th>', EscapeHTML($_) for map {@$_} @{$colchars};
101         print "<th>&nbsp;\n";
102 }
103 for my $c1group (@chars) {
104         print '<tbody>';
105         for my $c1 (@$c1group) {
106                 print '<tr><th>', EscapeHTML($c1);
107                 for my $c2 (map {@$_} @$colchars) {
108                         my $mnem = $c1 . $c2;
109                         if (not defined $di->{$mnem}) {
110                                 print '<td>';
111                                 next;
112                         }
113                         if (ref $di->{$mnem} ne 'ARRAY') {
114                                 printf '<td class="X Xr" title="%s">', EscapeHTML($mnem);
115                                 next;
116                         }
117                         my ($codepoint, $name, $prop, $script, $string) = @{ $di->{$mnem} };
118
119                         my $glyph = $string || chr $codepoint;
120                         utf8::upgrade($glyph);  # prevent latin1 output
121                         my $desc = $mnem . ($name && " ($name)");
122                         my @class = ('X', grep {$_} $prop, $script);
123
124                         $glyph = EscapeHTML($glyph);
125                         $glyph = "<span>$glyph</span>" if $script =~ /\bZs\b/;
126
127                         printf "\n".'<td class="%s" title="%s">%s',
128                                 join(' ', @class), EscapeHTML($desc), $glyph;
129                 }
130                 print "\n<th>", EscapeHTML($c1), "\n";
131         }
132 }
133 print "</table>\n";
134 print '<hr>' if exists $get{split};
135 }
136
137 if ($mode) {
138 :>
139 <div class="legend">
140         <table class="glyphs"><tr>
141         <td class="X l5">matching RFC-1345
142         <td class="X l3">matching proposal
143         <td class="X l2">unique to Xorg
144         <td class="X l1">conflict
145         <td class="X l0 ex">duplicate
146         </table>
147 </div>
148 <: } else { :>
149 <div class="legend">
150         <table class="glyphs"><tr>
151         <td class="X Cc">control
152         <td class="X Zs"><span>space</span>
153         <td class="X Mn">combining
154         <td class="X Sk">spacing&nbsp;modifier
155         <td class="X Pf">quote
156         <td class="X Po">punctuation
157         <td class="X So">symbol
158         <td class="X Sm">math
159         <td class="X Sc">currency
160         <td class="X No">numeric
161         <td class="X Greek">greek
162         <td class="X Cyrillic">cyrillic
163         <td class="X Latin">latin
164         <td class="X Hebrew">hebrew
165         <td class="X Arabic">arabic
166         <td class="X Hangul">korean
167         <td class="X Hiragana">japanese
168         <td class="X Bopomofo">chinese
169         </table>
170
171         <table class="glyphs"><tr>
172         <td class="X">unicode
173         <td class="X Xl">latin1
174         <td class="X Xa">ascii
175         <td class="X u-prop">vim extension
176         <td class="X u-prop ex">proposal
177         <td class="X ex">not in vim
178         </table>
179 </div>
180
181 <: }